00001 /* -*- Mode: C++ -*- */ 00002 00003 /* 00004 libmoot : moocow's part-of-speech tagging library 00005 Copyright (C) 2003-2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 2 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program; if not, write to the Free Software 00019 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 */ 00021 00022 /*============================================================================ 00023 * File: mootEnum.h 00024 * Author: Bryan Jurish <moocow@ling.uni-potsdam.de> 00025 * Description: 00026 * Templates & classes for runtime enumerations (Identifier<->unsigned maps) 00027 *============================================================================*/ 00028 00029 #ifndef _moot_ENUM_H 00030 #define _moot_ENUM_H 00031 00032 #include <mootTypes.h> 00033 00034 using namespace std; 00035 using namespace moot_STL_NAMESPACE; 00036 00040 typedef unsigned int mootEnumID; 00041 00045 const mootEnumID mootEnumNone = 0; 00046 00059 template <class NameType, class NameHashFcn, class NameEqualFcn> 00060 class mootEnum { 00061 public: 00062 //------ public typedefs 00064 typedef hash_map<NameType,mootEnumID,NameHashFcn,NameEqualFcn> Name2IdMap; 00065 00067 typedef vector<NameType> Id2NameMap; 00068 00069 public: 00070 //------ public data 00071 Name2IdMap names2ids; 00072 Id2NameMap ids2names; 00073 00074 public: 00076 mootEnum(void) 00077 { 00078 ids2names.push_back(NameType()); 00079 }; 00080 00082 mootEnum(const NameType &unknownName) 00083 { 00084 ids2names.push_back(unknownName); 00085 }; 00086 00088 ~mootEnum(void) 00089 { 00090 clear(); 00091 }; 00092 00093 //------ access 00095 inline void unknown_name(const NameType &name) 00096 { 00097 names2ids[name] = 0; 00098 ids2names[0] = name; 00099 }; 00100 00101 //------ sanity checking 00103 inline bool nameExists(const NameType &name) const 00104 { 00105 return names2ids.find(name) != names2ids.end(); 00106 }; 00107 00109 inline bool idExists(const mootEnumID id) const 00110 { 00111 return id && ids2names.size() > id; 00112 }; 00113 00114 00115 //------ access 00117 inline mootEnumID size(void) const 00118 { 00119 return ids2names.size(); 00120 }; 00121 00123 inline mootEnumID name2id(const NameType &name) const 00124 { 00125 typename Name2IdMap::const_iterator i = names2ids.find(name); 00126 return i == names2ids.end() ? 0 : i->second; 00127 }; 00128 00133 inline const NameType &id2name(const mootEnumID id) const 00134 { 00135 return ids2names.size() <= id ? ids2names[0] : ids2names[id]; 00136 }; 00137 00138 //------ manipulation 00139 00145 inline mootEnumID insert(const NameType &name, mootEnumID id=0) 00146 { 00147 if (!id) id = ids2names.size(); 00148 if (ids2names.size() <= id) ids2names.resize(id+1); 00149 ids2names[id] = name; 00150 names2ids[name] = id; 00151 return id; 00152 }; 00153 00155 inline void clear(void) 00156 { 00157 names2ids.clear(); 00158 ids2names.resize(1); // keep "unknown" name 00159 }; 00160 }; 00161 00162 #endif /* _moot_ENUM_H */