ircd: irc_dictionary: allow storage of non-string types as keys
This commit is contained in:
parent
d99ff0298c
commit
45dfdf46c8
2 changed files with 35 additions and 26 deletions
|
@ -27,13 +27,13 @@
|
||||||
|
|
||||||
struct Dictionary; /* defined in src/dictionary.c */
|
struct Dictionary; /* defined in src/dictionary.c */
|
||||||
|
|
||||||
typedef int (*DCF)(const char *a, const char *b);
|
typedef int (*DCF)(/* const void *a, const void *b */);
|
||||||
|
|
||||||
struct DictionaryElement
|
struct DictionaryElement
|
||||||
{
|
{
|
||||||
struct DictionaryElement *left, *right, *prev, *next;
|
struct DictionaryElement *left, *right, *prev, *next;
|
||||||
void *data;
|
void *data;
|
||||||
const char *key;
|
const void *key;
|
||||||
int position;
|
int position;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@ extern DCF irc_dictionary_get_comparator_func(struct Dictionary *dict);
|
||||||
* irc_dictionary_get_linear_index() returns the linear index of an object in the
|
* irc_dictionary_get_linear_index() returns the linear index of an object in the
|
||||||
* DTree structure.
|
* DTree structure.
|
||||||
*/
|
*/
|
||||||
extern int irc_dictionary_get_linear_index(struct Dictionary *dict, const char *key);
|
extern int irc_dictionary_get_linear_index(struct Dictionary *dict, const void *key);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* irc_dictionary_destroy() destroys all entries in a dtree, and also optionally calls
|
* irc_dictionary_destroy() destroys all entries in a dtree, and also optionally calls
|
||||||
|
@ -126,22 +126,22 @@ extern void irc_dictionary_foreach_next(struct Dictionary *dtree,
|
||||||
/*
|
/*
|
||||||
* irc_dictionary_add() adds a key->value entry to the dictionary tree.
|
* irc_dictionary_add() adds a key->value entry to the dictionary tree.
|
||||||
*/
|
*/
|
||||||
extern struct DictionaryElement *irc_dictionary_add(struct Dictionary *dtree, const char *key, void *data);
|
extern struct DictionaryElement *irc_dictionary_add(struct Dictionary *dtree, const void *key, void *data);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* irc_dictionary_find() returns a struct DictionaryElement container from a dtree for key 'key'.
|
* irc_dictionary_find() returns a struct DictionaryElement container from a dtree for key 'key'.
|
||||||
*/
|
*/
|
||||||
extern struct DictionaryElement *irc_dictionary_find(struct Dictionary *dtree, const char *key);
|
extern struct DictionaryElement *irc_dictionary_find(struct Dictionary *dtree, const void *key);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* irc_dictionary_find() returns data from a dtree for key 'key'.
|
* irc_dictionary_find() returns data from a dtree for key 'key'.
|
||||||
*/
|
*/
|
||||||
extern void *irc_dictionary_retrieve(struct Dictionary *dtree, const char *key);
|
extern void *irc_dictionary_retrieve(struct Dictionary *dtree, const void *key);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* irc_dictionary_delete() deletes a key->value entry from the dictionary tree.
|
* irc_dictionary_delete() deletes a key->value entry from the dictionary tree.
|
||||||
*/
|
*/
|
||||||
extern void *irc_dictionary_delete(struct Dictionary *dtree, const char *key);
|
extern void *irc_dictionary_delete(struct Dictionary *dtree, const void *key);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* irc_dictionary_size() returns the number of elements in a dictionary tree.
|
* irc_dictionary_size() returns the number of elements in a dictionary tree.
|
||||||
|
@ -151,4 +151,20 @@ extern unsigned int irc_dictionary_size(struct Dictionary *dtree);
|
||||||
void irc_dictionary_stats(struct Dictionary *dict, void (*cb)(const char *line, void *privdata), void *privdata);
|
void irc_dictionary_stats(struct Dictionary *dict, void (*cb)(const char *line, void *privdata), void *privdata);
|
||||||
void irc_dictionary_stats_walk(void (*cb)(const char *line, void *privdata), void *privdata);
|
void irc_dictionary_stats_walk(void (*cb)(const char *line, void *privdata), void *privdata);
|
||||||
|
|
||||||
|
#define IRC_POINTER_TO_INT(x) ((int32_t) (long) (x))
|
||||||
|
#define IRC_INT_TO_POINTER(x) ((void *) (long) (int32_t) (x))
|
||||||
|
|
||||||
|
#define IRC_POINTER_TO_UINT(x) ((uint32_t) (unsigned long) (x))
|
||||||
|
#define IRC_UINT_TO_POINTER(x) ((void *) (unsigned long) (uint32_t) (x))
|
||||||
|
|
||||||
|
static inline int irc_int32cmp(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
return IRC_POINTER_TO_INT(b) - IRC_POINTER_TO_INT(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int irc_uint32cmp(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
return IRC_POINTER_TO_UINT(b) - IRC_POINTER_TO_UINT(a);
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -122,7 +122,7 @@ irc_dictionary_get_comparator_func(struct Dictionary *dict)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* irc_dictionary_get_linear_index(struct Dictionary *dict,
|
* irc_dictionary_get_linear_index(struct Dictionary *dict,
|
||||||
* const char *key)
|
* const void *key)
|
||||||
*
|
*
|
||||||
* Gets a linear index number for key.
|
* Gets a linear index number for key.
|
||||||
*
|
*
|
||||||
|
@ -137,7 +137,7 @@ irc_dictionary_get_comparator_func(struct Dictionary *dict)
|
||||||
* - rebuilds the linear index if the tree is marked as dirty.
|
* - rebuilds the linear index if the tree is marked as dirty.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
irc_dictionary_get_linear_index(struct Dictionary *dict, const char *key)
|
irc_dictionary_get_linear_index(struct Dictionary *dict, const void *key)
|
||||||
{
|
{
|
||||||
struct DictionaryElement *elem;
|
struct DictionaryElement *elem;
|
||||||
|
|
||||||
|
@ -165,7 +165,7 @@ irc_dictionary_get_linear_index(struct Dictionary *dict, const char *key)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* irc_dictionary_retune(struct Dictionary *dict, const char *key)
|
* irc_dictionary_retune(struct Dictionary *dict, const void *key)
|
||||||
*
|
*
|
||||||
* Retunes the tree, self-optimizing for the element which belongs to key.
|
* Retunes the tree, self-optimizing for the element which belongs to key.
|
||||||
*
|
*
|
||||||
|
@ -179,7 +179,7 @@ irc_dictionary_get_linear_index(struct Dictionary *dict, const char *key)
|
||||||
* - a new root node is nominated.
|
* - a new root node is nominated.
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
irc_dictionary_retune(struct Dictionary *dict, const char *key)
|
irc_dictionary_retune(struct Dictionary *dict, const void *key)
|
||||||
{
|
{
|
||||||
struct DictionaryElement n, *tn, *left, *right, *node;
|
struct DictionaryElement n, *tn, *left, *right, *node;
|
||||||
int ret;
|
int ret;
|
||||||
|
@ -634,7 +634,7 @@ void irc_dictionary_foreach_next(struct Dictionary *dtree,
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* irc_dictionary_find(struct Dictionary *dtree, const char *key)
|
* irc_dictionary_find(struct Dictionary *dtree, const void *key)
|
||||||
*
|
*
|
||||||
* Looks up a DTree node by name.
|
* Looks up a DTree node by name.
|
||||||
*
|
*
|
||||||
|
@ -649,7 +649,7 @@ void irc_dictionary_foreach_next(struct Dictionary *dtree,
|
||||||
* Side Effects:
|
* Side Effects:
|
||||||
* - none
|
* - none
|
||||||
*/
|
*/
|
||||||
struct DictionaryElement *irc_dictionary_find(struct Dictionary *dict, const char *key)
|
struct DictionaryElement *irc_dictionary_find(struct Dictionary *dict, const void *key)
|
||||||
{
|
{
|
||||||
s_assert(dict != NULL);
|
s_assert(dict != NULL);
|
||||||
s_assert(key != NULL);
|
s_assert(key != NULL);
|
||||||
|
@ -664,7 +664,7 @@ struct DictionaryElement *irc_dictionary_find(struct Dictionary *dict, const cha
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* irc_dictionary_add(struct Dictionary *dtree, const char *key, void *data)
|
* irc_dictionary_add(struct Dictionary *dtree, const void *key, void *data)
|
||||||
*
|
*
|
||||||
* Creates a new DTree node and binds data to it.
|
* Creates a new DTree node and binds data to it.
|
||||||
*
|
*
|
||||||
|
@ -680,7 +680,7 @@ struct DictionaryElement *irc_dictionary_find(struct Dictionary *dict, const cha
|
||||||
* Side Effects:
|
* Side Effects:
|
||||||
* - data is inserted into the DTree.
|
* - data is inserted into the DTree.
|
||||||
*/
|
*/
|
||||||
struct DictionaryElement *irc_dictionary_add(struct Dictionary *dict, const char *key, void *data)
|
struct DictionaryElement *irc_dictionary_add(struct Dictionary *dict, const void *key, void *data)
|
||||||
{
|
{
|
||||||
struct DictionaryElement *delem;
|
struct DictionaryElement *delem;
|
||||||
|
|
||||||
|
@ -693,20 +693,13 @@ struct DictionaryElement *irc_dictionary_add(struct Dictionary *dict, const char
|
||||||
delem->key = key;
|
delem->key = key;
|
||||||
delem->data = data;
|
delem->data = data;
|
||||||
|
|
||||||
/* TBD: is this needed? --nenolod */
|
|
||||||
if (delem->key == NULL)
|
|
||||||
{
|
|
||||||
rb_free(delem);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
irc_dictionary_link(dict, delem);
|
irc_dictionary_link(dict, delem);
|
||||||
|
|
||||||
return delem;
|
return delem;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* irc_dictionary_delete(struct Dictionary *dtree, const char *key)
|
* irc_dictionary_delete(struct Dictionary *dtree, const void *key)
|
||||||
*
|
*
|
||||||
* Deletes data from a dictionary tree.
|
* Deletes data from a dictionary tree.
|
||||||
*
|
*
|
||||||
|
@ -724,7 +717,7 @@ struct DictionaryElement *irc_dictionary_add(struct Dictionary *dict, const char
|
||||||
* Notes:
|
* Notes:
|
||||||
* - the returned data needs to be mowgli_freed/released manually!
|
* - the returned data needs to be mowgli_freed/released manually!
|
||||||
*/
|
*/
|
||||||
void *irc_dictionary_delete(struct Dictionary *dtree, const char *key)
|
void *irc_dictionary_delete(struct Dictionary *dtree, const void *key)
|
||||||
{
|
{
|
||||||
struct DictionaryElement *delem = irc_dictionary_find(dtree, key);
|
struct DictionaryElement *delem = irc_dictionary_find(dtree, key);
|
||||||
void *data;
|
void *data;
|
||||||
|
@ -741,7 +734,7 @@ void *irc_dictionary_delete(struct Dictionary *dtree, const char *key)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* irc_dictionary_retrieve(struct Dictionary *dtree, const char *key)
|
* irc_dictionary_retrieve(struct Dictionary *dtree, const void *key)
|
||||||
*
|
*
|
||||||
* Retrieves data from a dictionary.
|
* Retrieves data from a dictionary.
|
||||||
*
|
*
|
||||||
|
@ -756,7 +749,7 @@ void *irc_dictionary_delete(struct Dictionary *dtree, const char *key)
|
||||||
* Side Effects:
|
* Side Effects:
|
||||||
* - none
|
* - none
|
||||||
*/
|
*/
|
||||||
void *irc_dictionary_retrieve(struct Dictionary *dtree, const char *key)
|
void *irc_dictionary_retrieve(struct Dictionary *dtree, const void *key)
|
||||||
{
|
{
|
||||||
struct DictionaryElement *delem = irc_dictionary_find(dtree, key);
|
struct DictionaryElement *delem = irc_dictionary_find(dtree, key);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue