Space API

Represents a Space that can be filled.

A Space is a one-dimensional structure such as a tape, a memory region, or a disk. It contains non-overlapping, objects that have positions and sizes. These objects are not necessarily stored consecutively; the Space may also contain gaps between objects or on the ends of the Space.

Objects can be placed into the Space either at defined positions or just wherever they’ll fit. For an object to fit at any given position, that next object in the space must be no closer than the object’s size to the position. If for instance you have a Space of size 10:

0123456789
..........

And place an object A into at position 4 with size 3, you’ll have:

0123456789
....AAA...

This space now still has room for an object of size <= 4 and an object of size <= 3, but cannot hold a larger object anymore. If you then place an object B of size 4 with no guidelines on where to fit it, it will fill the first available gap:

0123456789
BBB.AAA...

Presently there is no ability to remove items from a Space.

The Space Container

class registermaps.space.Space(size=None, resizer=<class 'registermaps.space.NoResizer'>, placer=<class 'registermaps.space.NoPlacer'>)

A Space that can be filled.

Optional arguments are an initial size, a Resizer class, and a Placer class. Without providing a Resizer, the initial size is the size the Space will always be. Without providing a Placer no objects can be placed in the Space, making it pretty useless.

In boolean context, Space is True if there are any items stored in it, or False if the space is completely empty.

The data member enforce_rules_on_fixed determines whether when calling the .add method with a fixed start position the placer rules are used to determine whether that placement is legal. The default value of False allows explicit placement regardless of placer rules.

__bool__()

True if there are any true items in the space.

__getitem__(idx)

Shorthand for space.at(idx) or space.takeslice(idx) based on idx.

__init__(size=None, resizer=<class 'registermaps.space.NoResizer'>, placer=<class 'registermaps.space.NoPlacer'>)

Initialize self. See help(type(self)) for accurate signature.

__iter__()

Iterate over everything, items and gaps, in the space.

__str__()

A string respresentation for debugging.

__weakref__

list of weak references to the object (if defined)

add(obj, size, start=None)

Add an object into the Space.

Returns a PlacedObject, though this can usually be ignored. Raises ValueError if the object cannot be placed.

start, if given, provides a fixed start location.

addfixed(obj, size, start)

Add the object at a fixed location or raise a ValueError.

addfloating(obj, size)

Try to find a place for this object and place it there.

at(index)

Returns the PlacedObject that encompasses index.

Can also be called as space[index] where index is an int.

It will be true that for PlacedObject obj, obj.start <= index < obj.end

gapcount

Number of gaps in the space.

gaps()

Iterate over all the gaps in the space.

itemcount

Number of true items in the space.

items()

Iterate over all the actual items in the space.

last()

Returns a PlacedObject for the end of the space, either a gap or true object.

lastgap()

Returns a PlacedObject (possibly of zero size) representing the last gap

takeslice(slc)

Returns a list of PlacedObjects spanning the slice slc. The start and end object may be truncated such that the start will start at the slice start and the end ends at the slice end.

Can also be called as space[slc] where slc is a slice.

class registermaps.space.PlacedObject(obj, start, size)

Represents an object as placed in the Space.

Also iterable as obj, start, size for tuple unpacking.

Gaps are False, real objects are True.

obj

The stored object. If this object is None, the PlacedObject represents a gap rather than an object.

Type:object
start

The starting location in the Space.

Type:int
size

The amount of the Space occupied by the object.

Type:int
end

Position 1 past the end.

Placers

class registermaps.space.NoPlacer

A null Placer, prevents adding objects to a Space.

class registermaps.space.LinearPlacer

Place an object in the first place it will fit.

class registermaps.space.BinaryPlacer

Place an object on a power-of-2 boundary based on size.

class registermaps.space.Placer

Abstract base class for a Resizer.

place(obj, size, gap)

Try to place an object into a given gap.

Returns a new PlacedObject or None if it won’t fit.

placeInfinite(obj, size, minstart)

Make a PlacedObject in an infinite gap starting at minstart.

validate(po)

Is this PlacedObject legal by these placer rules?

Resizers

class registermaps.space.NoResizer

A null resizer; prevents resizing a Space.

class registermaps.space.LinearResizer

Adds only as much space as needed.

class registermaps.space.BinaryResizer

Adds enough space to keep a Space.size a power of 2.

class registermaps.space.Resizer

Abstract base class for a Resizer.

resize(spc, minsize)

Resize a Space to at least minsize.

spc - The space to be resized. minsize - The minimum new size of the space.