The unified diff between revisions [878203cf..] and [ecac2638..] is displayed below. It can also be downloaded as a raw diff.
#
#
# add_file "test.cc"
# content [601b75b1a52d72a222ee4d2ae83a42e8bb0605db]
#
# add_file "tokens_test.cc"
# content [a428306994e6e19bb09e0ced865fac495b2e2b50]
#
# patch "CMakeLists.txt"
# from [3a7a37f0632df3ba80cd4ecb9d3b710878a2ecb4]
# to [8ed254205fc2321a64b60d053bcef5b8e4d50f13]
#
# patch "fritz_server.cc"
# from [2837584fd0ed31f00560e0b44b2568ca205079d3]
# to [d54d439d19fb718be381dd9fd92d7ca88aa07734]
#
# patch "tokens.hh"
# from [2021d69a582f139f905d4df796a41f002e4626ea]
# to [f93c5530640aa4ed80e8d6d04867f3047428f65c]
#
============================================================
--- test.cc 601b75b1a52d72a222ee4d2ae83a42e8bb0605db
+++ test.cc 601b75b1a52d72a222ee4d2ae83a42e8bb0605db
@@ -0,0 +1,18 @@
+
+#include <ext/hash_map>
+#include <vector>
+#include <iostream>
+#include "tokens.hh"
+
+int
+main (int argc, char *argv[])
+{
+ __gnu_cxx::hash_map<std::string, int, string_hash<std::string> > h;
+ std::string blah("test"), cat("goat");
+
+ h[blah] = 2;
+ h[cat] = 23;
+ std::cout << blah << " ::: " << h[blah] << ":::" << h[cat] << std::endl;
+
+ return 0;
+}
============================================================
--- tokens_test.cc a428306994e6e19bb09e0ced865fac495b2e2b50
+++ tokens_test.cc a428306994e6e19bb09e0ced865fac495b2e2b50
@@ -0,0 +1,23 @@
+
+#include <iostream>
+#include "tokens.hh"
+
+void
+string_test (void)
+{
+ std::string s("test");
+ Tokens<std::string, string_hash<std::string> > t;
+
+ t.add (s);
+ t.lookup (s);
+ std::cout << s << std::endl;
+ t.playback ();
+}
+
+int
+main (int argc, char *argv[])
+{
+ string_test ();
+ return 0;
+}
+
============================================================
--- CMakeLists.txt 3a7a37f0632df3ba80cd4ecb9d3b710878a2ecb4
+++ CMakeLists.txt 8ed254205fc2321a64b60d053bcef5b8e4d50f13
@@ -21,3 +21,8 @@
ENDIF (LIB_DL)
add_executable (fritz_this fritz_this.cc)
+
+add_executable (tokens_test tokens_test.cc)
+
+add_executable (test test.cc)
+
============================================================
--- fritz_server.cc 2837584fd0ed31f00560e0b44b2568ca205079d3
+++ fritz_server.cc d54d439d19fb718be381dd9fd92d7ca88aa07734
@@ -1,27 +1,10 @@
#include <iostream>
#include "fritz.hh"
-#include "tokens.hh"
int
main (int argc, char *argv[])
{
- Tokens<char *> t;
-
- t.add ("hello?");
- t.add ("my");
- t.add ("name");
- t.add ("is");
- t.add ("bob");
- t.add ("and");
- t.add ("I'm");
- t.add ("and");
- t.add ("goodbye?");
- std::cout << t.token_query ("hello?") << std::endl;
- std::cout << t.token_query ("goodbye?") << std::endl;
- std::cout << t.token_query ("death?") << std::endl;
- t.playback ();
-
FritzServer f;
f.run ();
}
============================================================
--- tokens.hh 2021d69a582f139f905d4df796a41f002e4626ea
+++ tokens.hh f93c5530640aa4ed80e8d6d04867f3047428f65c
@@ -7,20 +7,26 @@ typedef unsigned long int token_t;
typedef unsigned long int token_t;
-struct eqstr
+template<class T>
+class string_hash
{
- bool operator()(const char *s1, const char *s2) const
+private:
+ __gnu_cxx::hash<char *> h;
+
+public:
+ size_t
+ operator()(const T &s) const
{
- return strcmp(s1, s2) == 0;
+ return h (s.c_str ());
}
};
-template<class T>
+template<class T, class H>
class Tokens {
private:
token_t last_id;
std::vector<token_t> stash;
- __gnu_cxx::hash_map<T, token_t, __gnu_cxx::hash<T> > token_to_id;
+ __gnu_cxx::hash_map<T, token_t, H> token_to_id;
__gnu_cxx::hash_map<token_t, T, __gnu_cxx::hash<token_t> > id_to_token;
public:
@@ -32,17 +38,18 @@ class Tokens {
}
- token_t token_query(T query) {
+ token_t lookup(const T &query) {
return token_to_id[query];
}
- token_t add(T add) {
+ token_t add(const T &add) {
+ T our_copy = add;
token_t tid;
- tid = token_to_id[add];
+ tid = token_to_id[our_copy];
if (tid == 0) {
- tid = token_to_id[add] = ++last_id;
- id_to_token[tid] = add;
+ tid = token_to_id[our_copy] = ++last_id;
+ id_to_token[tid] = our_copy;
}
stash.push_back (tid);
return tid;
@@ -53,7 +60,6 @@ class Tokens {
std::cout << *i << " : " << id_to_token[*i] << std::endl;
}
}
-
};
#endif