Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit dd7e2d8
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Scribble | ||
======== | ||
Collect status, build high performance components. 'Nuff said. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |