
from __future__ import annotations

from collections.abc import Callable
from typing import Generic, TypeVar

UpperCAmelCase	      = TypeVar('''T''')
UpperCAmelCase	      = TypeVar('''U''')







class        A_   (			Generic[T, U]   ):




	'''simple docstring'''


	def __init__(       self		,    snake_case		,    snake_case       ):
		lowercase       =    key
		lowercase       =    val
		lowercase       =    None
		lowercase       =    None





	def __repr__(       self       ):
		return (
		    F'''Node: key: {self.key}, val: {self.val}, '''
		    F'''has next: {bool(self.next       )}, has prev: {bool(self.prev       )}'''
		)







class        A_   (			Generic[T, U]   ):




	'''simple docstring'''


	def __init__(       self       ):
		lowercase       =    DoubleLinkedListNode(snake_case		,    snake_case       )
		lowercase       =    DoubleLinkedListNode(snake_case		,    snake_case       )
		lowercase  ,					lowercase       =    self.rear, self.head





	def __repr__(       self       ):
		lowercase       =    ['DoubleLinkedList']
		lowercase       =    self.head
		while node.next is not None:
			rep.append(str(snake_case       )       )
			lowercase       =    node.next
		rep.append(str(self.rear       )       )
		return ",\n    ".join(snake_case       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case       ):
		lowercase       =    self.rear.prev

		# All nodes other than self.head are guaranteed to have non-None previous
		assert previous is not None

		lowercase       =    node
		lowercase       =    previous
		lowercase       =    node
		lowercase       =    self.rear





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case       ):

		if node.prev is None or node.next is None:
			return None

		lowercase       =    node.next
		lowercase       =    node.prev
		lowercase       =    None
		lowercase       =    None
		return node







class        A_   (			Generic[T, U]   ):




	'''simple docstring'''


	_UpperCamelCase     :     dict[Callable[[T], U], LRUCache[T, U]]							 =       {}





	def __init__(       self		,    snake_case       ):
		lowercase       =    DoubleLinkedList()
		lowercase       =    capacity
		lowercase       =    0
		lowercase       =    0
		lowercase       =    0
		lowercase       =    {}





	def __repr__(       self       ):
		return (
		    F'''CacheInfo(hits={self.hits}, misses={self.miss}, '''
		    F'''capacity={self.capacity}, current size={self.num_keys})'''
		)





	def __contains__(       self		,    snake_case       ):
		return key in self.cache





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case       ):
		# Note: pythonic interface would throw KeyError rather than return None

		if key in self.cache:
			self.hits += 1
			lowercase       =    self.cache[key]
			lowercase       =    self.list.remove(self.cache[key]       )
			assert node == value_node

			# node is guaranteed not None because it is in self.cache
			assert node is not None
			self.list.add(snake_case       )
			return node.val
		self.miss += 1
		return None





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case       ):

		if key not in self.cache:
			if self.num_keys >= self.capacity:
				# delete first node (oldest) when over capacity
				lowercase       =    self.list.head.next

				# guaranteed to have a non-None first node when num_keys > 0
				# explain to type checker via assertions
				assert first_node is not None
				assert first_node.key is not None
				assert (
				    self.list.remove(snake_case       ) is not None
				)  # node guaranteed to be in list assert node.key is not None

				del self.cache[first_node.key]
				self.num_keys -= 1
			lowercase       =    DoubleLinkedListNode(snake_case		,    snake_case       )
			self.list.add(self.cache[key]       )
			self.num_keys += 1

		else:
			# bump node to the end of the list, update value
			lowercase       =    self.list.remove(self.cache[key]       )
			assert node is not None  # node guaranteed to be in list
			lowercase       =    value
			self.list.add(snake_case       )





	@classmethod
	def 	SCREAMING_SNAKE_CASE__						(       cls		,    snake_case = 128       ):

		def cache_decorator_inner(snake_case       ) -> Callable[..., U]:
			def cache_decorator_wrapper(*snake_case       ) -> U:
				if func not in cls.decorator_function_to_instance_map:
					lowercase       =    LRUCache(snake_case       )

				lowercase       =    cls.decorator_function_to_instance_map[func].get(args[0]       )
				if result is None:
					lowercase       =    func(*snake_case       )
					cls.decorator_function_to_instance_map[func].put(args[0]		,    snake_case       )
				return result

			def cache_info() -> LRUCache[T, U]:
				return cls.decorator_function_to_instance_map[func]

			setattr(snake_case		,    'cache_info'		,    snake_case       )  # noqa: B010

			return cache_decorator_wrapper

		return cache_decorator_inner


if __name__ == "__main__":
	import doctest

	doctest.testmod()



from manim import *







class        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    Rectangle(height=0.5		,    width=0.5       )
		lowercase       =    Rectangle(height=0.25		,    width=0.25       )
		lowercase       =    Rectangle(height=0.46		,    width=0.46       ).set_stroke(width=0       )

		lowercase       =    [mem.copy() for i in range(6       )]
		lowercase       =    [mem.copy() for i in range(6       )]
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    VGroup(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    Text('CPU'		,    font_size=24       )
		lowercase       =    Group(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0.5		,    aligned_edge=snake_case       )
		cpu.move_to([-2.5, -0.5, 0]       )
		self.add(snake_case       )

		lowercase       =    [mem.copy() for i in range(4       )]
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    Text('GPU'		,    font_size=24       )
		lowercase       =    Group(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0.5		,    aligned_edge=snake_case       )
		gpu.move_to([-1, -1, 0]       )
		self.add(snake_case       )

		lowercase       =    [mem.copy() for i in range(6       )]
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )

		lowercase       =    Text('Model'		,    font_size=24       )
		lowercase       =    Group(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0.5		,    aligned_edge=snake_case       )
		model.move_to([3, -1.0, 0]       )
		self.add(snake_case       )

		lowercase       =    []
		lowercase       =    []
		lowercase       =    []

		for i, rect in enumerate(snake_case       ):
			rect.set_stroke(snake_case       )

			lowercase       =    Rectangle(height=0.46 / 4		,    width=0.46 / 3       ).set_stroke(width=0.0       ).set_fill(snake_case		,    opacity=0.7       )

			if i == 0:
				cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT       )		,    buff=0.02		,    direction=snake_case       )
				cpu_target.set_x(cpu_target.get_x() + 0.1       )
			elif i == 3:
				cpu_target.next_to(model_cpu_arr[0]		,    direction=snake_case		,    buff=0.0       )
			else:
				cpu_target.next_to(model_cpu_arr[i - 1]		,    direction=snake_case		,    buff=0.0       )
			self.add(snake_case       )
			model_cpu_arr.append(snake_case       )

		self.add(*snake_case		,    *snake_case		,    *snake_case       )

		lowercase       =    [mem.copy() for i in range(6       )]
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )

		lowercase       =    Text('Loaded Checkpoint'		,    font_size=24       )
		lowercase       =    Group(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0.5		,    aligned_edge=snake_case       )
		checkpoint.move_to([3, 0.5, 0]       )

		self.add(snake_case       )

		lowercase       =    []
		lowercase       =    []

		for i, rect in enumerate(snake_case       ):
			lowercase       =    fill.copy().set_fill(snake_case		,    opacity=0.7       )
			target.move_to(snake_case       )
			ckpt_arr.append(snake_case       )

			lowercase       =    target.copy()
			if i < 5:
				cpu_target.move_to(cpu_left_col_base[i + 1]       )
			else:
				cpu_target.move_to(cpu_right_col_base[i - 5]       )
			ckpt_cpu_arr.append(snake_case       )
		self.add(*snake_case		,    *snake_case       )

		lowercase       =    Square(side_length=2.2       )
		key.move_to([-5, 2, 0]       )

		lowercase       =    MarkupText(
		    F'''<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'''		,    font_size=18		,    )

		key_text.move_to([-5, 2.4, 0]       )

		self.add(snake_case		,    snake_case       )

		lowercase       =    MarkupText(
		    F'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'''		,    font_size=18		,    )

		blue_text.next_to(snake_case		,    DOWN * 2.4		,    aligned_edge=key_text.get_left()       )
		self.add(snake_case       )

		lowercase       =    MarkupText(
		    F'''Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device.'''		,    font_size=24		,    )
		step_a.move_to([2, 2, 0]       )

		lowercase       =    [meta_mem.copy() for i in range(6       )]
		lowercase       =    [meta_mem.copy() for i in range(6       )]
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    VGroup(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    Text('Disk'		,    font_size=24       )
		lowercase       =    Group(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0.5		,    aligned_edge=snake_case       )
		disk.move_to([-4.0, -1.25, 0]       )
		self.play(Write(snake_case		,    run_time=3       )		,    Write(snake_case		,    run_time=1       )		,    Create(snake_case		,    run_time=1       )       )

		lowercase       =    []
		for i, rect in enumerate(snake_case       ):
			lowercase       =    rect.copy()
			target.generate_target()
			target.target.move_to(disk_left_col_base[i]       ).scale(0.5       )
			animations.append(MoveToTarget(snake_case		,    run_time=1.5       )       )
		self.play(*snake_case       )

		self.play(FadeOut(snake_case       )       )

		lowercase       =    MarkupText(F'''Then, the checkpoint is removed from memory\nthrough garbage collection.'''		,    font_size=24       )
		step_a.move_to([2, 2, 0]       )

		self.play(Write(snake_case		,    run_time=3       )       )

		self.play(
		    FadeOut(snake_case		,    snake_case		,    *snake_case		,    *snake_case       )		,    )

		self.wait()

