|
Data Structures and Algorithms
with Object-Oriented Design Patterns in Python |
class Heap(object):
def acquire(self, size):
"(Heap, int) -> int"
# ...
def release(self, offset):
"(Heap, int) -> None"
# ...
def __getitem__(self, offset):
"(Heap, int) -> int"
# ...
def __setitem__(self, offset, value):
"(Heap, int, int) -> None"
# ...
The acquire method allocates a region of size
consecutive ints in the array and returns the offset of the
first int in the region.
The release method release a region of ints
at the specified offset which was
obtained previously using acquire.
The __getitem__ and __setitem__ methods
access a value in the array at a given offset.class Handle(Object):
def getSize(self):
"(Handle) -> int"
# ...
getReference(self, offset):
"(Handle, int) -> Handle"
# ...
setReference(self, offset, handle):
"(Handle, int, Handle) -> None"
# ...
def __getitem__(self, offset):
"(Handle, int) -> int"
# ...
def __setitem__(self, offset, value):
"(Handle, int, int) -> int"
# ...
A handle refers to an object that contains
either ints or other handles.
The size of an object is total the number of ints
and handles it contains.
The various store and fetch methods are used to
insert and remove items from the object to which this
handle refers.class Heap(object):
def acquire(self, size):
"(Heap, int) -> Handle"
# ...
def release (self, handle):
"(Heap, Handle) -> None"
# ...
def collectGarbage(self):
"(Heap) -> None"
#
The acquire method allocates a handle and
space in the heap for an object of the given size.
The release method releases the given handle
but does not reclaim the associated heap space.
The collectGarbage method performs the
actual garbage collection operation.