Skip to content

Commit

Permalink
GHashTables, oh boy oh boy
Browse files Browse the repository at this point in the history
  • Loading branch information
eklitzke committed Apr 10, 2009
0 parents commit dd7e2d8
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
5 changes: 5 additions & 0 deletions LICENSE.md
@@ -0,0 +1,5 @@
This code is copyright Evan Klitkze, 2009 <evan@eklitzke.org>

This code is currently licensed under the terms of the GPL, version 3. You can
find the full text of that license online at
[gnu.org](http://www.gnu.org/licenses/gpl-3.0.txt).
2 changes: 2 additions & 0 deletions Makefile
@@ -0,0 +1,2 @@
test_counter: test_counter.c counter.c
gcc -o test_counter test_counter.c $(pkg-config glib-2.0 --cflags --libs)
3 changes: 3 additions & 0 deletions README.md
@@ -0,0 +1,3 @@
Scribble
========
Collect status, build high performance components. 'Nuff said.
70 changes: 70 additions & 0 deletions counter.c
@@ -0,0 +1,70 @@
/* This uses hash tables all over the place, which isn't optimal.
*/

#include <glib.h>

/* Create a new counter. This is really just a hash table. */
GHashTable* new_counter()
{
GHashTable *ht;
ht = g_hash_table_new(g_str_hash, g_str_equal);
return ht;
}

/* This increments a key in for ht[major][minor] */
void incr_counter(GHashTable *ht, char *major, char *minor)
{
GHashTable *sub;
gchar *major_cpy, *minor_cpy;
guint *val;

sub = g_hash_table_lookup(ht, major);
if (sub == NULL) {
major_cpy = g_strdup(major);
sub = g_hash_table_new(g_str_hash, g_str_equal);
g_hash_table_insert(ht, major_cpy, sub);
}

val = g_hash_table_lookup(sub, minor);
if (val == NULL) {
minor_cpy = g_strdup(minor);
val = g_slice_alloc0(sizeof(guint));
g_hash_table_insert(sub, minor_cpy, val);
}
(*val)++;
}

void free_counter(GHashTable *ht)
{
GHashTableIter iter_i, iter_j;
gpointer key_i, value_i, key_j, value_j;

g_hash_table_iter_init(&iter_i, ht);
while (g_hash_table_iter_next(&iter_i, &key_i, &value_i)) {
g_hash_table_iter_remove(&iter_i);
g_free(key_i);

g_hash_table_iter_init(&iter_j, value_i);
while (g_hash_table_iter_next(&iter_j, &key_j, &value_j)) {
g_hash_table_iter_remove(&iter_j);
g_free(key_j);
g_slice_free(guint, value_j);
}
g_hash_table_destroy(value_i);
}
}

guint lookup_counter(GHashTable *ht, char *major, char *minor)
{
GHashTable *sub;
guint *val;

sub = g_hash_table_lookup(ht, major);
if (sub == NULL)
return 0;

val = g_hash_table_lookup(sub, minor);
if (val == NULL)
return 0;
return *val;
}
16 changes: 16 additions & 0 deletions test_counter.c
@@ -0,0 +1,16 @@
#include <glib.h>
#include "counter.c"
#include <stdio.h>

int main()
{
GHashTable *ht = new_counter();
incr_counter(ht, "foo", "bar");
incr_counter(ht, "foo", "baz");
incr_counter(ht, "foo", "bar");
printf("foo.bar = %d\n", lookup_counter(ht, "foo", "bar"));
printf("foo.baz = %d\n", lookup_counter(ht, "foo", "baz"));
printf("foo.bad = %d\n", lookup_counter(ht, "foo", "bad"));
free_counter(ht);
return 0;
}

0 comments on commit dd7e2d8

Please sign in to comment.