File: small_map.h

package info (click to toggle)
cgal 3.6.1-2
  • links: PTS
  • area: non-free
  • in suites: squeeze
  • size: 62,184 kB
  • ctags: 95,782
  • sloc: cpp: 453,758; ansic: 96,821; sh: 226; makefile: 120; xml: 2
file content (282 lines) | stat: -rw-r--r-- 6,097 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* Copyright 2004
   Stanford University

   This file is part of the DSR PDB Library.

   The DSR PDB Library is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation; either version 2.1 of the License, or (at your
   option) any later version.

   The DSR PDB Library is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
   License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with the DSR PDB Library; see the file LICENSE.LGPL.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301, USA. */


#ifndef CGAL_DSR_PDB_SMALL_MAP_H
#define CGAL_DSR_PDB_SMALL_MAP_H
#include <CGAL/PDB/basic.h>
#include <vector>
#include <algorithm>

namespace CGAL { namespace PDB {

template <class K, class D>
class small_map_value_type {
  typedef small_map_value_type<K, D> This;
  K key_;
  D data_;
public:
  typedef K Key;
  typedef D Data;
  small_map_value_type(){}
  explicit small_map_value_type(Key k): key_(k){}
  small_map_value_type(Key k, const Data &d): key_(k), data_(d){}
  bool operator<(const This &o) const {
    return key_ < o.key_;
  }
  bool operator>(const This &o) const {
    return key_ > o.key_;
  }
  bool operator==(const This &o) const {
    return key_ == o.key_;
  }
 bool operator!=(const This &o) const {
    return key_ != o.key_;
  }
  bool operator<(Key k) const {
    return key_ < k;
  }
  bool operator>(Key k) const {
    return key_ > k;
  }
  bool operator==(Key k) const {
    return k == key_;
  }
  bool operator!=(Key k) const {
    return k == key_;
  }


  Key key() const {
    return key_;
  }
  const Data &data() const {
    return data_;
  }
  Data &data() {
    return data_;
  }
  operator Key() const {
    return key();
  }
  /*operator Key&() {
    return key();
    }*/

};


template <class K, class D>
bool operator< (const K& k, const small_map_value_type<K,D>& sm)
  {
    return sm > k;
  }


/*}}

CGAL_BEGIN_NAMESPACE
template <class K, class D>
CGAL::Comparison_result compare(const PDB::small_map_value_type<K,D> &a,
				const PDB::small_map_value_type<K,D> &b) {
  return CGAL::compare(a.key(), b.key());
}
CGAL_END_NAMESPACE

namespace CGAL { namespace PDB {*/
/*template <class K, class D>
bool operator<(K k, const small_map_value_type<K,D> &vk) {
  return k < vk.key();
}

template <class K, class D>
bool operator>(K k, const small_map_value_type<K,D> &vk) {
  return k > vk.key();
  }*/


#define CGAL_SMALL_MAP_VALUE_TYPE(CName, K, D, name) struct CName : public CGAL::PDB::small_map_value_type<K,D>{ \
typedef CGAL::PDB::small_map_value_type<K,D> P;\
CName(){}\
CName(K k): P(k){}\
CName(K k, const D &d): P(k,d){}\
D &name(){return P::data();}\
const D& name()const {return P::data();}\
}

template <class Storage>
class small_map {
  typedef small_map<Storage> This;
public:
  typedef Storage value_type;
  
  /*struct Compare {
    bool operator()(Key k, const value_type &v) const {
      return k < v.first;
    }
    bool operator()(const value_type &v, Key k) const {
      return v.first < k;
    }
    bool operator()(const value_type &a, const value_type &b) const {
      return a.first < b.first;
    }
    };*/

  typedef std::vector<value_type> container;
  typedef typename container::iterator iterator;
  typedef typename container::const_iterator const_iterator;

  typedef typename container::reverse_iterator reverse_iterator;
  typedef typename container::const_reverse_iterator const_reverse_iterator;


  small_map(std::size_t sz=0){
    c_.reserve(sz);
  }

  void swap_with(This &o) {
    std::swap(c_, o.c_);
  }

  template <class Key>
  iterator find(const Key k) {
    for (iterator i= begin(); i != end(); ++i) {
      if (i->key() == k) return i;
    }
    return end();
  }
  template <class Key>
  const_iterator find(const Key k) const {
    for (const_iterator i= begin(); i != end(); ++i) {
      if (i->key() == k) return i;
    }
    return end();
  }

  iterator begin() {
    return c_.begin();
  }

  iterator end() {
    return c_.end();
  }


  reverse_iterator rbegin() {
    return c_.rbegin();
  }

  reverse_iterator rend() {
    return c_.rend();
  }

  const_iterator begin() const {
    return c_.begin();
  }

  const_iterator end() const {
    return c_.end();
  }

  const_reverse_iterator rbegin() const {
    return c_.rbegin();
  }

  const_reverse_iterator rend() const {
    return c_.rend();
  }

  template <class K>
  iterator lower_bound(K k) {
    return std::lower_bound(begin(), end(), k/*, Compare()*/);
  }

  template <class K>
  typename Storage::Data& operator[](K k){
    iterator it= find(k);
    if (it == end()) {
      it= insert(value_type(k));
    }
    CGAL_postcondition(it->key() == k);
    return it->data();
  }

  iterator insert(const value_type &v) {
    if (c_.empty() || v > c_.back()) {
      c_.insert(end(), v);
      return c_.end()-1;
    } else {
      iterator it= lower_bound(v.key());
      CGAL_assertion(it== end() || *it != v);
      return c_.insert(it, v);
    }
  }

  std::size_t size() const {
    return c_.size();
  }

  bool empty() const {
    return c_.empty();
  }

  template <class K>
  void erase(K k) {
    iterator it = find(k);
    c_.erase(it);
  }

  /*bool locked() const {
    return locked_;
    }*/
  void sort() {
    std::sort(begin(), end()/*, Compare()*/);
  }

  void clear() {
    c_.clear();
  }

protected:
  container c_;
};

template <class C>
inline void swap(small_map<C> &a, small_map<C> &b) {
  a.swap_with(b);
}

}}


namespace std{
  /*template <class K, class D>
  bool operator<(K k, const CGAL::PDB::small_map_value_type<K,D> &vk) {
    return k < vk.key();
  }

  template <class K, class D>
  bool operator>(K k, const CGAL::PDB::small_map_value_type<K,D> &vk) {
    return k > vk.key();
    }*/

}

#endif