aboutsummaryrefslogtreecommitdiff
path: root/src/value.c
diff options
context:
space:
mode:
authorPetri Lehtinen <petri@digip.org>2012-01-24 20:36:31 +0200
committerPetri Lehtinen <petri@digip.org>2012-01-24 21:01:23 +0200
commita2381948bb7871aee7c55c6918953bfbdf9c3eb1 (patch)
tree3ad2da4700906f291d2412cdb0fe001e3620511d /src/value.c
parent5eb2c442a9f403f36d70b0c0e6efddf41adb056c (diff)
downloadjansson-a2381948bb7871aee7c55c6918953bfbdf9c3eb1.zip
jansson-a2381948bb7871aee7c55c6918953bfbdf9c3eb1.tar.gz
jansson-a2381948bb7871aee7c55c6918953bfbdf9c3eb1.tar.bz2
Make hashtable less generic
This will make it possible to implement json_object_foreach(). It should also have some (positive) effect on speed.
Diffstat (limited to 'src/value.c')
-rw-r--r--src/value.c86
1 files changed, 7 insertions, 79 deletions
diff --git a/src/value.c b/src/value.c
index e0e21cb..c728427 100644
--- a/src/value.c
+++ b/src/value.c
@@ -26,50 +26,6 @@ static JSON_INLINE void json_init(json_t *json, json_type type)
/*** object ***/
-/* From http://www.cse.yorku.ca/~oz/hash.html */
-size_t jsonp_hash_str(const void *ptr)
-{
- const char *str = (const char *)ptr;
-
- size_t hash = 5381;
- size_t c;
-
- while((c = (size_t)*str))
- {
- hash = ((hash << 5) + hash) + c;
- str++;
- }
-
- return hash;
-}
-
-int jsonp_str_equal(const void *ptr1, const void *ptr2)
-{
- return strcmp((const char *)ptr1, (const char *)ptr2) == 0;
-}
-
-/* This macro just returns a pointer that's a few bytes backwards from
- string. This makes it possible to pass a pointer to object_key_t
- when only the string inside it is used, without actually creating
- an object_key_t instance. */
-#define string_to_key(string) container_of(string, object_key_t, key)
-
-static size_t hash_key(const void *ptr)
-{
- return jsonp_hash_str(((const object_key_t *)ptr)->key);
-}
-
-static int key_equal(const void *ptr1, const void *ptr2)
-{
- return jsonp_str_equal(((const object_key_t *)ptr1)->key,
- ((const object_key_t *)ptr2)->key);
-}
-
-static void value_decref(void *value)
-{
- json_decref((json_t *)value);
-}
-
json_t *json_object(void)
{
json_object_t *object = jsonp_malloc(sizeof(json_object_t));
@@ -77,9 +33,7 @@ json_t *json_object(void)
return NULL;
json_init(&object->json, JSON_OBJECT);
- if(hashtable_init(&object->hashtable,
- hash_key, key_equal,
- jsonp_free, value_decref))
+ if(hashtable_init(&object->hashtable))
{
jsonp_free(object);
return NULL;
@@ -116,13 +70,12 @@ json_t *json_object_get(const json_t *json, const char *key)
return NULL;
object = json_to_object(json);
- return hashtable_get(&object->hashtable, string_to_key(key));
+ return hashtable_get(&object->hashtable, key);
}
int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
{
json_object_t *object;
- object_key_t *k;
if(!value)
return -1;
@@ -134,20 +87,7 @@ int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
}
object = json_to_object(json);
- /* offsetof(...) returns the size of object_key_t without the
- last, flexible member. This way, the correct amount is
- allocated. */
- k = jsonp_malloc(offsetof(object_key_t, key) + strlen(key) + 1);
- if(!k)
- {
- json_decref(value);
- return -1;
- }
-
- k->serial = object->serial++;
- strcpy(k->key, key);
-
- if(hashtable_set(&object->hashtable, k, value))
+ if(hashtable_set(&object->hashtable, key, object->serial++, value))
{
json_decref(value);
return -1;
@@ -175,7 +115,7 @@ int json_object_del(json_t *json, const char *key)
return -1;
object = json_to_object(json);
- return hashtable_del(&object->hashtable, string_to_key(key));
+ return hashtable_del(&object->hashtable, key);
}
int json_object_clear(json_t *json)
@@ -236,7 +176,7 @@ void *json_object_iter_at(json_t *json, const char *key)
return NULL;
object = json_to_object(json);
- return hashtable_iter_at(&object->hashtable, string_to_key(key));
+ return hashtable_iter_at(&object->hashtable, key);
}
void *json_object_iter_next(json_t *json, void *iter)
@@ -250,20 +190,12 @@ void *json_object_iter_next(json_t *json, void *iter)
return hashtable_iter_next(&object->hashtable, iter);
}
-const object_key_t *jsonp_object_iter_fullkey(void *iter)
-{
- if(!iter)
- return NULL;
-
- return hashtable_iter_key(iter);
-}
-
const char *json_object_iter_key(void *iter)
{
if(!iter)
return NULL;
- return jsonp_object_iter_fullkey(iter)->key;
+ return hashtable_iter_key(iter);
}
json_t *json_object_iter_value(void *iter)
@@ -276,14 +208,10 @@ json_t *json_object_iter_value(void *iter)
int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
{
- json_object_t *object;
-
if(!json_is_object(json) || !iter || !value)
return -1;
- object = json_to_object(json);
- hashtable_iter_set(&object->hashtable, iter, value);
-
+ hashtable_iter_set(iter, value);
return 0;
}