diff options
| author | Tyge Løvset <[email protected]> | 2021-02-04 13:06:27 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-02-04 13:06:27 +0100 |
| commit | 20dd32503b8f0c556984ecacae83e22d9ac8e73b (patch) | |
| tree | f1ac366f3182f7a2fefb4860152a153df397ee58 | |
| parent | bb15765f34e22bc09eee029e19b8be01c49462a9 (diff) | |
| download | STC-modified-20dd32503b8f0c556984ecacae83e22d9ac8e73b.tar.gz STC-modified-20dd32503b8f0c556984ecacae83e22d9ac8e73b.zip | |
Changed return type it iter_t in csmap_X_find() and cmap_X_find() to be consistent.
| -rw-r--r-- | docs/ccommon_api.md | 50 | ||||
| -rw-r--r-- | docs/cmap_api.md | 4 | ||||
| -rw-r--r-- | docs/csmap_api.md | 4 | ||||
| -rw-r--r-- | examples/advanced.c | 2 | ||||
| -rw-r--r-- | examples/complex.c | 2 | ||||
| -rw-r--r-- | examples/csmap_v1.h | 10 | ||||
| -rw-r--r-- | examples/demos.c | 2 | ||||
| -rw-r--r-- | examples/phonebook.c | 2 | ||||
| -rw-r--r-- | examples/stc_astar.c | 2 | ||||
| -rw-r--r-- | stc/cmap.h | 14 | ||||
| -rw-r--r-- | stc/csmap.h | 10 |
11 files changed, 64 insertions, 38 deletions
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 596ba4ad..210c75ae 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -5,26 +5,27 @@ This describes the features the ccommon.h header file. ## Macros The following macros a completely safe to use, with no side-effects. -#### c_new, c_del -- Type* c_new (VType) -- Type* c_new (VType, size_t N) -- c_del (CType, CType* x1, ..., CType* xN) -#### c_malloc, c_calloc, c_realloc, c_free -Macros that can be overloaded by user to use a different allocator for the entire library +#### c_new, c_del -#### c_foreach -- c_foreach (it, CType, container) +| Usage | Meaning | +|:-------------------------------|:----------------------------------------| +| `c_new (type)` | `(type *) c_malloc(sizeof(type))` | +| `c_new (type, N)` | `(type *) c_malloc((N) * sizeof(type))` | +| `c_del (ctype, c1, ..., cN)` | `ctype_del(c1); ... ctype_del(cN)` | ```c -using_cvec(x, double); -... -cvec_x vec = cvec_x_init(); -double sum = 0; -c_foreach (i, cvec_x, vec) sum += *i.ref; +int* array = c_new (int, 100); +c_free(array); + +cstr a = cstr_from("Hello"), b = cstr_from("World"); +c_del(cstr, &a, &b); ``` +#### c_malloc, c_calloc, c_realloc, c_free +Memory allocator for the entire library. Macros can be overloaded by the user. + #### c_forrange -Declare an iterator and specify a range to iterate with a for loop. Like python's ***range()*** function: +Declare an iterator and specify a range to iterate with a for loop. Like python's ***for i in range()*** function: | Usage | Python equivalent | |:----------------------------------------------|:-------------------------------------| @@ -37,7 +38,7 @@ Declare an iterator and specify a range to iterate with a for loop. Like python' ```c c_forrange (5) printf("x"); // xxxxx -c_forrange (i, 5) printf(" %zu"); +c_forrange (i, 5) printf(" %zu", i); // 0 1 2 3 4 c_forrange (i, int, -3, 3) printf(" %d", i); // -3 -2 -1 0 1 2 @@ -45,6 +46,25 @@ c_forrange (i, int, 30, 0, -5) printf(" %d", i); // 30 25 20 15 10 5 ``` +#### c_foreach + +| Usage | Description | +|:----------------------------------------------|:--------------------------------| +| `c_foreach (it, ctype, container)` | `Iteratate all elements ` | +| `c_foreach (it, ctype, it1, it2)` | `Iterate the range [it1, it2)` | + +```c +using_csset(x, int); +... +c_init (csset_x, set, {23, 3, 7, 5, 12}); +double sum = 0; +c_foreach (i, csset_x, set) printf(" %d", *i.ref); +// 3 5 7 12 23 +csset_x_iter_t it = csset_x_find(&set, 7); +c_foreach (i, csset_x, it, csset_x_end(&set)) printf(" %d", *i.ref); +// 7 12 23 +``` + #### c_withbuffer #### c_withfile diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 3fee4e66..4a39e3ae 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -79,7 +79,7 @@ size_t cmap_X_erase(cmap_X* self, RawKey rkey); void cmap_X_erase_entry(cmap_X* self, cmap_X_value_t* entry); cmap_X_iter_t cmap_X_erase_at(cmap_X* self, cmap_X_iter_t pos); -cmap_X_value_t* cmap_X_find(const cmap_X* self, RawKey rkey); // NULL if not found +cmap_X_iter_t cmap_X_find(const cmap_X* self, RawKey rkey); bool cmap_X_contains(const cmap_X* self, RawKey rkey); cmap_X_iter_t cmap_X_begin(cmap_X* self); @@ -316,7 +316,7 @@ int main() VikingRaw lookup = {"Einar", "Norway"}; - cmap_vk_value_t *e = cmap_vk_find(&vikings, lookup); + cmap_vk_value_t *e = cmap_vk_find(&vikings, lookup).ref; e->second += 3; // add 3 hp points to Einar cmap_vk_emplace(&vikings, lookup, 0).first->second += 5; // add 5 more to Einar diff --git a/docs/csmap_api.md b/docs/csmap_api.md index 92457d2c..2ebf08d8 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -68,8 +68,8 @@ csmap_X_mapped_t* csmap_X_at(const csmap_X* self, RawKey rkey); size_t csmap_X_erase(csmap_X* self, RawKey rkey); csmap_X_iter_t csmap_X_erase_at(csmap_X* self, csmap_X_iter_t pos); -csmap_X_value_t* csmap_X_find(const csmap_X* self, RawKey rkey); // NULL if not found -csmap_X_value_t* csmap_X_find_it(const csmap_X* self, RawKey rkey, csmap_X_iter_t* out); +csmap_X_iter_t csmap_X_find(const csmap_X* self, RawKey rkey); +csmap_X_value_t* csmap_X_find_it(const csmap_X* self, RawKey rkey, csmap_X_iter_t* out); // return NULL if not found bool csmap_X_contains(const csmap_X* self, RawKey rkey); csmap_X_iter_t csmap_X_begin(csmap_X* self); diff --git a/examples/advanced.c b/examples/advanced.c index 753bba1e..f99d8671 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -54,7 +54,7 @@ int main() VikingRaw lookup = {"Einar", "Norway"}; - cmap_vk_value_t *e = cmap_vk_find(&vikings, lookup); + cmap_vk_value_t *e = cmap_vk_find(&vikings, lookup).ref; e->second += 3; // add 3 hp points to Einar cmap_vk_emplace(&vikings, lookup, 0).first->second += 5; // add 5 more to Einar diff --git a/examples/complex.c b/examples/complex.c index cbf07af2..d7ea016e 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -30,7 +30,7 @@ int main() { cmap_s_put(&myMap, strKey, listMap);
// Access the data entry
- carray2f arr_b = *clist_a_back(&cmap_l_find(&cmap_s_find(&myMap, strKey)->second, tableKey)->second);
+ carray2f arr_b = *clist_a_back(&cmap_l_find(&cmap_s_find(&myMap, strKey).ref->second, tableKey).ref->second);
printf("value (%d, %d) is: %f\n", y, x, *carray2f_at(&arr_b, y, x));
cmap_s_del(&myMap); // free up everything!
diff --git a/examples/csmap_v1.h b/examples/csmap_v1.h index 34917c5d..bc7a978d 100644 --- a/examples/csmap_v1.h +++ b/examples/csmap_v1.h @@ -142,7 +142,7 @@ int main(void) { typedef struct { \
C##_##X##_value_t *ref; \
int _top; \
- C##_##X##_node_t *_tn, *_st[50]; \
+ C##_##X##_node_t *_tn, *_st[48]; \
} C##_##X##_iter_t
@@ -206,14 +206,16 @@ int main(void) { STC_API C##_##X##_value_t* \
C##_##X##_find_it(const C##_##X* self, RawKey rkey, C##_##X##_iter_t* out); \
\
- STC_INLINE C##_##X##_value_t* \
+ STC_INLINE C##_##X##_iter_t \
C##_##X##_find(const C##_##X* self, RawKey rkey) { \
C##_##X##_iter_t it; \
- return C##_##X##_find_it(self, rkey, &it); \
+ C##_##X##_find_it(self, rkey, &it); \
+ return it; \
} \
STC_INLINE bool \
C##_##X##_contains(const C##_##X* self, RawKey rkey) { \
- return C##_##X##_find(self, rkey) != NULL; \
+ C##_##X##_iter_t it; \
+ return C##_##X##_find_it(self, rkey, &it) != NULL; \
} \
\
STC_API C##_##X##_result_t \
diff --git a/examples/demos.c b/examples/demos.c index aa1fa28b..46715b74 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -162,7 +162,7 @@ void mapdemo3() cmap_str_put(&table, "Map", "test");
cmap_str_put(&table, "Make", "my");
cmap_str_put(&table, "Sunny", "day");
- cmap_str_value_t *e = cmap_str_find(&table, "Make");
+ cmap_str_value_t *e = cmap_str_find(&table, "Make").ref;
c_foreach (i, cmap_str, table)
printf("entry: %s: %s\n", i.ref->first.str, i.ref->second.str);
printf("size %zu: remove: Make: %s\n", cmap_str_size(table), e->second.str);
diff --git a/examples/phonebook.c b/examples/phonebook.c index 1bedcce9..0f781334 100644 --- a/examples/phonebook.c +++ b/examples/phonebook.c @@ -52,7 +52,7 @@ int main(int argc, char **argv) printf("\nPhone book after adding Zak Byers:\n");
print_phone_book(phone_book);
- if (cmap_str_find(&phone_book, "Tariq Beltran") != NULL)
+ if (cmap_str_find(&phone_book, "Tariq Beltran").ref != NULL)
printf("\nTariq Beltran is in phone book\n");
erased = cmap_str_erase(&phone_book, "Tariq Beltran");
diff --git a/examples/stc_astar.c b/examples/stc_astar.c index 6421693a..91d475e9 100644 --- a/examples/stc_astar.c +++ b/examples/stc_astar.c @@ -103,7 +103,7 @@ astar(cstr maze, int width) int new_cost = *csmap_mc_at(&cost_so_far, current);
if (maze.str[mpoint_index(&next)] != '#')
{
- csmap_mc_value_t* cost = csmap_mc_find(&cost_so_far, next);
+ csmap_mc_value_t* cost = csmap_mc_find(&cost_so_far, next).ref;
if (!cost || new_cost < cost->second)
{
csmap_mc_put(&cost_so_far, next, new_cost); // update (put)
@@ -42,8 +42,8 @@ int main(void) { cmap_mx_put(&m, 5, 'a');
cmap_mx_put(&m, 8, 'b');
cmap_mx_put(&m, 12, 'c');
- cmap_mx_value_t *e = cmap_mx_find(&m, 10); // = NULL
- char val = cmap_mx_find(&m, 5)->second;
+ cmap_mx_iter_t it = cmap_mx_find(&m, 10); // none
+ char val = cmap_mx_find(&m, 5).ref->second;
cmap_mx_put(&m, 5, 'd'); // update
cmap_mx_erase(&m, 8);
c_foreach (i, cmap_mx, m)
@@ -221,7 +221,7 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; C##_##X##_del(C##_##X* self); \
STC_API void \
C##_##X##_clear(C##_##X* self); \
- STC_API C##_##X##_value_t* \
+ STC_API C##_##X##_iter_t \
C##_##X##_find(const C##_##X* self, RawKey rkey); \
STC_API bool \
C##_##X##_contains(const C##_##X* self, RawKey rkey); \
@@ -357,11 +357,13 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; return b; \
} \
\
- STC_DEF C##_##X##_value_t* \
+ STC_DEF C##_##X##_iter_t \
C##_##X##_find(const C##_##X* self, RawKey rkey) { \
- if (self->size == 0) return NULL; \
+ C##_##X##_iter_t it = {NULL}; \
+ if (self->size == 0) return it; \
C##_bucket_t b = C##_##X##_bucket(self, &rkey); \
- return self->_hashx[b.idx] ? &self->table[b.idx] : NULL; \
+ if (*(it._hx = self->_hashx+b.idx)) it.ref = self->table+b.idx; \
+ return it; \
} \
\
STC_DEF bool \
diff --git a/stc/csmap.h b/stc/csmap.h index 4edbb277..1adfcbc2 100644 --- a/stc/csmap.h +++ b/stc/csmap.h @@ -147,7 +147,7 @@ int main(void) { C##_##X##_value_t *ref; \
C##_##X##_node_t *_d; \
int _top; \
- C##_##X##_size_t _tn, _st[64]; \
+ C##_##X##_size_t _tn, _st[48]; \
} C##_##X##_iter_t
#define _using_CBST(X, C, Key, Mapped, keyCompareRaw, mappedDel, keyDel, \
@@ -207,14 +207,16 @@ int main(void) { STC_API C##_##X##_value_t* \
C##_##X##_find_it(const C##_##X* self, RawKey rkey, C##_##X##_iter_t* out); \
\
- STC_INLINE C##_##X##_value_t* \
+ STC_INLINE C##_##X##_iter_t \
C##_##X##_find(const C##_##X* self, RawKey rkey) { \
C##_##X##_iter_t it; \
- return C##_##X##_find_it(self, rkey, &it); \
+ C##_##X##_find_it(self, rkey, &it); \
+ return it; \
} \
STC_INLINE bool \
C##_##X##_contains(const C##_##X* self, RawKey rkey) { \
- return C##_##X##_find(self, rkey) != NULL; \
+ C##_##X##_iter_t it; \
+ return C##_##X##_find_it(self, rkey, &it) != NULL; \
} \
\
STC_API C##_##X##_result_t \
|
