Sparse 0.0.1
Sparse Array for C
Loading...
Searching...
No Matches
sparse.h
1#ifndef _SPARSE_H_
2#define _SPARSE_H_
3
4#include "dynarr.h"
5
6#define ALIGNMENT sizeof(size_t)
7
8typedef dynarr_t sparse_t;
9
10typedef struct
11{
12 size_t element_size; /* excluding index and padding */
13 size_t initial_cap;
14 float grow_factor;
15 float grow_threshold;
16 float shrink_threshold;
17}
18sparse_opts_t;
19
20typedef struct
21{
22 size_t element_size; /* stores actual data size
23 (excuding index and padding) */
24}
25sparse_header_t;
26
27typedef enum sparse_status_t
28{
29 SPARSE_SUCCESS = DYNARR_SUCCESS,
30 SPARSE_ALLOC_ERROR = DYNARR_ALLOC_ERROR,
31 SPARSE_INSERT_INDEX_OVERRIDE = DYNARR_STATUS_LAST
32}
33sparse_status_t;
34
35/*
36* Create wrappers:
37*/
38#define sparse_create(...) \
39 sparse_create_( \
40 &(sparse_opts_t){ \
41 DYNARR_DEFAULT_ARGS, \
42 __VA_ARGS__ \
43 } \
44 )
45
46
47/*
48* Allocate new sparse array with provided options.
49*/
50sparse_t *sparse_create_(const sparse_opts_t *const opts);
51
52
53/*
54* Duplicates sparse array.
55*/
56sparse_t *sparse_clone(const sparse_t *const array);
57
58
59/*
60* Frees sparse array resources.
61*/
62void sparse_destroy(sparse_t *const array);
63
64
65/*
66* Returs neto data size of the element.
67* (excluding storage for index and padding)
68*/
69size_t sparse_element_size(const sparse_t *const array);
70
71
72/*
73* Actual amount of stored elements. (excluding empty ones)
74*/
75size_t sparse_size(const sparse_t *const array);
76
77
78/*
79* Returns first stored index.
80*/
81size_t sparse_first_index(const sparse_t *const array);
82
83
84/*
85* Returns last stored index.
86*/
87size_t sparse_last_index(const sparse_t *const array);
88
89
90/*
91* Returns first free index. (Middle insert may cause inefficiencies)
92*/
93size_t sparse_first_free_index(const sparse_t *const array);
94
95
96/*
97* Returns last free index that can be used to reserve an element.
98*/
99size_t sparse_last_free_index(const sparse_t *const array);
100
101
102/*
103* Inserts new element into an array,
104* will not override already stored element at provided index
105* (in which case returns SPARSE_INSERT_INDEX_OVERRIDE)
106*/
107sparse_status_t sparse_insert(sparse_t **const array, const size_t index, const void *const value);
108
109
110/*
111* Reserve space for element at requested index without copying data.
112* will not override already stored element at provided index
113* (in which case returns SPARSE_INSERT_INDEX_OVERRIDE)
114*/
115sparse_status_t sparse_insert_reserve(sparse_t **const array, const size_t index);
116
117
118/*
119* Returns stored element's value address.
120*/
121void *sparse_get(const sparse_t *array, const size_t index);
122
123
124/*
125* Removes an element at a given index.
126*/
127sparse_status_t sparse_remove(sparse_t **const array, const size_t index);
128
129
130/*
131* Checks wherether element exists.
132* Elements at indecies exceeding array bounds considered
133* to be empty as well.
134*/
135bool sparse_is_empty_element(const sparse_t *const array, const size_t index);
136
137
138typedef int (*sparse_foreach_t) (const size_t index, const void *const element, void *const param);
139typedef int (*sparse_aggregate_t) (const size_t index, const void *const element, void *const acc, void *const param);
140typedef int (*sparse_transform_t) (const size_t index, void *const element, void *const param);
141
142int sparse_foreach(const sparse_t *const sparse,
143 const sparse_foreach_t func,
144 void *const param);
145
146int sparse_aggregate(const sparse_t *const sparse,
147 const sparse_aggregate_t func,
148 void *const acc,
149 void *const param);
150
151int sparse_transform(sparse_t *const sparse,
152 const sparse_transform_t func,
153 void *const param);
154
155#endif/*_SPARSE_H_*/
struct vector_t dynarr_t
DYNARR_ALLOC_ERROR
DYNARR_SUCCESS
DYNARR_STATUS_LAST