auto resizing¶
- class pythonic_fp.circulararray.auto.CA¶
Bases:
GenericAuto resizing circular array CA
O(1) pops either end
O(1) amortized pushes either end
O(1) indexing, fully supports slicing
auto-resizing more storage capacity when necessary, manually compatible
iterable (see __iter__ method below)
comparisons compare identity before equality, like builtins
in boolean context, falsy when empty, otherwise truthy
function
caproduces auto-resizing circular array from arguments
- __init__(*xs: Iterable) None¶
initializer
Populate
CAwith 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 when empty
truthy when not empty
- returns:
Truewhen not empty,Falseotherwise.
- __len__() int¶
length
Number of items in the
CA.- returns:
The number of items in the
CA.
- __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
CAcontents right to left.
- __eq__(other: object) bool¶
equality comparison
Efficiently compare
CAto another object.- param other:
The object to be compared.
- returns:
Trueifotheris anotherCAwhose contents compare as equal to the corresponding contents of theCA, otherwiseFalse.
- __getitem__(idx: int) X¶
- __getitem__(idx: slice) CA[X]
getitem
Auto resizing circular arrays are fully indexable and sliceable.
- __setitem__(idx: int, vals: X) None¶
- __setitem__(idx: slice, vals: Iterable[X]) None
setitem
Auto resizing circular arrays are fully indexable and sliceable.
- __delitem__(idx: int) None¶
- __delitem__(idx: slice) None
delitem
Auto resizing circular arrays are fully indexable and sliceable.
- __repr__() str¶
repr string
Construct string ‘CA(x₁, x₂, … xₙ)’ where x₁, x₂, … xₙ are the contents displayed with
repr().- returns:
A string to reproduce the
CA.
- __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(*xs: X) None¶
push left
Push items from the left onto the
CAin the order they were iterated.- param xs:
Items to be pushed onto the front of the
CAfrom the left.
- pushr(*xs: X) None¶
push right
Push items from the right onto the
CAin the order they were iterated.- param xs:
Items to be pushed onto the rear of the
CAfrom the right.
- popl() X¶
pop left
Pop a single items off the left side of the
CA.- returns:
Item popped from left side (front) of the
CA.- raises ValueError:
When called on an empty
CA.
- popr() X¶
pop right
Pop a single items off the right side of the
CA.- returns:
Item popped from right side (rear) of the
CA.- raises ValueError:
When called on an empty
CA.
- popld(default: X) X¶
pop Left with default
Pop a single items off the left side of the
CA.- param default:
Default value to return if
CAis empty.- returns:
Item popped from left side (front) of the
CAif 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
CA.- param default:
Default value to return if
CAis empty.- returns:
Item popped from right side (rear) of the
CAif 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
CA.- param maximum:
Maximum number of items to pop, may pop less if not enough items in
CA.- 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
CA.- param maximum:
Maximum number of items to pop, may pop less if not enough items in
CA.- returns:
A
tupleof the items popped, right to left.
- rotl(n: int = 1) None¶
Rotate left
Rotate contents of
CAto 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
CAto the right putting last item onto front.- param n:
Number of times to shift items right. Default 1 time.
- map(f: Callable[[X], Y]) CA¶
Map function over the CA
Apply function
fover the circular array’s contents.- param f:
Callable from type
Xto typeY.- returns:
New auto-resizing circular array instance.
- foldl(f: Callable[[X, X], X]) X¶
- foldl(f: Callable[[L, X], L], start: L) L
Fold left
Fold
CAleft 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
CAright 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 current storage capacity of the circular array.
- returns:
Current 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.
- resize(minimum_capacity: int = 2) None¶
Resize
Compact circular array and, if necessary, resize to a minimum storage capacity. To just compact the circular array, do not provide
minimum_capacity.- Parameters:
minimum_capacity – Minimum storage capacity to compact the circular array.