ref: 36c06f8d2cfb2fe82dc7d5b435bce2bbb26b5836
parent: 37c983e7dd7d4f9a908ab206eb54866026e1042c
author: Ali Gholami Rudi <ali@rudi.ir>
date: Wed Aug 24 14:00:54 EDT 2022
iset: iset_has()
--- a/iset.c
+++ b/iset.c
@@ -59,7 +59,7 @@
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],
@@ -71,4 +71,16 @@
}
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;
}
--- a/roff.h
+++ b/roff.h
@@ -121,6 +121,7 @@
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);