Main Page | Directories | File List

mootEnum.h

Go to the documentation of this file.
00001 /* -*- Mode: C++ -*- */
00002 
00003 /*
00004    libmoot : moocow's part-of-speech tagging library
00005    Copyright (C) 2003-2005 by Bryan Jurish <moocow@ling.uni-potsdam.de>
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2.1 of the License, or (at your option) any later version.
00011    
00012    This library 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 GNU
00015    Lesser General Public License for more details.
00016    
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with this library; 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,
00060           class NameHashFcn  = hash     <NameType>,
00061           class NameEqualFcn = equal_to <NameType>  >
00062 class mootEnum {
00063 public:
00064   //------ public typedefs
00066   typedef hash_map<NameType,mootEnumID,NameHashFcn,NameEqualFcn> Name2IdMap;
00067 
00069   typedef vector<NameType>                                       Id2NameMap;
00070 
00071 public:
00072   //------ public data
00073   Name2IdMap names2ids;  
00074   Id2NameMap ids2names;  
00075 
00076 public:
00078   mootEnum(void)
00079   {
00080     ids2names.push_back(NameType());
00081   };
00082   
00084   mootEnum(const NameType &unknownName)
00085   {
00086     ids2names.push_back(unknownName);
00087   };
00088 
00090   ~mootEnum(void)
00091   {
00092     clear();
00093   };
00094 
00095   //------ access
00097   inline void unknown_name(const NameType &name)
00098   {
00099     names2ids[name] = 0;
00100     ids2names[0] = name;
00101   };
00102 
00103   //------ sanity checking
00105   inline bool nameExists(const NameType &name) const
00106   {
00107     return names2ids.find(name) != names2ids.end();
00108   };
00109 
00111   inline bool idExists(const mootEnumID id) const
00112   {
00113     return id && ids2names.size() > id;
00114   };
00115 
00116 
00117   //------ access
00119   inline mootEnumID size(void) const
00120   {
00121     return ids2names.size();
00122   };
00123 
00125   inline mootEnumID name2id(const NameType &name) const
00126   {
00127     typename Name2IdMap::const_iterator i = names2ids.find(name);
00128     return i == names2ids.end() ? 0 : i->second;
00129   };
00130 
00135   inline const NameType &id2name(const mootEnumID id) const
00136   {
00137     return ids2names.size() <= id ? ids2names[0] : ids2names[id];
00138   };
00139 
00140   //------ manipulation
00141 
00147   inline mootEnumID insert(const NameType &name, mootEnumID id=0)
00148   {
00149     if (!id) id = ids2names.size();
00150     if (ids2names.size() <= id) ids2names.resize(id+1);
00151     ids2names[id] = name;
00152     names2ids[name] = id;
00153     return id;
00154   };
00155 
00157   inline void clear(void)
00158   {
00159     names2ids.clear();
00160     ids2names.resize(1);  // keep "unknown" name
00161   };
00162 };
00163 
00164 #endif /* _moot_ENUM_H */

Generated on Sat Sep 17 01:20:33 2005 for libmoot by  doxygen 1.4.4