00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef Tanl_Text_WordIndex_H
00025 #define Tanl_Text_WordIndex_H
00026
00027
00028 #include "include/unordered_map.h"
00029
00030
00031 #include <string.h>
00032 #include <stdlib.h>
00033
00034 namespace Tanl {
00035 namespace Text {
00036
00037 struct eqstr
00038 {
00039 typedef const char* first_argument_type;
00040 typedef const char* second_argument_type;
00041 typedef bool result_type;
00042
00043 bool operator()(const char* s1, const char* s2) const
00044 {
00045 return strcmp(s1, s2) == 0;
00046 }
00047 };
00048
00049 struct eqstrcase
00050 {
00051 typedef const char* first_argument_type;
00052 typedef const char* second_argument_type;
00053 typedef bool result_type;
00054
00055 bool operator()(const char* s1, const char* s2) const
00056 {
00057 return strcasecmp(s1, s2) == 0;
00058 }
00059 };
00060
00064 struct WordIndex : public unordered_map<const char*, unsigned, hash<const char*>, eqstr>
00065 {
00066 static const unsigned None = (unsigned)-1;
00067 typedef unordered_map<const char*, unsigned, hash<const char*>, eqstr> HashMap;
00068
00069 WordIndex() { }
00070
00071 WordIndex(WordIndex const& other) {
00072 for (WordIndex::const_iterator oit = other.begin();
00073 oit != other.end(); ++oit)
00074 insert(*oit);
00075 }
00076
00077 ~WordIndex() { clear(); }
00078
00079 void clear() {
00080 for (WordIndex::iterator it = begin(); it != end(); ++it) {
00081 free((void*)it->first);
00082 }
00083 HashMap::clear();
00084 }
00085
00090 mapped_type index(const key_type& key) {
00091 iterator _Where = this->find(key);
00092 return (_Where == this->end()) ? None : _Where->second;
00093 }
00094
00098 mapped_type insert(const key_type& key) {
00099 iterator _Where = this->find(key);
00100 if (_Where == this->end()) {
00101 key_type _Keyval = ::strdup(key);
00102 mapped_type mapval = this->size();
00103 HashMap::insert(value_type(_Keyval, mapval));
00104 return mapval;
00105 } else
00106 return _Where->second;
00107 }
00108
00112 std::pair<iterator,bool> insert(const value_type& __obj) {
00113 iterator _Where = this->find(__obj.first);
00114 key_type _Keyval = (_Where == this->end()) ?
00115 ::strdup(__obj.first) :
00116 _Where->first;
00117 return HashMap::insert(value_type(_Keyval, __obj.second));
00118 }
00119
00120 mapped_type& operator[](const key_type& _Keyval) {
00121
00122 iterator _Where = this->find(_Keyval);
00123 if (_Where == this->end())
00124 _Where = this->insert(value_type(_Keyval, mapped_type())).first;
00125 return _Where->second;
00126 }
00127
00128 WordIndex& operator=(const WordIndex& wi)
00129 {
00130 clear();
00131 for (WordIndex::const_iterator it = wi.begin(); it != wi.end(); ++it)
00132 insert(*it);
00133 return *this;
00134 }
00135
00136 };
00137
00138 }
00139 }
00140
00141 #endif // Tanl_Text_WordIndex_H