From 00d4f92b717fbcbed6f9eee361975d6ee5380d59 Mon Sep 17 00:00:00 2001 From: Michele Calgaro Date: Sun, 6 Dec 2020 19:28:06 +0900 Subject: Renaming of files in preparation for code style tools. Signed-off-by: Michele Calgaro --- flow/gsl/CMakeLists.txt | 2 +- flow/gsl/dummy.cc | 3 - flow/gsl/dummy.cpp | 3 + flow/gsl/gslartsthreads.cc | 203 ------------------------------------------- flow/gsl/gslartsthreads.cpp | 203 +++++++++++++++++++++++++++++++++++++++++++ flow/gsl/gslglibhash.cc | 149 ------------------------------- flow/gsl/gslglibhash.cpp | 149 +++++++++++++++++++++++++++++++ flow/gsl/gslglibhashtest.cc | 123 -------------------------- flow/gsl/gslglibhashtest.cpp | 123 ++++++++++++++++++++++++++ 9 files changed, 479 insertions(+), 479 deletions(-) delete mode 100644 flow/gsl/dummy.cc create mode 100644 flow/gsl/dummy.cpp delete mode 100644 flow/gsl/gslartsthreads.cc create mode 100644 flow/gsl/gslartsthreads.cpp delete mode 100644 flow/gsl/gslglibhash.cc create mode 100644 flow/gsl/gslglibhash.cpp delete mode 100644 flow/gsl/gslglibhashtest.cc create mode 100644 flow/gsl/gslglibhashtest.cpp (limited to 'flow/gsl') diff --git a/flow/gsl/CMakeLists.txt b/flow/gsl/CMakeLists.txt index 877b77c..e8e9576 100644 --- a/flow/gsl/CMakeLists.txt +++ b/flow/gsl/CMakeLists.txt @@ -144,7 +144,7 @@ set( ${target}_SRCS gslconvert.c gslfft.c gslsignal.c gslloader.c gslwaveosc.c gslengine.c gsloputil.c gslopmaster.c gslopschedule.c gsldatahandle-mad.c gslosctable.c - gsloscillator.c gslfilehash.c gslartsthreads.cc + gsloscillator.c gslfilehash.c gslartsthreads.cpp ) tde_add_library( ${target} STATIC_PIC diff --git a/flow/gsl/dummy.cc b/flow/gsl/dummy.cc deleted file mode 100644 index 9fde279..0000000 --- a/flow/gsl/dummy.cc +++ /dev/null @@ -1,3 +0,0 @@ -/* this is a C++ dummy source to force c++ linkage of the examples */ - -namespace Arts { int arts_flow_gsl_dummy = 0; } diff --git a/flow/gsl/dummy.cpp b/flow/gsl/dummy.cpp new file mode 100644 index 0000000..9fde279 --- /dev/null +++ b/flow/gsl/dummy.cpp @@ -0,0 +1,3 @@ +/* this is a C++ dummy source to force c++ linkage of the examples */ + +namespace Arts { int arts_flow_gsl_dummy = 0; } diff --git a/flow/gsl/gslartsthreads.cc b/flow/gsl/gslartsthreads.cc deleted file mode 100644 index 9bd89e5..0000000 --- a/flow/gsl/gslartsthreads.cc +++ /dev/null @@ -1,203 +0,0 @@ - /* - - Copyright (C) 2001-2002 Stefan Westerfeld - stefan@space.twc.de - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. - - */ - -#include "thread.h" -#include "debug.h" -#include "gslglib.h" -#include - -#if defined(__DECCXX) -#define EXTC extern "C" -#else -#define EXTC -#endif - -EXTC gpointer -gsl_arts_mutex_new () -{ - return new Arts::Mutex; -} - -EXTC void -gsl_arts_mutex_free (gpointer mutex) -{ - Arts::Mutex *m = static_cast(mutex); - delete m; -} - -EXTC void -gsl_arts_mutex_lock (gpointer mutex) -{ - Arts::Mutex *m = static_cast(mutex); - m->lock(); -} - -EXTC gboolean -gsl_arts_mutex_trylock (gpointer mutex) -{ - Arts::Mutex *m = static_cast(mutex); - return m->tryLock(); -} - -EXTC void -gsl_arts_mutex_unlock (gpointer mutex) -{ - Arts::Mutex *m = static_cast(mutex); - m->unlock(); -} - -EXTC gpointer -gsl_arts_cond_new () -{ - return new Arts::ThreadCondition; -} - -EXTC void -gsl_arts_cond_free (gpointer cond) -{ - Arts::ThreadCondition *c = static_cast(cond); - delete c; -} - -EXTC void -gsl_arts_cond_signal (gpointer cond) -{ - Arts::ThreadCondition *c = static_cast(cond); - c->wakeOne(); -} - -EXTC void -gsl_arts_cond_broadcast (gpointer cond) -{ - Arts::ThreadCondition *c = static_cast(cond); - c->wakeAll(); -} - -EXTC void -gsl_arts_cond_wait (gpointer cond, gpointer mutex) -{ - Arts::ThreadCondition *c = static_cast(cond); - Arts::Mutex *m = static_cast(mutex); - - c->wait(*m); -} - -EXTC void -gsl_arts_cond_timed_wait (gpointer /*cond*/, gpointer /*mutex*/, GTimeVal * /*time*/) -{ - arts_assert(false); -} - - -class GslArtsThread : public Arts::Thread { -protected: - gpointer (*func)(gpointer data); - gpointer data; - gpointer result; - -public: - GThread gthread; - - GslArtsThread(gpointer (*func)(gpointer data), gpointer data) - : func(func), data(data) - { - gthread.data = 0; - } - void run() - { - result = func(data); - } -}; - - -/* KCC (KAI C++) is buggy. If we write out the type of the first argument - to gsl_arts_thread_create(), ala - gsl_arts_thread_create (gpointer (*func) (gpointer data2), ...) - it becomes C++ linkage, i.e. it's name gets mangled, _despite_ declared - extern "C" in the header. Other sources only calling this function, - i.e. those only seeing the prototype correctly call the unmangled - extern "C" variant, but for the above reason it isn't defined anywhere. - The solution is to go through a typedef for that argument, _which must - also be declared extern "C"_. I'm not sure, but I think it's an error - of KCC, that it puts the invisible type of the first arg into C++ mode, - although the whole function should be C only. If one declares - two equal function types, one extern "C", one not, they are even assignment - compatible. But somehow KCC puts that type into C++ mode, which for - other strange reasons force the whole function to go into C++ linkage. - It's enough, when this typedef is local to this file. (matz) */ - -/* Due to gcc's unhappyness with 'extern "C" typedef ...' we enclose - it in a real extern "C" {} block. */ -extern "C" { -typedef gpointer (*t_func)(gpointer data2); -} - -// This is C++: -GThread* gsl_arts_thread_create_full_CPP(gpointer (*func)(gpointer data), - gpointer data, - gulong /*stack_size*/, - gboolean /*joinable*/, - gboolean /*bound*/, - GThreadPriority /*priority*/, - GError ** /*error*/) -{ - GslArtsThread *thread = new GslArtsThread(func, data); - return &thread->gthread; -} - -// This is C: -extern "C" GThread* -gsl_arts_thread_create_full(gpointer (*func)(gpointer data), - gpointer data, - gulong stack_size, - gboolean joinable, - gboolean bound, - GThreadPriority priority, - GError **error) -{ - return gsl_arts_thread_create_full_CPP(func, - data, - stack_size, - joinable, - bound, - priority, - error); -} - -EXTC gpointer -gsl_arts_thread_self () -{ - GslArtsThread *current = static_cast(Arts::SystemThreads::the()->getCurrentThread()); - - if(current) - return ¤t->gthread; - else - { - static GThread mainThread = { 0, }; - return &mainThread; - } -} - -EXTC void -gsl_arts_thread_init (gpointer /*arg*/) -{ -} diff --git a/flow/gsl/gslartsthreads.cpp b/flow/gsl/gslartsthreads.cpp new file mode 100644 index 0000000..9bd89e5 --- /dev/null +++ b/flow/gsl/gslartsthreads.cpp @@ -0,0 +1,203 @@ + /* + + Copyright (C) 2001-2002 Stefan Westerfeld + stefan@space.twc.de + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. + + */ + +#include "thread.h" +#include "debug.h" +#include "gslglib.h" +#include + +#if defined(__DECCXX) +#define EXTC extern "C" +#else +#define EXTC +#endif + +EXTC gpointer +gsl_arts_mutex_new () +{ + return new Arts::Mutex; +} + +EXTC void +gsl_arts_mutex_free (gpointer mutex) +{ + Arts::Mutex *m = static_cast(mutex); + delete m; +} + +EXTC void +gsl_arts_mutex_lock (gpointer mutex) +{ + Arts::Mutex *m = static_cast(mutex); + m->lock(); +} + +EXTC gboolean +gsl_arts_mutex_trylock (gpointer mutex) +{ + Arts::Mutex *m = static_cast(mutex); + return m->tryLock(); +} + +EXTC void +gsl_arts_mutex_unlock (gpointer mutex) +{ + Arts::Mutex *m = static_cast(mutex); + m->unlock(); +} + +EXTC gpointer +gsl_arts_cond_new () +{ + return new Arts::ThreadCondition; +} + +EXTC void +gsl_arts_cond_free (gpointer cond) +{ + Arts::ThreadCondition *c = static_cast(cond); + delete c; +} + +EXTC void +gsl_arts_cond_signal (gpointer cond) +{ + Arts::ThreadCondition *c = static_cast(cond); + c->wakeOne(); +} + +EXTC void +gsl_arts_cond_broadcast (gpointer cond) +{ + Arts::ThreadCondition *c = static_cast(cond); + c->wakeAll(); +} + +EXTC void +gsl_arts_cond_wait (gpointer cond, gpointer mutex) +{ + Arts::ThreadCondition *c = static_cast(cond); + Arts::Mutex *m = static_cast(mutex); + + c->wait(*m); +} + +EXTC void +gsl_arts_cond_timed_wait (gpointer /*cond*/, gpointer /*mutex*/, GTimeVal * /*time*/) +{ + arts_assert(false); +} + + +class GslArtsThread : public Arts::Thread { +protected: + gpointer (*func)(gpointer data); + gpointer data; + gpointer result; + +public: + GThread gthread; + + GslArtsThread(gpointer (*func)(gpointer data), gpointer data) + : func(func), data(data) + { + gthread.data = 0; + } + void run() + { + result = func(data); + } +}; + + +/* KCC (KAI C++) is buggy. If we write out the type of the first argument + to gsl_arts_thread_create(), ala + gsl_arts_thread_create (gpointer (*func) (gpointer data2), ...) + it becomes C++ linkage, i.e. it's name gets mangled, _despite_ declared + extern "C" in the header. Other sources only calling this function, + i.e. those only seeing the prototype correctly call the unmangled + extern "C" variant, but for the above reason it isn't defined anywhere. + The solution is to go through a typedef for that argument, _which must + also be declared extern "C"_. I'm not sure, but I think it's an error + of KCC, that it puts the invisible type of the first arg into C++ mode, + although the whole function should be C only. If one declares + two equal function types, one extern "C", one not, they are even assignment + compatible. But somehow KCC puts that type into C++ mode, which for + other strange reasons force the whole function to go into C++ linkage. + It's enough, when this typedef is local to this file. (matz) */ + +/* Due to gcc's unhappyness with 'extern "C" typedef ...' we enclose + it in a real extern "C" {} block. */ +extern "C" { +typedef gpointer (*t_func)(gpointer data2); +} + +// This is C++: +GThread* gsl_arts_thread_create_full_CPP(gpointer (*func)(gpointer data), + gpointer data, + gulong /*stack_size*/, + gboolean /*joinable*/, + gboolean /*bound*/, + GThreadPriority /*priority*/, + GError ** /*error*/) +{ + GslArtsThread *thread = new GslArtsThread(func, data); + return &thread->gthread; +} + +// This is C: +extern "C" GThread* +gsl_arts_thread_create_full(gpointer (*func)(gpointer data), + gpointer data, + gulong stack_size, + gboolean joinable, + gboolean bound, + GThreadPriority priority, + GError **error) +{ + return gsl_arts_thread_create_full_CPP(func, + data, + stack_size, + joinable, + bound, + priority, + error); +} + +EXTC gpointer +gsl_arts_thread_self () +{ + GslArtsThread *current = static_cast(Arts::SystemThreads::the()->getCurrentThread()); + + if(current) + return ¤t->gthread; + else + { + static GThread mainThread = { 0, }; + return &mainThread; + } +} + +EXTC void +gsl_arts_thread_init (gpointer /*arg*/) +{ +} diff --git a/flow/gsl/gslglibhash.cc b/flow/gsl/gslglibhash.cc deleted file mode 100644 index c933929..0000000 --- a/flow/gsl/gslglibhash.cc +++ /dev/null @@ -1,149 +0,0 @@ -/* GSL Glib Hashtable implementation - * Copyright (C) 2001 Stefan Westerfeld - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General - * Public License along with this library; if not, write to the - * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - */ -#include "gslglib.h" -#include -#include - -using std::list; -using std::pair; -using std::map; - -/* - * this uses a map of list to emulate somewhat hashtable like behaviour - note - * that insert and remove are O(log N) due to the use of a map, instead of - * ~ O(1) which they would be for a "real" hashtable - */ -struct _GHashTable -{ - GHashFunc hashFunc; - GEqualFunc equalFunc; - map > /*(key,value) pairs*/> nodes; - - /*Con*/ _GHashTable (GHashFunc hf, GEqualFunc ef) : - hashFunc(hf), equalFunc(ef) { } -}; - - -GHashTable* g_hash_table_new (GHashFunc hash_func, - GEqualFunc key_equal_func) -{ - return new GHashTable (hash_func?hash_func:g_direct_hash, key_equal_func?key_equal_func:g_direct_equal); -} -void g_hash_table_destroy (GHashTable *hash_table) -{ - g_return_if_fail (hash_table != NULL); - - delete hash_table; -} -void g_hash_table_insert (GHashTable *hash_table, - gpointer key, - gpointer value) -{ - g_return_if_fail (hash_table != NULL); - guint hashvalue = hash_table->hashFunc (key); - - list< pair >& bucket = hash_table->nodes[hashvalue]; - list< pair >::iterator i; - - for (i = bucket.begin(); i != bucket.end(); i++) - { - if (hash_table->equalFunc(i->first, key)) - { - if (value || TRUE) - { - i->second = value; /* overwrite old hash value */ - return; - } - else - { - bucket.erase(i); /* remove value */ - - if (bucket.empty()) /* remove bucket if this was the only value */ - hash_table->nodes.erase (hashvalue); - return; - } - } - } - - if (value) - hash_table->nodes[hashvalue].push_back(std::make_pair (key, value)); -} - -gpointer g_hash_table_lookup (GHashTable *hash_table, - gconstpointer key) -{ - g_return_val_if_fail (hash_table != NULL, NULL); - - guint hashvalue = hash_table->hashFunc (key); - - list< pair >& bucket = hash_table->nodes[hashvalue]; - list< pair >::iterator i; - - for (i = bucket.begin(); i != bucket.end(); i++) - { - if (hash_table->equalFunc(i->first, key)) - return i->second; - } - - return 0; -} -gboolean g_hash_table_remove (GHashTable *hash_table, - gconstpointer key) -{ - g_return_val_if_fail (hash_table != NULL, FALSE); - - guint hashvalue = hash_table->hashFunc (key); - - list< pair >& bucket = hash_table->nodes[hashvalue]; - list< pair >::iterator i; - - for (i = bucket.begin(); i != bucket.end(); i++) - { - if (hash_table->equalFunc(i->first, key)) - { - bucket.erase (i); - - if (bucket.empty()) /* remove bucket if this was the only value */ - hash_table->nodes.erase (hashvalue); - return true; - } - } - - return false; -} - -void g_hash_table_foreach (GHashTable *hash_table, - GHFunc func, - gpointer user_data) -{ - map > >::iterator bi; - - g_return_if_fail (hash_table != NULL); - - /* for all buckets */ - for (bi = hash_table->nodes.begin (); bi != hash_table->nodes.end (); bi++) - { - list< pair >& bucket = bi->second; - list< pair >::iterator i; - - /* for each element in the current bucket */ - for (i = bucket.begin(); i != bucket.end(); i++) - func ((void*) i->first, (void*) i->second, user_data); - } -} diff --git a/flow/gsl/gslglibhash.cpp b/flow/gsl/gslglibhash.cpp new file mode 100644 index 0000000..c933929 --- /dev/null +++ b/flow/gsl/gslglibhash.cpp @@ -0,0 +1,149 @@ +/* GSL Glib Hashtable implementation + * Copyright (C) 2001 Stefan Westerfeld + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ +#include "gslglib.h" +#include +#include + +using std::list; +using std::pair; +using std::map; + +/* + * this uses a map of list to emulate somewhat hashtable like behaviour - note + * that insert and remove are O(log N) due to the use of a map, instead of + * ~ O(1) which they would be for a "real" hashtable + */ +struct _GHashTable +{ + GHashFunc hashFunc; + GEqualFunc equalFunc; + map > /*(key,value) pairs*/> nodes; + + /*Con*/ _GHashTable (GHashFunc hf, GEqualFunc ef) : + hashFunc(hf), equalFunc(ef) { } +}; + + +GHashTable* g_hash_table_new (GHashFunc hash_func, + GEqualFunc key_equal_func) +{ + return new GHashTable (hash_func?hash_func:g_direct_hash, key_equal_func?key_equal_func:g_direct_equal); +} +void g_hash_table_destroy (GHashTable *hash_table) +{ + g_return_if_fail (hash_table != NULL); + + delete hash_table; +} +void g_hash_table_insert (GHashTable *hash_table, + gpointer key, + gpointer value) +{ + g_return_if_fail (hash_table != NULL); + guint hashvalue = hash_table->hashFunc (key); + + list< pair >& bucket = hash_table->nodes[hashvalue]; + list< pair >::iterator i; + + for (i = bucket.begin(); i != bucket.end(); i++) + { + if (hash_table->equalFunc(i->first, key)) + { + if (value || TRUE) + { + i->second = value; /* overwrite old hash value */ + return; + } + else + { + bucket.erase(i); /* remove value */ + + if (bucket.empty()) /* remove bucket if this was the only value */ + hash_table->nodes.erase (hashvalue); + return; + } + } + } + + if (value) + hash_table->nodes[hashvalue].push_back(std::make_pair (key, value)); +} + +gpointer g_hash_table_lookup (GHashTable *hash_table, + gconstpointer key) +{ + g_return_val_if_fail (hash_table != NULL, NULL); + + guint hashvalue = hash_table->hashFunc (key); + + list< pair >& bucket = hash_table->nodes[hashvalue]; + list< pair >::iterator i; + + for (i = bucket.begin(); i != bucket.end(); i++) + { + if (hash_table->equalFunc(i->first, key)) + return i->second; + } + + return 0; +} +gboolean g_hash_table_remove (GHashTable *hash_table, + gconstpointer key) +{ + g_return_val_if_fail (hash_table != NULL, FALSE); + + guint hashvalue = hash_table->hashFunc (key); + + list< pair >& bucket = hash_table->nodes[hashvalue]; + list< pair >::iterator i; + + for (i = bucket.begin(); i != bucket.end(); i++) + { + if (hash_table->equalFunc(i->first, key)) + { + bucket.erase (i); + + if (bucket.empty()) /* remove bucket if this was the only value */ + hash_table->nodes.erase (hashvalue); + return true; + } + } + + return false; +} + +void g_hash_table_foreach (GHashTable *hash_table, + GHFunc func, + gpointer user_data) +{ + map > >::iterator bi; + + g_return_if_fail (hash_table != NULL); + + /* for all buckets */ + for (bi = hash_table->nodes.begin (); bi != hash_table->nodes.end (); bi++) + { + list< pair >& bucket = bi->second; + list< pair >::iterator i; + + /* for each element in the current bucket */ + for (i = bucket.begin(); i != bucket.end(); i++) + func ((void*) i->first, (void*) i->second, user_data); + } +} diff --git a/flow/gsl/gslglibhashtest.cc b/flow/gsl/gslglibhashtest.cc deleted file mode 100644 index 9205c8c..0000000 --- a/flow/gsl/gslglibhashtest.cc +++ /dev/null @@ -1,123 +0,0 @@ -/* GSL Glib Hashtable test - * Copyright (C) 2001 Stefan Westerfeld - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General - * Public License along with this library; if not, write to the - * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - */ -#include "gsldefs.h" -#include - -/* cause hashvalue collisions for the test */ -guint g_str_narrow_hash (gconstpointer key) { - return g_str_hash (key) & 15; -} - -struct Test { - char *key; - char *value; - int count; -} test[50]; - -void print_func (gpointer key, gpointer value, gpointer user_data) { -#if 0 /* <- enable to get printing of some entries */ - g_print ("%s: %s = %s\n", user_data, key, value); -#endif -} - -void fail_func (gpointer key, gpointer value, gpointer user_data) { - g_error ("*this* should not have happened"); -} - - -void count_func (gpointer key, gpointer value, gpointer user_data) { - g_assert (value != NULL && key != NULL); - for(int i=0;i<50;i++) - { - if(strcmp((char *)key, test[i].key) == 0) - { - g_assert(strcmp((char *)value, test[i].value) == 0); - test[i].count++; - } - } -} - - -int main() -{ - GHashTable *t = g_hash_table_new (g_str_narrow_hash, g_str_equal); - - for(int i=0;i<50;i++) - { - test[i].key = g_strdup_printf ("key-%d",i); - test[i].value = g_strdup_printf ("value-%d",i); - test[i].count = 0; - - g_hash_table_insert (t, test[i].key, test[i].value); - } - - g_assert (strcmp ((char *)g_hash_table_lookup (t, "key-24"), "value-24") == 0); - g_hash_table_foreach(t, print_func, g_strdup("all-keys-50")); - g_hash_table_foreach(t, count_func, 0); - - for(int i=0;i<50;i++) - { - /* each key should have been found once in the hash table now */ - g_assert(test[i].count == 1); - } - - for(int i=0;i<25;i++) - { - test[i].key = g_strdup_printf ("key-%d",i); - test[i].value = g_strdup_printf ("another-value-%d",i); - - g_hash_table_insert (t, test[i].key, test[i].value); - } - - g_assert (strcmp ((char *)g_hash_table_lookup (t, "key-24"), "another-value-24") == 0); - g_hash_table_foreach(t, print_func, g_strdup("all-keys-new-25-old-25")); - g_hash_table_foreach(t, count_func, 0); - - for(int i=0;i<50;i++) - { - /* each key should have been found once (with the new value) */ - g_assert(test[i].count == 2); - } - - for(int i=0;i<50;i+=2) - { - /* remove even valued keys */ - g_hash_table_remove (t, test[i].key); - } - - g_assert (g_hash_table_lookup (t, "key-24") == 0); - g_hash_table_foreach(t, print_func, g_strdup("only-odd-keys-25")); - g_hash_table_foreach(t, count_func, 0); - - for(int i=0;i<50;i++) - { - /* only odd keys should be there */ - g_assert((test[i].count == 3 && (i & 1)) - || (test[i].count == 2 && !(i & 1))); - } - - for(int i=1;i<50;i+=2) - { - /* remove odd valued keys */ - g_hash_table_remove (t, g_strdup(test[i].key)); - } - - g_hash_table_foreach(t, fail_func, 0); - return 0; -} diff --git a/flow/gsl/gslglibhashtest.cpp b/flow/gsl/gslglibhashtest.cpp new file mode 100644 index 0000000..9205c8c --- /dev/null +++ b/flow/gsl/gslglibhashtest.cpp @@ -0,0 +1,123 @@ +/* GSL Glib Hashtable test + * Copyright (C) 2001 Stefan Westerfeld + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ +#include "gsldefs.h" +#include + +/* cause hashvalue collisions for the test */ +guint g_str_narrow_hash (gconstpointer key) { + return g_str_hash (key) & 15; +} + +struct Test { + char *key; + char *value; + int count; +} test[50]; + +void print_func (gpointer key, gpointer value, gpointer user_data) { +#if 0 /* <- enable to get printing of some entries */ + g_print ("%s: %s = %s\n", user_data, key, value); +#endif +} + +void fail_func (gpointer key, gpointer value, gpointer user_data) { + g_error ("*this* should not have happened"); +} + + +void count_func (gpointer key, gpointer value, gpointer user_data) { + g_assert (value != NULL && key != NULL); + for(int i=0;i<50;i++) + { + if(strcmp((char *)key, test[i].key) == 0) + { + g_assert(strcmp((char *)value, test[i].value) == 0); + test[i].count++; + } + } +} + + +int main() +{ + GHashTable *t = g_hash_table_new (g_str_narrow_hash, g_str_equal); + + for(int i=0;i<50;i++) + { + test[i].key = g_strdup_printf ("key-%d",i); + test[i].value = g_strdup_printf ("value-%d",i); + test[i].count = 0; + + g_hash_table_insert (t, test[i].key, test[i].value); + } + + g_assert (strcmp ((char *)g_hash_table_lookup (t, "key-24"), "value-24") == 0); + g_hash_table_foreach(t, print_func, g_strdup("all-keys-50")); + g_hash_table_foreach(t, count_func, 0); + + for(int i=0;i<50;i++) + { + /* each key should have been found once in the hash table now */ + g_assert(test[i].count == 1); + } + + for(int i=0;i<25;i++) + { + test[i].key = g_strdup_printf ("key-%d",i); + test[i].value = g_strdup_printf ("another-value-%d",i); + + g_hash_table_insert (t, test[i].key, test[i].value); + } + + g_assert (strcmp ((char *)g_hash_table_lookup (t, "key-24"), "another-value-24") == 0); + g_hash_table_foreach(t, print_func, g_strdup("all-keys-new-25-old-25")); + g_hash_table_foreach(t, count_func, 0); + + for(int i=0;i<50;i++) + { + /* each key should have been found once (with the new value) */ + g_assert(test[i].count == 2); + } + + for(int i=0;i<50;i+=2) + { + /* remove even valued keys */ + g_hash_table_remove (t, test[i].key); + } + + g_assert (g_hash_table_lookup (t, "key-24") == 0); + g_hash_table_foreach(t, print_func, g_strdup("only-odd-keys-25")); + g_hash_table_foreach(t, count_func, 0); + + for(int i=0;i<50;i++) + { + /* only odd keys should be there */ + g_assert((test[i].count == 3 && (i & 1)) + || (test[i].count == 2 && !(i & 1))); + } + + for(int i=1;i<50;i+=2) + { + /* remove odd valued keys */ + g_hash_table_remove (t, g_strdup(test[i].key)); + } + + g_hash_table_foreach(t, fail_func, 0); + return 0; +} -- cgit v1.2.1