pmhash: permite iterar os valores.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sun, 11 Dec 2016 13:39:39 +0000 (11:39 -0200)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sun, 11 Dec 2016 13:39:39 +0000 (11:39 -0200)
A função pmhash_next retorna o próximo valor de um hash dado um iterador
opaco que deve ser iniciado com NULL.

lib/pmhash.c
lib/pmhash.h

index 5bada1e..2bf6c43 100644 (file)
@@ -95,3 +95,17 @@ void pmhash_del(struct pmhash *pmhash)
        }
        free(pmhash);
 }
+
+void * pmhash_next(struct pmhash *pmhash, void **iter)
+{
+       int i;
+       struct item *item = *iter;
+       if (item == NULL)
+               i = 0;
+       else
+               i = item - pmhash->items + 1;
+       if (i >= (int) pmhash->len)
+               return NULL;
+       *iter = pmhash->items + i;
+       return pmhash->items[i].val;
+}
index d1c0206..a4e6019 100644 (file)
@@ -25,5 +25,6 @@ struct pmhash * pmhash_new(void);
 int pmhash_add(struct pmhash **pmhash, char *key, void *val);
 void * pmhash_get(struct pmhash *pmhash, char *key);
 void pmhash_del(struct pmhash *pmhash);
+void * pmhash_next(struct pmhash *pmhash, void **iter);
 
 #endif