list
- class typedlist
list-like array that stores objects as packed data. The objects added must necessarily be packed objects (builtin objects with no references).
This is a dynamically sized, borrowing array, which mean the internal buffer of data is reallocated on insertion, but can be used to view and extract from any buffer.
Methods added to the signature of list:
reserve(n)
reallocate if necessary to make sure n elements canbe inserted without reallocation
capacity() -> int
return the current number of elements that can becontained without reallocation
shrink()
shorten the allocated memory to fit the current contentalso the slices do not copy the content
Use it as a list:
>>> a = typedlist(dtype=vec3) >>> >>> # build from an iterable >>> a = typedlist([], dtype=vec3) >>> >>> # append some data >>> a.append(vec3(1,2,3)) >>> >>> # extend with an iterable >>> a.extend(vec3(i) for i in range(5)) >>> >>> len(a) # the current number of elements 6 >>> >>> a.owner # the current data buffer b'.........'
Use it as a slice:
>>> # no data is copied >>> myslice = a[:5] typedlist(....)
Use it as a view on top of a random buffer
>>> a = np.ones((6,3), dtype='f4') >>> myslice = typedlist(a, dtype=vec3)
It does support the buffer protocol, so it can be converted in a great variety of well known arrays, even without any copy
>>> np.array(typedlist([....]))
Constructors:
- size
byte size of the current content
- Type:
int
- allocated
byte size of the memory allocated memory
- Type:
int
- owner
object realy owning the data instead of the current
typedlist
- dtype
the python data type
- Type:
type
- static empty(dtype, size)
create a new typedlist with the given
size
and unitialized elements of typedtype
- static full(value, size)
create a new typedlist with the given
size
, all elements initialized tovalue
.
Methods matching those from
list
- append(value)
append the given object at the end of the array
if there is not enough allocated memory, reallocate enough to amortize the realocation time over the multiple appends
- extend(iterable)
append all elements from the other array
- insert(index, value)
insert value at index
- clear()
remove all elements from the array but does not deallocate, very fast operation
- reverse()
reverse the order of elementd contained
- index(value)
return the index of the first element binarily equal to the given one
- __add__()
concatenation of two arrays
- __mul__()
duplicate the sequence by a certain number
- __getitem__()
self[index]
currently supports:
indices
negative indices
slices with step=1
- __setitem__(key, value, /)
Set self[key] to value.
- __delitem__(key, /)
Delete self[key].
- __iter__()
yield successive elements in the list
- __copy__()
shallow copy will create a copy of that array referencing the same buffer
- __deepcopy__(memo)
deep recursive copy, will duplicate the viewed data in the underlying buffer
The following methods are added on top of python
list
signature, in order to manage memory in a more efficient way.
- capacity()
return the total number of elements that can be stored with the current allocated memory
- reserve(amount: int)
Make sure there is enough allocated memory to append the given amount of elements.
if there is not enough of allocated memory, the memory is reallocated immediately.
- shrink()
reallocate the array to have allocated the exact current size of the array