auto resizing

class pythonic_fp.circulararray.auto.CA

Bases: Generic

Auto 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 ca produces auto-resizing circular array from arguments

__init__(*xs: Iterable) None

initializer

Populate CA 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 when empty

  • truthy when not empty

returns:

True when not empty, False otherwise.

__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 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 CA contents right to left.

__eq__(other: object) bool

equality comparison

Efficiently compare CA to another object.

param other:

The object to be compared.

returns:

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

__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 CA in the order they were iterated.

param xs:

Items to be pushed onto the front of the CA from the left.

pushr(*xs: X) None

push right

Push items from the right onto the CA in the order they were iterated.

param xs:

Items to be pushed onto the rear of the CA from 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 CA is empty.

returns:

Item popped from left side (front) of the CA 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 CA.

param default:

Default value to return if CA is empty.

returns:

Item popped from right side (rear) of the CA 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 CA.

param maximum:

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

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 CA.

param maximum:

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

returns:

A tuple of the items popped, right to left.

rotl(n: int = 1) None

Rotate left

Rotate contents of CA 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 CA 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]) CA

Map function over the CA

Apply function f over the circular array’s contents.

param f:

Callable from type X to type Y.

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 CA 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 CA right 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 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.

pythonic_fp.circulararray.auto.ca(*ts: T) CA

Circular array factory function

Produce a auto resizing circular array from a variable number of arguments.

param ts:

Initial items for a new auto-resizing circular array.

returns:

New variable storage capacity circular array.