ddc
StaticVectorMap.h
Go to the documentation of this file.
1 //
2 // This file is part of DDC.
3 //
4 // DDC is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // DDC is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with DDC. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // ========== Dialing Lemmatizer (www.aot.ru)
18 // ========== Copyright by Alexey Sokirko, Bryan Jurish (2011)
19 
20 #ifndef __index_vector_h_
21 #define __index_vector_h_
22 
23 /*
24  CStaticVectorMap is a class for storing and retrieving a vector of T by a key.
25  The key is of DWORD and there should be no holes between two neighbour keys. i.e.
26  for each i key[i]+1 = key[i+1].
27 */
28 template <class T>
30 {
31  // the keys, each item points to the beginning of the vector. For example,
32  // "m_Keys[i] = j" means that the vector No i starts from j position.
33  // One "CStaticVectorMap" stores m_Keys.size() vectors.
34  vector< DWORD > m_Keys;
35 
36  // the base of the index
37  vector< T > m_Base;
38 
39 public:
40  typedef typename vector<T>::const_iterator const_iter_t;
41 
42 
43  const_iter_t GetVectorEnd (DWORD VectorNo) const
44  {
45  return ( VectorNo+1 == m_Keys.size() )
46  ? m_Base.end()
47  : m_Base.begin() + m_Keys[VectorNo + 1];
48  }
49  const_iter_t GetVectorBegin (DWORD VectorNo) const
50  {
51  assert(VectorNo < m_Keys.size());
52  return m_Base.begin() + m_Keys[VectorNo];
53  }
54  DWORD GetVectorLength (DWORD VectorNo) const
55  {
56  const_iter_t begin = GetVectorBegin(VectorNo);
57  const_iter_t end = GetVectorEnd(VectorNo);
58  return end - begin;
59  }
60 
61  DWORD size() const
62  {
63  return m_Keys.size();
64  }
65 
66  bool empty() const
67  {
68  return m_Keys.empty();
69  }
70 
71  void Create(vector< vector<T> >& src)
72  {
73  m_Keys.resize( src.size() );
74  m_Base.clear();
75  for (DWORD i = 0; i < src.size(); i++)
76  {
77  m_Keys[i] = m_Base.size();
78  m_Base.insert(m_Base.end(), src[i].begin(), src[i].end());
79  };
80  }
81 
82 
83  bool LoadVectorMap(string filename)
84  {
85  FILE* fp;
86  try
87  {
88  fp = fopen(filename.c_str(), "rb");
89  if (!fp)
90  return false;
91 
92  DWORD Length;
93  fread ((void *)&Length, 1, sizeof(DWORD), fp);
94  assert(Length > 0);
95  m_Keys.clear();
96  ReadVectorInner(fp, m_Keys, Length);
97 
98 
99  fread ((void *)&Length, 1, sizeof(DWORD), fp);
100  m_Base.clear();
101  ReadVectorInner(fp, m_Base, Length);
102 
103  fclose (fp);
104  }
105  catch(...)
106  {
107  return false;
108  }
109  return true;
110  }
111 
112  bool SaveVectorMap(string filename)
113  {
114  try
115  {
116  FILE* fp = fopen(filename.c_str(), "wb");
117  if (!fp)
118  return(false);
119 
120  DWORD nCount = m_Keys.size();
121  fwrite((void *)&nCount, sizeof(DWORD), 1, fp);
122  WriteVectorInner(fp, m_Keys);
123 
124  nCount = m_Base.size();
125  fwrite ((void *)&nCount, sizeof(DWORD), 1, fp);
126  WriteVectorInner(fp, m_Base);
127 
128  fclose(fp);
129  }
130  catch(...)
131  {
132  return false;
133  }
134  return true;
135  }
136 
137  size_t GetBaseSize() const
138  {
139  return m_Base.size();
140  };
141 
142  bool GetKeyByBaseNo(DWORD BaseNo, DWORD& KeyNo, DWORD& DataNo) const
143  {
144  KeyNo = 0;
145  if (BaseNo != 0)
146  {
147  vector<DWORD>::const_iterator it = lower_bound(m_Keys.begin(), m_Keys.end(), BaseNo);
148  if (it == m_Keys.begin()) return false;
149  if (it == m_Keys.end()) return false;
150  if (BaseNo < *it) it--;
151  KeyNo = it - m_Keys.begin();
152  };
153  DataNo = BaseNo - m_Keys[KeyNo];
154  return true;
155  };
156 
157 
158 
159 };
160 
161 
162 #endif
163 
164 /*--- emacs style variables ---
165  * Local Variables:
166  * mode: C++
167  * c-file-style: "ellemtel"
168  * c-basic-offset: 4
169  * tab-width: 8
170  * indent-tabs-mode: nil
171  * End:
172  */
size_t GetBaseSize() const
Definition: StaticVectorMap.h:137
void Create(vector< vector< T > > &src)
Definition: StaticVectorMap.h:71
vector< DWORD > m_Keys
Definition: StaticVectorMap.h:34
vector< T >::const_iterator const_iter_t
Definition: StaticVectorMap.h:40
vector< T > m_Base
Definition: StaticVectorMap.h:37
const_iter_t GetVectorBegin(DWORD VectorNo) const
Definition: StaticVectorMap.h:49
bool LoadVectorMap(string filename)
Definition: StaticVectorMap.h:83
void ReadVectorInner(FILE *fp, vector< T > &V, size_t Count)
Definition: bserialize.h:251
const_iter_t GetVectorEnd(DWORD VectorNo) const
Definition: StaticVectorMap.h:43
bool empty() const
Definition: StaticVectorMap.h:66
bool GetKeyByBaseNo(DWORD BaseNo, DWORD &KeyNo, DWORD &DataNo) const
Definition: StaticVectorMap.h:142
DWORD size() const
Definition: StaticVectorMap.h:61
bool WriteVectorInner(FILE *fp, const vector< T > &V)
Definition: bserialize.h:329
bool SaveVectorMap(string filename)
Definition: StaticVectorMap.h:112
uint32_t DWORD
Definition: utilit.h:105
DWORD GetVectorLength(DWORD VectorNo) const
Definition: StaticVectorMap.h:54
Definition: StaticVectorMap.h:29