mootEnum.h
Go to the documentation of this file.
1 /* -*- Mode: C++ -*- */
2 
3 /*
4  libmoot : moocow's part-of-speech tagging library
5  Copyright (C) 2003-2017 by Bryan Jurish <moocow@cpan.org>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 3 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
22 /*============================================================================
23  * File: mootEnum.h
24  * Author: Bryan Jurish <moocow@cpan.org>
25  * Description:
26  * Templates & classes for runtime enumerations (Identifier<->unsigned maps)
27  *============================================================================*/
28 
34 #ifndef _moot_ENUM_H
35 #define _moot_ENUM_H
36 
37 #include <mootTypes.h>
38 
39 using namespace std;
40 using namespace moot_STL_NAMESPACE;
41 
46 
51 
64 template <class NameType,
65  class NameHashFcn = moot_hash <NameType>,
66  class NameEqualFcn = equal_to <NameType> >
67 class mootEnum {
68 public:
69  //------ public typedefs
71  typedef hash_map<NameType,mootEnumID,NameHashFcn,NameEqualFcn> Name2IdMap;
72 
74  typedef vector<NameType> Id2NameMap;
75 
76 public:
77  //------ public data
78  Name2IdMap names2ids;
79  Id2NameMap ids2names;
80 
81 public:
83  mootEnum(void)
84  {
85  unknown_name(NameType());
86  };
87 
89  mootEnum(const NameType &unknownName)
90  {
91  unknown_name(unknownName);
92  };
93 
95  ~mootEnum(void)
96  {
97  /*clear();*/
98  };
99 
100  //------ access
102  inline void unknown_name(const NameType &name)
103  {
104  if (ids2names.empty()) ids2names.resize(1);
105  ids2names[0] = name;
106  names2ids[name] = 0;
107  };
108 
109  //------ sanity checking
111  inline bool nameExists(const NameType &name) const
112  {
113  return names2ids.find(name) != names2ids.end();
114  };
115 
117  inline bool idExists(const mootEnumID id) const
118  {
119  return id && ids2names.size() > id;
120  };
121 
122 
123  //------ access
125  inline mootEnumID size(void) const
126  {
127  return ids2names.size();
128  };
129 
131  inline mootEnumID name2id(const NameType &name) const
132  {
133  typename Name2IdMap::const_iterator i = names2ids.find(name);
134  return i == names2ids.end() ? 0 : i->second;
135  };
136 
141  inline const NameType &id2name(const mootEnumID id) const
142  {
143  return ids2names.size() <= id ? ids2names[0] : ids2names[id];
144  };
145 
146  //------ manipulation
147 
153  inline mootEnumID insert(const NameType &name, mootEnumID id=0)
154  {
155  if (!id) id = ids2names.size();
156  if (ids2names.size() <= id) ids2names.resize(id+1);
157  ids2names[id] = name;
158  names2ids[name] = id;
159  return id;
160  };
161 
167  inline void remove(const NameType &name, mootEnumID id=mootEnumNone)
168  {
169  names2ids.erase(name);
170  if (id != mootEnumNone) ids2names[id] = ids2names[mootEnumNone];
171  };
172 
174  inline void remove(mootEnumID id)
175  {
176  remove(ids2names[id],id);
177  };
178 
179 
183  inline mootEnumID get_id(const NameType &name)
184  {
185  typename Name2IdMap::const_iterator i = names2ids.find(name);
186  return i == names2ids.end() ? insert(name,0) : i->second;
187  };
188 
190  inline void clear(void)
191  {
192  names2ids.clear();
193  //--
194  ids2names.resize(1); //-- keep "unknown" name
195  //--
196  //ids2names.clear(); //-- clear *everything*
197  };
198 
202  void resize(size_t newsize)
203  {
204  if (newsize < 1) newsize=1; //-- minimum size = 1 (always keep "unknown")
205  if (newsize > size()) { //-- grow (only ids2names)
206  ids2names.resize(newsize);
207  }
208  else if (newsize < size()) { //-- shrink
209  for (mootEnumID id = newsize; id < size(); id++) {
210  names2ids.erase(ids2names[id]);
211  }
212  ids2names.resize(newsize);
213  }
214  };
215 };
216 
217 #endif /* _moot_ENUM_H */
void resize(size_t newsize)
Definition: mootEnum.h:202
Name2IdMap names2ids
maps names to IDs
Definition: mootEnum.h:78
mootEnumID insert(const NameType &name, mootEnumID id=0)
Definition: mootEnum.h:153
const NameType & id2name(const mootEnumID id) const
Definition: mootEnum.h:141
void clear(void)
Definition: mootEnum.h:190
mootEnumID name2id(const NameType &name) const
Definition: mootEnum.h:131
Definition: mootEnum.h:67
bool nameExists(const NameType &name) const
Definition: mootEnum.h:111
void unknown_name(const NameType &name)
Definition: mootEnum.h:102
bool idExists(const mootEnumID id) const
Definition: mootEnum.h:117
BinUInt UInt
Definition: mootTypes.h:86
mootEnumID get_id(const NameType &name)
Definition: mootEnum.h:183
hash_map< NameType, mootEnumID, NameHashFcn, NameEqualFcn > Name2IdMap
Definition: mootEnum.h:71
Common typedefs and constants.
mootEnumID size(void) const
Definition: mootEnum.h:125
~mootEnum(void)
Definition: mootEnum.h:95
mootEnum(void)
Definition: mootEnum.h:83
mootEnum(const NameType &unknownName)
Definition: mootEnum.h:89
Id2NameMap ids2names
maps IDs to names
Definition: mootEnum.h:79
moot::UInt mootEnumID
Definition: mootEnum.h:45
const mootEnumID mootEnumNone
Definition: mootEnum.h:50
vector< NameType > Id2NameMap
Definition: mootEnum.h:74