fixed capacity

class pythonic_fp.circulararray.fixed.CAF

Bases: Generic

Fixed storage capacity circular array CAF

  • O(1) pops and pushes either end

  • O(1) indexing, does not support slicing

  • fixed total storage capacity

  • iterable (see __iter__ method below)

  • comparisons compare identity before equality, like builtins

  • in boolean context, falsy when either empty or full, otherwise truthy

  • function caf produces fixed capacity circular array from arguments

__init__(*xs: Iterable, cap: int = 2) None

initializer

Populate CAF with an optional iterable from front (left) to rear (right).

param xs:

Takes an optional iterable parameter.

raises ValueError:

When more than one parameter is provided.

raises TypeError:

When passed a non-iterable parameter.

__bool__() bool

bool

  • falsy if either empty or full

  • truthy otherwise

returns:

True when partially filled, False otherwise.

__len__() int

length

Number of items in the CAF.

returns:

The number of items in the CAF.

__iter__() Iterator

iterate

Iterates circular array, left (front) to right (rear).

yields:

The CA contents front to rear.

Warning

Not threadsafe, especially for long living iterators.

Tip

Cache contents to make more thread tolerant. Put a lock around circular array during caching process to make threadsafe.

__reversed__() Iterator

reverse iterate

Iterates circular array, right (rear) to left (front).

yields:

The CAF contents rear to front.

__eq__(other: object) bool

equality comparison

Efficiently compare CAF to another object.

param other:

The object to be compared.

returns:

True if other is another CAF whose contents compare as equal to the corresponding contents of the CAF, otherwise False.

__getitem__(idx: int) X

getitem

Fixed capacity circular arrays are indexable but not sliceable.

__setitem__(idx: int, val: X) None

setitem

Fixed capacity circular arrays are indexable but not sliceable.

__delitem__(idx: int) None

delitem

Fixed capacity circular arrays are indexable but not sliceable.

__repr__() str

repr string

Construct string ‘CAF(x₁, x₂, … xₙ)’ where x₁, x₂, … xₙ are the contents displayed with repr().

returns:

A string to reproduce the CAF.

__str__() str

user string Construct string ‘(|x₁, x₂, … xₙ|)’ where x₁, x₂, …, xₙ are the contents displayed with str().

returns:

A string meaningful to an end user.

pushl(x: X) None

push left

Push single item from the left onto the CAF.

param x:

Single item to be pushed onto the front of the CAF from the left.

raises ValueError:

When called on a full fixed storage capacity circular array.

pushr(x: X) None

push right

Push single item from the right onto the CAF.

param x:

Single item to be pushed onto the rear of the CAF from the right.

raises ValueError:

When called on a full fixed storage capacity circular array.

popl() X

pop left

Pop a single items off the left side of the CAF.

returns:

Item popped from left side (front) of the CAF.

raises ValueError:

When called on an empty CAF.

popr() X

pop right

Pop a single items off the right side of the CAF.

returns:

Item popped from right side (rear) of the CAF.

raises ValueError:

When called on an empty CAF.

popld(default: X) X

pop Left with default

Pop a single items off the left side of the CAF.

param default:

Default value to return if CAF is empty.

returns:

Item popped from left side (front) of the CAF if not empty, otherwise return the provided default value.

poprd(default: X) X

pop Right with default

Pop a single items off the right side of the CAF.

param default:

Default value to return if CAF is empty.

returns:

Item popped from right side (rear) of the CAF if not empty, otherwise return the provided default value.

poplt(maximum: int) tuple[X, ...]

Pop multiple items from left

Pop items off the left side of the CAF.

param maximum:

Maximum number of items to pop, may pop less if not enough items in CAF.

returns:

A tuple of the items popped, left to right.

poprt(maximum: int) tuple[X, ...]

Pop multiple items from right

Pop items off the right side of the CAF.

param maximum:

Maximum number of items to pop, may pop less if not enough items in CAF.

returns:

A tuple of the items popped, right to left.

rotl(n: int = 1) None

Rotate left

Rotate contents of CAF to the left putting first item onto rear.

param n:

Number of times to shift items left. Default 1 time.

rotr(n: int = 1) None

Rotate right

Rotate contents of CAF to the right putting last item onto front.

param n:

Number of times to shift items right. Default 1 time.

map(f: Callable[[X], Y]) CAF

Map function over the CAF

Apply function f over the circular array’s contents.

param f:

Callable from type X to type Y.

returns:

New fixed capacity circular array instance.

foldl(f: Callable[[X, X], X]) X
foldl(f: Callable[[L, X], L], start: L) L

Fold left

Fold CAF left with a function and optional starting item.

param f:

Folding function, first argument to f is for the accumulator.

param start:

Optional starting item.

returns:

Reduced value produced by the left fold.

raises ValueError:

When circular array empty and start not given.

foldr(f: Callable[[X, X], X]) X
foldr(f: Callable[[X, R], R], start: R) R

Fold right

Fold CAF right left with a function and optional starting item.

param f:

Folding function, second argument to f is for the accumulator.

param start:

Optional starting item.

returns:

Reduced value produced by the right fold.

raises ValueError:

When circular array empty and start not given.

capacity() int

Get capacity

Get the fixed storage capacity of the circular array.

returns:

Fixed storage capacity.

empty() None

Empty circular array

Empty the circular array, keep current storage capacity.

fraction_filled() float

Get fraction filled

Find fraction of the storage capacity which is filled.

returns:

The ratio count/capacity.

pythonic_fp.circulararray.fixed.caf(*ts: T, cap: int = 2) CAF

Circular array factory function

Produce a fixed capacity circular array from a variable number of arguments.

param ts:

Initial items for a new fixed capacity circular array.

param cap:

The minimum storage capacity to set.

returns:

New fixed storage capacity circular array.