fixed capacity¶
- class pythonic_fp.circulararray.fixed.CAF¶
Bases:
GenericFixed 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
cafproduces fixed capacity circular array from arguments
- __init__(*xs: Iterable, cap: int = 2) None¶
initializer
Populate
CAFwith 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:
Truewhen partially filled,Falseotherwise.
- __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
CAcontents 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
CAFcontents rear to front.
- __eq__(other: object) bool¶
equality comparison
Efficiently compare
CAFto another object.- param other:
The object to be compared.
- returns:
Trueifotheris anotherCAFwhose contents compare as equal to the corresponding contents of theCAF, otherwiseFalse.
- __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
CAFfrom 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
CAFfrom 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
CAFis empty.- returns:
Item popped from left side (front) of the
CAFif 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
CAFis empty.- returns:
Item popped from right side (rear) of the
CAFif 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
tupleof 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
tupleof the items popped, right to left.
- rotl(n: int = 1) None¶
Rotate left
Rotate contents of
CAFto 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
CAFto 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
fover the circular array’s contents.- param f:
Callable from type
Xto typeY.- 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
CAFleft with a function and optional starting item.- param f:
Folding function, first argument to
fis for the accumulator.- param start:
Optional starting item.
- returns:
Reduced value produced by the left fold.
- raises ValueError:
When circular array empty and
startnot given.
- foldr(f: Callable[[X, X], X]) X¶
- foldr(f: Callable[[X, R], R], start: R) R
Fold right
Fold
CAFright left with a function and optional starting item.- param f:
Folding function, second argument to
fis for the accumulator.- param start:
Optional starting item.
- returns:
Reduced value produced by the right fold.
- raises ValueError:
When circular array empty and
startnot 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.