Main Page | Directories | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

mootAssocVector.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-2004 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: mootAssocVector.h
00024  * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
00025  * Description:
00026  *   + moocow's PoS tagger : LISP-style assoc vectors
00027  *--------------------------------------------------------------------------*/
00028 
00029 #ifndef MOOT_ASSOC_VECTOR_H
00030 #define MOOT_ASSOC_VECTOR_H
00031 
00032 #include <vector>
00033 #include <algorithm>
00034 
00035 namespace moot {
00036 
00037   //======================================================================
00038   // AssocVectorNode
00040   template<typename KeyT, typename ValT>
00041   struct AssocVectorNode
00042     : public std::pair<KeyT,ValT>
00043   {
00044     //----------------------------------------------------------
00045     // Node: types
00046     typedef KeyT                        key_type;
00047     typedef ValT                        value_type;
00048     typedef std::pair<KeyT,ValT>        PairT;
00049     typedef AssocVectorNode<KeyT,ValT>  ThisT;
00050 
00051     //----------------------------------------------------------
00052     // Node: Data
00053     //KeyT  node_key;
00054     //ValT  node_val;
00055 
00056     //----------------------------------------------------------
00057     // Node: Methods
00058 
00059     //--------------------------------------------------
00061 
00062 
00063     inline AssocVectorNode(void) {};
00064 
00066     inline AssocVectorNode(const KeyT &key)
00067       : PairT(key,ValT())
00068     {};
00069 
00071     inline AssocVectorNode(const KeyT &key, const ValT &val)
00072       : PairT(key,val)
00073     {};
00074 
00076     inline AssocVectorNode(const ThisT &x)
00077       : PairT(x.first,x.second)
00078     {};
00079 
00081     inline ~AssocVectorNode(void) {};
00083 
00084     //--------------------------------------------------
00086 
00087     inline       key_type   &key   (void)       { return first; };
00088     inline const key_type   &key   (void) const { return first; };
00089 
00090     inline       value_type &value (void)       { return second; };
00091     inline const value_type &value (void) const { return second; };
00093 
00094     //--------------------------------------------------
00096 
00097     inline bool operator<(const ThisT &x) const
00098     {
00099       return (first < x.first
00100               ? true
00101               : (first > x.first
00102                  ? false
00103                  : second < x.second));
00104     };
00106   }; //-- /AssocVectorNode
00107 
00108 
00109   //======================================================================
00110   // AssocVector
00111 
00123   template<typename KeyT, typename ValT>
00124   class AssocVector  : public std::vector<AssocVectorNode<KeyT,ValT> >
00125   {
00126   public:
00127     //--------------------------------------------------------------------
00128     // AssocVector: Types
00129     typedef KeyT                                                  assoc_key_type;
00130     typedef ValT                                                  assoc_value_type;
00131     typedef AssocVectorNode<KeyT,ValT>                            assoc_node_type;
00132     typedef std::vector<assoc_node_type>                          assoc_vector_type;
00133 
00134     typedef typename assoc_vector_type::iterator                  iterator;
00135     typedef typename assoc_vector_type::const_iterator            const_iterator;
00136 
00137     typedef typename assoc_vector_type::reverse_iterator          reverse_iterator;
00138     typedef typename assoc_vector_type::const_reverse_iterator    const_reverse_iterator;
00139 
00140   public:
00141     //--------------------------------------------------------------------
00142     // AssocVector: Utility Types
00143 
00145     struct value_sort_func {
00146       inline bool operator()(const assoc_node_type &x, const assoc_node_type &y) const
00147       {
00148         return (x.second < y.second
00149                 ? true
00150                 : (x.second > y.second
00151                    ? false
00152                    : x.first < y.first));
00153       };
00154     };
00155 
00156   public:
00157     //--------------------------------------------------------------------
00158     // AssocVector: Data
00159     //(empty)
00160 
00161   public:
00162     //--------------------------------------------------------------------
00163     // AssocVector: Methods
00164 
00165     //--------------------------------------------------
00167 
00168 
00169     inline AssocVector(void) {};
00170 
00172     inline AssocVector(const size_t mysize)
00173     {
00174       reserve(mysize);
00175     };
00176 
00178     inline ~AssocVector(void) {};
00180 
00181     //--------------------------------------------------
00183 
00184 
00186     inline iterator find(const KeyT &key)
00187     {
00188       iterator i;
00189       for (i = begin(); i != end() && i->first != key; i++) ;
00190       return i;
00191     };
00192 
00195     inline const_iterator find(const KeyT &key) const
00196     {
00197       const_iterator i;
00198       for (i = begin(); i != end() && i->first != key; i++) ;
00199       return i;
00200     };
00201 
00204     inline iterator get(const KeyT &key)
00205     {
00206       iterator i = find(key);
00207       return (i == end() ? assoc_vector_type::insert(i,key) : i);
00208     };
00209 
00215     inline iterator insert(const KeyT &key, const ValT &val)
00216     {
00217       iterator i  = get(key);
00218       i->second = val;
00219       return i;
00220     };
00222 
00223     //--------------------------------------------------
00225 
00226 
00230     inline assoc_node_type &get_node(const KeyT &key)
00231     { return *(get(key)); };
00232 
00234     inline assoc_value_type &get_value(const KeyT &key)
00235     { return get_node(key).second; };
00236 
00238     inline assoc_node_type &nth(const size_t n)
00239     { return assoc_vector_type::operator[](n); };
00241 
00242     //--------------------------------------------------
00244 
00245 
00246     inline assoc_value_type &operator[](const KeyT &key)
00247     { return get_node(key).second; };
00248 
00250     inline const assoc_value_type &operator[](const KeyT &key) const
00251     { return find(key)->second; };
00253 
00254     //--------------------------------------------------
00256 
00257 
00258     inline void sort_bykey(void)
00259     { std::sort(begin(),end()); };
00260 
00262     inline void rsort_bykey(void)
00263     { std::sort(rbegin(),rend()); };
00264 
00266     inline void sort_byvalue(void)
00267     { std::sort(begin(),end(),value_sort_func()); };
00268 
00270     inline void rsort_byvalue(void)
00271     { std::sort(rbegin(),rend(),value_sort_func()); };
00273 
00274   }; //-- /AssocVector
00275 
00276 }; //-- /namespace moot
00277 
00278 #endif // MOOT_ASSOC_VECTOR_H

Generated on Mon Jun 27 13:05:25 2005 for libmoot by  doxygen 1.3.8-20040913