a lightweight dynamic (and generic) array
type C is missing.
This is not a linked list but a dynamic array. Meaning that their elements are just next to each other.
Although removing element in the middle is expensive for this type, every time its capacity hits the limit, realloc
occurs for the two times of the last capacity,
so tasks such as keep adding items, overwriting, removing the last element, slicing... are pretty cheap.
I'd have totally freed this [word]
hello world and bye world
I'd have totally freed this [world]
I'd have totally freed this [bye]
I'd have totally freed this [and]
I'd have totally freed this [world]
I'd have totally freed this [hello]
Being a smart ass, I decided that declare_array(typename)
macro to declare ItypenameI
struct type that totally looks like [typename]
.
- use
declare_array(typename)
globally to make array type oftypename
. - declare a
ItypenameI
variable. - use
II__init(variable, elementwise_cleanup, ...)
to initialize an array. - profit.
If you don't like two underscores (you have every right to be angry about it, but I have my own reasons), or the naming itself, please do change it, instead of swallowing it.
Use this with defer.h
to make the world better place, where the sun is shining, lollipops are singing, rainbows are giggling... and arrays are freeing themselves.