* (c) 2002--2004 Martin Mares <mj@ucw.cz>
* (c) 2002--2005 Robert Spalek <robert@ucw.cz>
* (c) 2010 Pavel Charvat <pchar@ucw.cz>
+ * (c) 2012 Tomas Valla <tom@ucw.cz>
*
* This software may be freely distributed and used according to the terms
* of the GNU Lesser General Public License.
* HASH_WANT_LOOKUP node *lookup(key) -- find node with given key,
* if it doesn't exist, create it. Defining
* HASH_GIVE_INIT_DATA is strongly recommended.
+ * Use HASH_LOOKUP_DETECT_NEW if you want to know
+ * whether the node was newly created or not.
* HASH_WANT_DELETE int delete(key) -- delete and deallocate node
* with given key. Returns success.
* HASH_WANT_REMOVE remove(node *) -- delete and deallocate given node.
* a node. Default is xmalloc() or pooled allocation, depending
* on HASH_USE_POOL, HASH_AUTO_POOL, HASH_USE_ELTPOOL
* and HASH_AUTO_ELTPOOL switches. void free(void *) -- the converse.
- * HASH_GIVE_TABLE_ALLOC void *table_alloc(unsigned int size), void *table_free(void *)
+ * HASH_GIVE_TABLE_ALLOC void *table_alloc(unsigned int size), void *table_free(void *)
* Allocate or free space for the table itself. Default is xmalloc()
* or the functions defined by HASH_GIVE_ALLOC if HASH_TABLE_ALLOC is set.
*
* HASH_TABLE_DYNAMIC Support multiple hash tables; the first parameter of all
* hash table operations is struct HASH_PREFIX(table) *.
* HASH_TABLE_VARS Extra variables to be defined in table structure
+ * HASH_LOOKUP_DETECT_NEW
+ * the prototype for lookup is changed to node *lookup(key, int *new_item)
+ * new_item must not be NULL and returns 1 whether lookup
+ * just created a new item in the hashtable or 0 otherwise.
*
* You also get a iterator macro at no extra charge:
*
#endif
#ifdef HASH_WANT_LOOKUP
+#ifdef HASH_LOOKUP_DETECT_NEW
/**
* Finds a node with a given key. If it does not exist, a new one is created.
* It is strongly recommended to use <<give_init_data,`HASH_GIVE_INIT_DATA`>>.
*
* This one is enabled by the <<want_lookup,`HASH_WANT_LOOKUP`>> macro.
+ * The @new_item argument is available only if <<lookup_detect_new,`HASH_LOOKUP_DETECT_NEW`>> was given.
**/
+static HASH_NODE* HASH_PREFIX(lookup)(TAC HASH_KEY_DECL, int *new_item)
+#else
static HASH_NODE* HASH_PREFIX(lookup)(TAC HASH_KEY_DECL)
+#endif
{
uns h0 = P(hash) (TTC HASH_KEY( ));
uns h = h0 % T.hash_size;
#ifndef HASH_CONSERVE_SPACE
b->hash == h0 &&
#endif
- P(eq)(TTC HASH_KEY( ), HASH_KEY(b->n.)))
+ P(eq)(TTC HASH_KEY( ), HASH_KEY(b->n.))) {
+#ifdef HASH_LOOKUP_DETECT_NEW
+ *new_item = 0;
+#endif
return &b->n;
+ }
}
b = P(new_bucket) (TTC sizeof(struct P(bucket)) + HASH_EXTRA_SIZE(HASH_KEY( )));
P(init_data)(TTC &b->n);
if (T.hash_count++ >= T.hash_max)
P(rehash)(TTC 2*T.hash_size);
+#ifdef HASH_LOOKUP_DETECT_NEW
+ *new_item = 1;
+#endif
return &b->n;
}
#endif
#undef HASH_TABLE_DYNAMIC
#undef HASH_TABLE_VARS
#undef HASH_ZERO_FILL
+#undef HASH_LOOKUP_DETECT_NEW