Skip to content

Commit

Permalink
iset: iset_has()
Browse files Browse the repository at this point in the history
  • Loading branch information
aligrudi committed Aug 24, 2022
1 parent 37c983e commit 36c06f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
14 changes: 13 additions & 1 deletion iset.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void iset_put(struct iset *iset, int key, int ent)
iset_extend(iset, ALIGN(key + 1, CNTMIN));
if (key >= 0 && key < iset->cnt && iset->len[key] + 1 >= iset->sz[key]) {
int olen = iset->sz[key];
int nlen = iset->sz[key] * 2 + 8;
int nlen = olen ? olen * 2 : 8;
void *nset = xmalloc(nlen * sizeof(iset->set[key][0]));
if (iset->set[key]) {
memcpy(nset, iset->set[key],
Expand All @@ -72,3 +72,15 @@ void iset_put(struct iset *iset, int key, int ent)
iset->set[key][iset->len[key]++] = ent;
iset->set[key][iset->len[key]] = -1;
}

/* check entry membership */
int iset_has(struct iset *iset, int key, int ent)
{
int i;
if (key < 0 || key >= iset->cnt || iset->len[key] == 0)
return 0;
for (i = 0; i < iset->len[key]; i++)
if (iset->set[key][i] == ent)
return 1;
return 0;
}
1 change: 1 addition & 0 deletions roff.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ void iset_free(struct iset *iset);
int *iset_get(struct iset *iset, int key);
void iset_put(struct iset *iset, int key, int ent);
int iset_len(struct iset *iset, int key);
int iset_has(struct iset *iset, int key, int ent);

/* mapping strings to longs */
struct dict *dict_make(int notfound, int dupkeys, int hashlen);
Expand Down

0 comments on commit 36c06f8

Please sign in to comment.