mootAssocVector.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-2009 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: mootAssocVector.h
24  * Author: Bryan Jurish <moocow@cpan.org>
25  * Description:
26  * + moocow's PoS tagger : LISP-style assoc vectors
27  *--------------------------------------------------------------------------*/
28 
33 #ifndef MOOT_ASSOC_VECTOR_H
34 #define MOOT_ASSOC_VECTOR_H
35 
36 #include <vector>
37 #include <algorithm>
38 
39 namespace moot {
40 
41  //======================================================================
42  // AssocVectorNode
44  template<typename KeyT, typename ValT>
46  : public std::pair<KeyT,ValT>
47  {
48  //----------------------------------------------------------
49  // Node: types
50  typedef KeyT key_type;
51  typedef ValT value_type;
52  typedef std::pair<KeyT,ValT> PairT;
54 
55  //----------------------------------------------------------
56  // Node: Data
57  //KeyT node_key;
58  //ValT node_val;
59 
60  //----------------------------------------------------------
61  // Node: Methods
62 
63  //--------------------------------------------------
65 
66 
67  inline AssocVectorNode(void)
68  : PairT()
69  {};
70 
72  inline AssocVectorNode(const KeyT &key)
73  : PairT(key,ValT())
74  {};
75 
77  inline AssocVectorNode(const KeyT &key, const ValT &val)
78  : PairT(key,val)
79  {};
80 
82  inline AssocVectorNode(const ThisT &x)
83  : PairT(x.first,x.second)
84  {};
85 
87  ~AssocVectorNode(void) {};
89 
90  //--------------------------------------------------
92 
93  inline key_type &key (void) { return this->first; };
94  inline const key_type &key (void) const { return this->first; };
95 
96  inline value_type &value (void) { return this->second; };
97  inline const value_type &value (void) const { return this->second; };
99 
100  //--------------------------------------------------
102 
103  inline bool operator<(const ThisT &x) const
104  {
105  return (this->first < x.first
106  ? true
107  : (this->first > x.first
108  ? false
109  : this->second < x.second));
110  };
112  }; //-- /AssocVectorNode
113 
114 
115  //======================================================================
116  // AssocVector
117 
129  template<typename KeyT, typename ValT>
130  class AssocVector : public std::vector<AssocVectorNode<KeyT,ValT> >
131  {
132  public:
133  //--------------------------------------------------------------------
134  // AssocVector: Types
135  typedef KeyT assoc_key_type;
136  typedef ValT assoc_value_type;
138  typedef std::vector<assoc_node_type> assoc_vector_type;
139 
140  typedef typename assoc_vector_type::iterator iterator;
141  typedef typename assoc_vector_type::const_iterator const_iterator;
142 
143  typedef typename assoc_vector_type::reverse_iterator reverse_iterator;
144  typedef typename assoc_vector_type::const_reverse_iterator const_reverse_iterator;
145 
146  public:
147  //--------------------------------------------------------------------
148  // AssocVector: Utility Types
149 
152  inline bool operator()(const assoc_node_type &x, const assoc_node_type &y) const
153  {
154  return (x.second < y.second
155  ? true
156  : (x.second > y.second
157  ? false
158  : x.first < y.first));
159  };
160  };
161 
162  public:
163  //--------------------------------------------------------------------
164  // AssocVector: Data
165  //(empty)
166 
167  public:
168  //--------------------------------------------------------------------
169  // AssocVector: Methods
170 
171  //--------------------------------------------------
173 
174 
175  AssocVector(void)
176  : assoc_vector_type()
177  {};
178 
180  inline AssocVector(const size_t mysize)
181  {
182  this->reserve(mysize);
183  };
184 
186  ~AssocVector(void) {};
188 
189  //--------------------------------------------------
191 
192 
194  inline iterator find(const KeyT &key)
195  {
196  iterator i;
197  for (i = this->begin(); i != this->end() && i->first != key; i++) ;
198  return i;
199  };
200 
203  inline const_iterator find(const KeyT &key) const
204  {
205  const_iterator i;
206  for (i = this->begin(); i != this->end() && i->first != key; i++) ;
207  return i;
208  };
209 
212  inline iterator get(const KeyT &key)
213  {
214  iterator i = find(key);
215  return (i == this->end() ? assoc_vector_type::insert(i,key) : i);
216  };
217 
223  inline iterator insert(const KeyT &key, const ValT &val)
224  {
225  iterator i = get(key);
226  i->second = val;
227  return i;
228  };
230 
231  //--------------------------------------------------
233 
234 
238  inline assoc_node_type &get_node(const KeyT &key)
239  { return *(get(key)); };
240 
242  inline assoc_value_type &get_value(const KeyT &key)
243  { return get_node(key).second; };
244 
246  inline assoc_node_type &nth(const size_t n)
247  { return assoc_vector_type::operator[](n); };
249 
250  //--------------------------------------------------
252 
253 
254  inline assoc_value_type &operator[](const KeyT &key)
255  { return get_node(key).second; };
256 
258  inline const assoc_value_type &operator[](const KeyT &key) const
259  { return find(key)->second; };
261 
262  //--------------------------------------------------
264 
265 
266  inline void sort_bykey(void)
267  { std::sort(this->begin(),this->end()); };
268 
270  inline void rsort_bykey(void)
271  { std::sort(this->rbegin(),this->rend()); };
272 
274  inline void sort_byvalue(void)
275  { std::sort(this->begin(),this->end(),value_sort_func()); };
276 
278  inline void rsort_byvalue(void)
279  { std::sort(this->rbegin(),this->rend(),value_sort_func()); };
281 
282  }; //-- /AssocVector
283 
284 }; //-- /namespace moot
285 
286 #endif // MOOT_ASSOC_VECTOR_H
iterator find(const KeyT &key)
Definition: mootAssocVector.h:194
Definition: mootAssocVector.h:39
void rsort_byvalue(void)
Definition: mootAssocVector.h:278
assoc_vector_type::const_iterator const_iterator
Definition: mootAssocVector.h:141
Definition: mootAssocVector.h:151
assoc_vector_type::reverse_iterator reverse_iterator
Definition: mootAssocVector.h:143
AssocVector(const size_t mysize)
Definition: mootAssocVector.h:180
bool operator<(const ThisT &x) const
Definition: mootAssocVector.h:103
AssocVector(void)
Definition: mootAssocVector.h:175
AssocVectorNode< KeyT, ValT > assoc_node_type
Definition: mootAssocVector.h:137
assoc_value_type & operator[](const KeyT &key)
Definition: mootAssocVector.h:254
assoc_node_type & get_node(const KeyT &key)
Definition: mootAssocVector.h:238
ValT assoc_value_type
Definition: mootAssocVector.h:136
iterator insert(const KeyT &key, const ValT &val)
Definition: mootAssocVector.h:223
AssocVectorNode(const KeyT &key, const ValT &val)
Definition: mootAssocVector.h:77
assoc_vector_type::const_reverse_iterator const_reverse_iterator
Definition: mootAssocVector.h:144
LISP-style assoc list using vector<>: map-like class with small memory footprint. Useful for small as...
Definition: mootAssocVector.h:130
void sort_bykey(void)
Definition: mootAssocVector.h:266
assoc_node_type & nth(const size_t n)
Definition: mootAssocVector.h:246
KeyT assoc_key_type
Definition: mootAssocVector.h:135
void rsort_bykey(void)
Definition: mootAssocVector.h:270
void sort_byvalue(void)
Definition: mootAssocVector.h:274
ValT value_type
Definition: mootAssocVector.h:51
AssocVectorNode(const KeyT &key)
Definition: mootAssocVector.h:72
const value_type & value(void) const
Definition: mootAssocVector.h:97
key_type & key(void)
Definition: mootAssocVector.h:93
const assoc_value_type & operator[](const KeyT &key) const
Definition: mootAssocVector.h:258
AssocVectorNode< KeyT, ValT > ThisT
Definition: mootAssocVector.h:53
AssocVectorNode(const ThisT &x)
Definition: mootAssocVector.h:82
std::vector< assoc_node_type > assoc_vector_type
Definition: mootAssocVector.h:138
std::pair< KeyT, ValT > PairT
Definition: mootAssocVector.h:52
~AssocVectorNode(void)
Definition: mootAssocVector.h:87
KeyT key_type
Definition: mootAssocVector.h:50
assoc_value_type & get_value(const KeyT &key)
Definition: mootAssocVector.h:242
assoc_vector_type::iterator iterator
Definition: mootAssocVector.h:140
AssocVectorNode(void)
Definition: mootAssocVector.h:67
template class for individual AssocVector nodes
Definition: mootAssocVector.h:45
const_iterator find(const KeyT &key) const
Definition: mootAssocVector.h:203
~AssocVector(void)
Definition: mootAssocVector.h:186
value_type & value(void)
Definition: mootAssocVector.h:96
const key_type & key(void) const
Definition: mootAssocVector.h:94
bool operator()(const assoc_node_type &x, const assoc_node_type &y) const
Definition: mootAssocVector.h:152