File: map

package info (click to toggle)
html2text 1.3.2a-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 736 kB
  • ctags: 1,694
  • sloc: cpp: 9,311; yacc: 828; sh: 209; makefile: 144
file content (588 lines) | stat: -rw-r--r-- 18,237 bytes parent folder | download | duplicates (8)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588

/* ------------------------------------------------------------------------- */

/*
 * Copyright (c) 1999
 *      GMRS Software GmbH, Innsbrucker Ring 159, 81669 Munich, Germany.
 *      http://www.gmrs.de
 *      All rights reserved.
 *      Author: Arno Unkrig (arno.unkrig@gmrs.de)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by GMRS Software GmbH.
 * 4. The name of GMRS Software GmbH may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY GMRS SOFTWARE GMBH ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GMRS SOFTWARE GMBH BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

/* ------------------------------------------------------------------------- */

#ifndef __map_INCLUDED__ /* { */
#define __map_INCLUDED__

/* ------------------------------------------------------------------------- */

#ident "$Id: map,v 1.2 1999/11/09 19:54:44 arno Exp $"

#include "../rb_tree.h"

/* ------------------------------------------------------------------------- */

template <class key_type, class mapped_type> class map__node;
template <class key_type, class mapped_type> class map__iterator;
template <class key_type, class mapped_type> class map__const_iterator;
template <class key_type, class mapped_type> class map;
template <class key_type, class mapped_type> class multimap;

template <class key_type, class mapped_type>
struct map__node : public rb_tree::node_type {
  map__node(const pair<const key_type, mapped_type> &v) : value(v) {
  }
//~map__node() {
//}
  pair<const key_type, mapped_type> value;
};

template <class key_type, class mapped_type>
class map__iterator {
public:
  map__iterator() {
  }
  map__iterator(const map__iterator<key_type, mapped_type> &x) : node(x.node) {
  }
  pair<const key_type, mapped_type> &operator*() const {
    return ((map__node<key_type, mapped_type> *) node)->value;
  }
  pair<const key_type, mapped_type> *operator->() const {
    return &((map__node<key_type, mapped_type> *) node)->value;
  }
  bool operator==(const map__iterator<key_type, mapped_type> &x) const {
    return node == x.node;
  }
  bool operator!=(const map__iterator<key_type, mapped_type> &x) const {
    return node != x.node;
  }
  const map__iterator<key_type, mapped_type> &operator++() {  // pre-increment
    node = rb_tree::successor(node); return *this;
  }
private:
  map__iterator(rb_tree::node_type *n) : node(n) {
  }
  rb_tree::node_type *node;
  friend map<key_type, mapped_type>;
  friend multimap<key_type, mapped_type>;
  friend map__const_iterator<key_type, mapped_type>;
};

template <class key_type, class mapped_type>
class map__const_iterator {
public:
  map__const_iterator() {
  }
  map__const_iterator(const map__const_iterator<key_type, mapped_type> &x) :
    node(x.node) {
  }
  map__const_iterator(const map__iterator<key_type, mapped_type> &x) :
    node(x.node) {
  }
  const pair<const key_type, mapped_type> &operator*() const {
    return ((const map__node<key_type, mapped_type> *) node)->value;
  }
  const pair<const key_type, mapped_type> *operator->() const {
    return &((const map__node<key_type, mapped_type> *) node)->value;
  }
  bool operator==(const map__const_iterator<key_type, mapped_type> &x) const {
    return node == x.node;
  }
  bool operator!=(const map__const_iterator<key_type, mapped_type> &x) const {
    return node != x.node;
  }
  const map__const_iterator<key_type, mapped_type> &operator++() { // pre-incr
    node = rb_tree::successor(node); return *this;
  }
private:
  map__const_iterator(const rb_tree::node_type *n) : node(n) {
  }
  const rb_tree::node_type *node;

  friend map<key_type, mapped_type>;
  friend multimap<key_type, mapped_type>;
};

/* ------------------------------------------------------------------------- */

template <class key_type, class mapped_type>
class map : public rb_tree {

  // Public types

public:
  typedef pair<const key_type, mapped_type>          value_type;
  typedef map__iterator<key_type, mapped_type>       iterator;
  typedef map__const_iterator<key_type, mapped_type> const_iterator;

  // Private types

private:
  typedef map__node<key_type, mapped_type> node_type;

  // Construct/Copy/Destroy

public:
  explicit map() : rb_tree() {
  }
  map(const_iterator i1, const_iterator i2) :
    rb_tree(i1.node, i2.node, copy_node2) {
  }
  map(const map<key_type, mapped_type> &x) : rb_tree(x) {
  }
  const map<key_type, mapped_type> &operator=(
    const map<key_type, mapped_type> &x
  ) {
    rb_tree::operator=(x); return *this;
  }
  ~map() {
    clear(delete_node2);
  }

  // Iterators

  iterator begin() {
    return iterator(rb_tree::begin());
  }
  const_iterator begin() const {
    return const_iterator(rb_tree::begin());
  }
  iterator end() {
    return iterator(rb_tree::end());
  }
  const_iterator end() const {
    return const_iterator(rb_tree::end());
  }
//reverse_iterator rbegin();
//const_reverse_iterator rbegin() const;
//reverse_iterator rend();
//const_reverse_iterator rend() const;

  // Capacity

//bool      empty()    const;  // Inherited from "rb_tree".
//size_type size()     const;  // Inherited from "rb_tree".
//size_type max_size() const;  // Inherited from "rb_tree".

  // Element access

  mapped_type &operator[](const key_type &x) {
    rb_tree::node_type *n = rb_tree::find_any((value_pointer) &x);
    if (n == rb_tree::end()) {
      n = rb_tree::insert(new node_type(value_type(x, mapped_type())));
    }
    return ((node_type *) n)->value.second;
  }

  // Modifiers

  // Must not use "iterator", else CFRONT fails.
  pair<map__iterator<key_type, mapped_type>, bool> insert(const value_type &x) {
    if (rb_tree::find_any((value_pointer) &x.first) != rb_tree::end()) {
      return pair<iterator, bool>(end(), false);
    }
    return pair<iterator, bool>(
      iterator(rb_tree::insert(new node_type(x))),
      true
    );
  }
  iterator insert(iterator, const value_type &);
  void insert(const_iterator from, const_iterator to) {
    rb_tree::insert(from.node, to.node);
  }
  size_type erase(const key_type &x) {
    return rb_tree::erase_one((value_pointer) &x);
  }
  iterator erase(iterator i) {
    return iterator(rb_tree::erase(i.node));
  }
  iterator erase(iterator i1, iterator i2) {
    return iterator(rb_tree::erase(i1.node, i2.node));
  }
  void swap(map<key_type, mapped_type> &x) {
    rb_tree::swap((rb_tree &) x);
  }
//void clear();                // Inherited from "rb_tree".

  // Map operations

  iterator find(const key_type &x) {
    return iterator(rb_tree::find_any((value_pointer) &x));
  }
  const_iterator find(const key_type &x) const {
    return const_iterator(rb_tree::find_any((value_pointer) &x));
  }
  size_type count(const key_type &x) const {
    return find_any((value_pointer) &x) != rb_tree::end();
  }
  iterator lower_bound(const key_type &x) {
    return iterator(rb_tree::lower_bound((value_pointer) &x));
  }
  const_iterator lower_bound(const key_type &x) const {
    return const_iterator(rb_tree::lower_bound((value_pointer) &x));
  }
  iterator upper_bound(const key_type &x) {
    return iterator(rb_tree::upper_bound((value_pointer) &x));
  }
  const_iterator upper_bound(const key_type &x) const {
    return const_iterator(rb_tree::upper_bound((value_pointer) &x));
  }
  // Must not use "pair<iterator, iterator>", else CFRONT fails
  pair<
    map__iterator<key_type, mapped_type>,
    map__iterator<key_type, mapped_type>
  > equal_range(const key_type &x) {
    return pair<iterator, iterator>(lower_bound(x), upper_bound(x));
  }
  // Must not use "pair<const_iterator, const_iterator>", else CFRONT fails
  pair<
    map__const_iterator<key_type, mapped_type>,
    map__const_iterator<key_type, mapped_type>
  > equal_range(const key_type &x) const {
    return pair<const_iterator, const_iterator>(
      lower_bound(x),
      upper_bound(x)
    );
  }

  bool operator==(const map<key_type, mapped_type> &x) const {
    return rb_tree::operator==((const rb_tree &) x);
  }
  bool operator<(const map<key_type, mapped_type> &x) const {
    return rb_tree::operator<((const rb_tree &) x);
  }

  // Implementation of "rb_tree"'s virtual methods.

private:
  /*virtual*/ bool node_less_than(
    const rb_tree::node_type *x,
    const rb_tree::node_type *y
  ) const {
    return (
      ((const node_type *) x)->value.first <
      ((const node_type *) y)->value.first
    );
  }
  /*virtual*/ bool node_less_than(
    value_pointer            x,
    const rb_tree::node_type *y
  ) const {
    return *(const key_type *) x < ((const node_type *) y)->value.first;
  }
  /*virtual*/ bool node_less_than(
    const rb_tree::node_type *x,
    value_pointer            y
  ) const {
    return ((const node_type *) x)->value.first < *(const key_type *) y;
  }

  /*virtual*/ rb_tree::node_type *copy_node(const rb_tree::node_type *n) const {
    return new node_type(((const node_type *) n)->value);
  }
  /*virtual*/ void delete_node(rb_tree::node_type *n) const {
    delete (node_type *) n;
  }

  typedef void (*key_mapped_printer)(
    ostream           &,
    const key_type    &,
    const mapped_type &
  );
  /*virtual*/ void print_node_value(
    const rb_tree::node_type &n,
    ostream                  &os,
    void                     *closure
  ) const {
    (*(key_mapped_printer *) closure)(
      os,
      ((const node_type &) n).value.first,
      ((const node_type &) n).value.second
    );
  }
  void print(ostream &os, key_mapped_printer np) const {
    rb_tree::print(os, (void *) &np);
  }
  friend ostream &operator<<(
    ostream                          &,
    const map<key_type, mapped_type> &
  );

  // Needed by "map(iter, iter)".
  static rb_tree::node_type *copy_node2(const rb_tree::node_type *n) {
    return new node_type(((const node_type *) n)->value);
  }
  // Needed by "~map()".
  static void delete_node2(rb_tree::node_type *n) {
    delete (node_type *) n;
  }

  friend map__iterator<key_type, mapped_type>;
  friend map__const_iterator<key_type, mapped_type>;
};

/* ------------------------------------------------------------------------- */

template <class key_type, class mapped_type>
class multimap : public rb_tree {

  // Public types

public:
  typedef pair<const key_type, mapped_type>          value_type;
  typedef map__iterator<key_type, mapped_type>       iterator;
  typedef map__const_iterator<key_type, mapped_type> const_iterator;

  // Private types

private:
  typedef map__node<key_type, mapped_type> node_type;

  // Construct/Copy/Destroy

public:
  explicit multimap() : rb_tree() {
  }
  multimap(const_iterator i1, const_iterator i2) :
    rb_tree(i1.node, i2.node, copy_node2) {
  }
  multimap(const multimap<key_type, mapped_type> &x) : rb_tree(x) {
  }
  const multimap<key_type, mapped_type> &operator=(
    const multimap<key_type, mapped_type> &x
  ) {
    rb_tree::operator=(x); return *this;
  }
  ~multimap() {
    clear(delete_node2);
  }

  // Iterators

  iterator begin() {
    return iterator((node_type *) rb_tree::begin());
  }
  const_iterator begin() const {
    return const_iterator((const node_type *) rb_tree::begin());
  }
  iterator end() {
    return iterator((node_type *) rb_tree::end());
  }
  const_iterator end() const {
    return const_iterator((const node_type *) rb_tree::end());
  }
//reverse_iterator rbegin();
//const_reverse_iterator rbegin() const;
//reverse_iterator rend();
//const_reverse_iterator rend() const;

  // Capacity

//bool      empty()    const;  // Inherited from "rb_tree".
//size_type size()     const;  // Inherited from "rb_tree".
//size_type max_size() const;  // Inherited from "rb_tree".

  // Modifiers

  iterator insert(const value_type &x) {
    return iterator((node_type *) rb_tree::insert(new node_type(x)));
  }
//iterator insert(iterator, const value_type &);
  void insert(const_iterator from, const_iterator to) {
    rb_tree::insert(from.node, to.node);
  }
  size_type erase(const key_type &x) {
    return rb_tree::erase_all((value_pointer) &x);
  }
  iterator erase(iterator i) {
    return iterator(rb_tree::erase(i.node));
  }
  iterator erase(iterator i1, iterator i2) {
    return iterator(rb_tree::erase(i1.node, i2.node));
  }
  void swap(multimap<key_type, mapped_type> &x) {
    rb_tree::swap((rb_tree &) x);
  }
//void clear();                // Inherited from "rb_tree".

  // Multimap operations

  iterator find(const key_type &x) {
    return iterator(rb_tree::find_first((value_pointer) &x));
  }
  const_iterator find(const key_type &x) const {
    return const_iterator(rb_tree::find_first((value_pointer) &x));
  }
  size_type count(const key_type &x) const {
    return rb_tree::count((value_pointer) &x);
  }
  iterator lower_bound(const key_type &x) {
    return iterator(rb_tree::lower_bound((value_pointer) &x));
  }
  const_iterator lower_bound(const key_type &x) const {
    return const_iterator(rb_tree::lower_bound((value_pointer) &x));
  }
  iterator upper_bound(const key_type &x) {
    return iterator(rb_tree::upper_bound((value_pointer) &x));
  }
  const_iterator upper_bound(const key_type &x) const {
    return const_iterator(rb_tree::upper_bound((value_pointer) &x));
  }
  // Must not use "pair<iterator, iterator>", else CFRONT fails
  // G++ 2.7.2.1 cannot compile this ("field "first" has incomplete type")!?
//pair<
//  map__iterator<key_type, mapped_type>,
//  map__iterator<key_type, mapped_type>
//> equal_range(const key_type &x) {
//  return pair<iterator, iterator>(lower_bound(x), upper_bound(x));
//}
  // Must not use "pair<const_iterator, const_iterator>", else CFRONT fails
  pair<
    map__const_iterator<key_type, mapped_type>,
    map__const_iterator<key_type, mapped_type>
  > equal_range(const key_type &x) const {
    return pair<const_iterator, const_iterator>(lower_bound(x), upper_bound(x));
  }

  bool operator==(const multimap<key_type, mapped_type> &x) const {
    return rb_tree::operator==((const rb_tree &) x);
  }
  bool operator<(const multimap<key_type, mapped_type> &x) const {
    return rb_tree::operator<((const rb_tree &) x);
  }

  // Implementation of "rb_tree"'s virtual methods.

private:
  /*virtual*/ bool node_less_than(
    const rb_tree::node_type *x,
    const rb_tree::node_type *y
  ) const {
    return (
      ((const node_type *) x)->value.first <
      ((const node_type *) y)->value.first
    );
  }
  /*virtual*/ bool node_less_than(
    value_pointer            x,
    const rb_tree::node_type *y
  ) const {
    return *(const key_type *) x < ((const node_type *) y)->value.first;
  }
  /*virtual*/ bool node_less_than(
    const rb_tree::node_type *x,
    value_pointer            y
  ) const {
    return ((const node_type *) x)->value.first < *(const key_type *) y;
  }

  /*virtual*/ rb_tree::node_type *copy_node(const rb_tree::node_type *n) const {
    return new node_type(((const node_type *) n)->value);
  }
  /*virtual*/ void delete_node(rb_tree::node_type *n) const {
    delete (node_type *) n;
  }

  typedef void (*key_mapped_printer)(
    ostream           &,
    const key_type    &,
    const mapped_type &
  );
  /*virtual*/ void print_node_value(
    const rb_tree::node_type &n,
    ostream                  &os,
    void                     *closure
  ) const {
    (*(key_mapped_printer *) closure)(
      os,
      ((const node_type &) n).value.first,
      ((const node_type &) n).value.second
    );
  }
  void print(ostream &os, key_mapped_printer np) const {
    rb_tree::print(os, (void *) &np);
  }
  friend ostream &operator<<(
    ostream                               &,
    const multimap<key_type, mapped_type> &
  );

  // Needed by "multimap(iter, iter)".
  static rb_tree::node_type *copy_node2(const rb_tree::node_type *n) {
    return new node_type(((const node_type *) n)->value);
  }
  // Needed by "~multimap()".
  static void delete_node2(rb_tree::node_type *n) {
    delete (node_type *) n;
  }

  friend map__iterator<key_type, mapped_type>;
  friend map__const_iterator<key_type, mapped_type>;
};

/* ------------------------------------------------------------------------- */

/*
 * MUST DEFINE THESE HERE AT THE END OF THIS FILE; FOR ELSE STUPID CFRONT
 * INSTANTIATES IT OUT-OF-LINE!?
 */

template<class key_type, class mapped_type>
inline void map__print_key_mapped(
  ostream           &os,
  const key_type    &key,
  const mapped_type &mapped
)
{
  os << "(" << key << " => " << mapped << ")";
}

template<class key_type, class mapped_type>
inline ostream &
operator<<(ostream &os, const map<key_type, mapped_type> &x)
{
  x.print(os, map__print_key_mapped);
  return os;
}

template<class key_type, class mapped_type>
inline ostream &
operator<<(ostream &os, const multimap<key_type, mapped_type> &x)
{
  x.print(os, map__print_key_mapped);
  return os;
}

/* ------------------------------------------------------------------------- */

#endif /* } */

/* ------------------------------------------------------------------------- */