InlineArray
Xi::InlineArray<T> is the highest-performance, continuous dynamic array in the Xi ecosystem. It is a Reference-Counted, Copy-on-Write (CoW) buffer designed to replace std::vector when zero-copy slicing and exact memory control are paramount.
Architectural Overview: Reference Counting & CoW
Unlike standard vectors that deep-copy their contents when passed by value, Xi::InlineArray operates via a tiny pointer shell to a shared heap Block.
Pass-by-Value is Free: Passing an
InlineArrayinto a function increments a strong reference count (useCount) instead of duplicating memory.Slicing is Free: You can create "Views" (sub-arrays) that point to the exact same shared memory block, simply adjusting their internal logical
offsetand logicallength.Copy On Write (CoW): If you attempt to mutate (
push(),operator[]) anInlineArraythat shares itsBlockwith another instance, it will transparently detach, allocate its own contiguous block, copy the data, and then apply the mutation. This guarantees perfect safety with immense performance benefits for read-heavy operations.1:1 C-API & STL Compatibility: Because internal memory is strictly contiguous, invoking
.data()returns a standardT*pointer. This meansInlineArrayseamlessly interoperates with all legacy C-Libraries (likememcpy), POSIX system calls, and Linux headers directly with zero headaches.
π Complete API Reference
1. Memory & Block Management
bool allocate(usz len, bool safe = false)Allocates contiguous memory forlenelements. Ifsafeis true, the method will instantly fail and returnfalseif the array shares memory with another instance (preventing CoW detachments).T* data()Returns the rawT*pointer starting precisely at the array's logicaloffset.usz size()/usz length_js()Returns the logical size of the array or view.void retain()/void destroy()Manual reference count overrides (typically handled automatically by C++ destructors and copy constructors).
2. Mutation Methods
void push(const T &val)Appends an element. Triggers a CoW detachment if the memory block is shared.void pushEach(const T *vals, usz count)Efficient bulk append from a raw C-pointer.T pop()Removes and returns the last element.void unshift(const T &val)Prepends an element to the front of the array, shifting everything right.T shift()Removes and returns the first element of the array, shifting everything left.
3. Data Retrieval & Lookup
bool has(usz idx)Returnstrueif the global index is strictly within the bounds of this logical view.long long indexOf(const T &val)Performs a linear scan and returns the index of the first matched element, or-1if not found.T& operator[](usz idx)Direct access. Triggers CoW detachment if writing to a shared block!
4. Zero-Copy Slicing & Views
InlineArray acts as its own View proxy. Slicing returns a lightweight InlineArray object that shares the original block.
InlineArray begin(usz start, usz end)Creates a bounded sub-view from logical indexstarttoend.InlineArray begin(usz start)Creates a sub-view from indexstartto the end of the array.
π» Example: Zero-Copy Views
Last updated