File: PlainHashMap.hh

package info (click to toggle)
topcom 1.1.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 31,784 kB
  • sloc: cpp: 37,616; sh: 4,262; makefile: 497; ansic: 49
file content (195 lines) | stat: -rw-r--r-- 6,873 bytes parent folder | download | duplicates (2)
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
////////////////////////////////////////////////////////////////////////////////
// 
// PlainHashMap.hh
//    produced: 01/09/97 jr
// last change: 24/01/99 jr
//
////////////////////////////////////////////////////////////////////////////////
#ifndef PLAINHASHMAP_HH
#define PLAINHASHMAP_HH

#include <stdlib.h>
#include <iostream>
#include <ctype.h>
#include <string.h>

#include "Global.hh"
#include "RefCount.hh"
#include "PlainHashTable.hh"

namespace topcom {

  template<class Key, class Data>
  class HashMapData;

  template<class Key, class Data>
  class __hmd_reader;

  template<class Key, class Data>
  inline std::istream& operator>>(std::istream& ist, __hmd_reader<Key, Data>& r);

  template<class Key, class Data>
  inline std::ostream& operator<<(std::ostream& ost, const HashMapData<Key, Data>& hmd);

  template<class Key, class Data>
  class __hmd_reader {
  public:
    Key  key;
    Data data;
  public:
    friend std::istream& operator>><>(std::istream& ist, __hmd_reader& r);
  };

  template<class Key, class Data>
  class HashMapData {
  public:
    typedef Key                      key_type;
    typedef Data                     data_type;
    typedef SmartPtr<Key>            keyptr_type;
    typedef SmartPtr<const Key>      const_keyptr_type;
    typedef SmartPtr<Data>           dataptr_type;
    typedef SmartPtr<const Data>     const_dataptr_type;
    typedef __hmd_reader<Key, Data>  reader;
  private:
    const const_keyptr_type  _keyptr;
    dataptr_type             _dataptr;
  public:
    static data_type    default_data;
    static dataptr_type null;
  public:
    // constructors:
    inline HashMapData() : _keyptr(), _dataptr() {}
    inline HashMapData(const HashMapData& hmd) : _keyptr(hmd._keyptr), _dataptr(hmd._dataptr) {}
    inline HashMapData(const Key& new_key, const Data& new_data) : _keyptr(new_key), _dataptr(new_data) {}
    inline HashMapData(const reader& r) : _keyptr(r.key), _dataptr(r.data) {}
    // destructor:
    inline ~HashMapData() {}
  private:
    // assignment:
    inline HashMapData& operator=(const HashMapData& hmd) {
      if (this == &hmd) {
	return *this;
      }
      (keyptr_type)_keyptr = hmd._keyptr;
      _dataptr = hmd._dataptr;
      return *this;
    }
  public:
    // accessors:
    inline const const_keyptr_type&  keyptr()  const { return _keyptr; }
    inline const dataptr_type&       dataptr() const { return _dataptr; }
    inline       dataptr_type&       dataptr()       { return _dataptr; }
    inline const Key&                key()     const { return *_keyptr; }
    inline const Data&               data()    const { return *_dataptr; }
    inline       Data&               data()          { return *_dataptr; }
    // comparison:
    inline const bool operator==(const HashMapData<Key, Data>& hmd) const { 
      return (key() == hmd.key()) && (data() == hmd.data()); 
    }
    inline const bool operator!=(const HashMapData<Key, Data>& hmd) const { 
      return !((key() == hmd.key()) && (data() == hmd.data())); 
    }
    // iostream:
    inline std::ostream& write(std::ostream& ost) const {
      return ost << *_keyptr << "->" << *_dataptr;
    }
    friend std::ostream& operator<<<>(std::ostream& ost, const HashMapData<Key, Data>& hmd);
  };

  template<class Key, class Data>
  class PlainHashMap : public PlainHashTable< HashMapData<Key, Data> > {
  public:
    typedef          PlainHashTable< HashMapData<Key, Data> >   plainhashmap_data;
    typedef typename HashMapData<Key, Data>::keyptr_type        keyptr_type;
    typedef typename HashMapData<Key, Data>::const_keyptr_type  const_keyptr_type;
    typedef typename HashMapData<Key, Data>::dataptr_type       dataptr_type;
    typedef typename HashMapData<Key, Data>::const_dataptr_type const_dataptr_type;
  public:
    // constructors:
    inline PlainHashMap() : plainhashmap_data() {}
    inline PlainHashMap(const PlainHashMap& phm) : plainhashmap_data(phm) {}
    inline PlainHashMap(const plainhashmap_data& phmd) : plainhashmap_data(phmd) {}
    inline PlainHashMap(const size_type init_bucket_count) : plainhashmap_data(init_bucket_count) {}
    // destructor:
    inline ~PlainHashMap() {}
    // assignment:
    inline PlainHashMap& operator=(const PlainHashMap& phm) {
      plainhashmap_data::operator=(phm);
      return *this;
    }
    // functions:
    inline const dataptr_type& member(const Key& key) const {
      const HashMapData<Key, Data>* hashptr(plainhashmap_data::member(key));
      if (hashptr) return hashptr->dataptr();
      else return HashMapData<Key, Data>::null;
    }
    inline dataptr_type& member(const Key& key) {
      HashMapData<Key, Data>* hashptr(plainhashmap_data::member(key));
      if (hashptr) return hashptr->dataptr();
      else return HashMapData<Key, Data>::null;
    }
    inline dataptr_type& insert(const Key& new_key, const Data& new_data) {
      return plainhashmap_data::insert(HashMapData<Key, Data>(new_key, new_data))->dataptr();
    }
    inline dataptr_type& overwrite(const Key& new_key, const Data& new_data) {
      dataptr_type& dataptr = 
	plainhashmap_data::insert(HashMapData<Key, Data>(new_key, HashMapData<Key, Data>::default_data))->dataptr();
      *dataptr = new_data;
      return dataptr;
    }
    inline const Data& operator[](const Key& key) const { 
      return *member(key); 
    }
    inline Data& operator[](const Key& key) {
      return *insert(key, HashMapData<Key, Data>::default_data); 
    }
  };
 
  template<class Key, class Data>
  typename HashMapData<Key, Data>::data_type HashMapData<Key, Data>::default_data;
 
  template<class Key, class Data>
  typename HashMapData<Key, Data>::dataptr_type HashMapData<Key, Data>::null;

  // friends:

  template<class Key, class Data>
  inline std::istream& operator>>(std::istream& ist, __hmd_reader<Key, Data>& r) {
    char dash;
    char arrow;
    if (!(ist >> std::ws >> r.key)) {
#ifdef READ_DEBUG
      std::cerr << "std::istream& operator>>(std::istream&, HashMapData<Key, Data>::reader&): "
		<< "key not found." << std::endl;
#endif
      ist.clear(std::ios::failbit);
      return ist;
    }
    if (!(ist >> std::ws >> dash >> arrow)) {
#ifdef READ_DEBUG
      std::cerr << "std::istream& operator>>(std::istream&, HashMapData<Key, Data>::reader&): "
		<< "`->' not found." << std::endl;
#endif
      ist.clear(std::ios::failbit);
      return ist;
    }
    if (!(ist >> std::ws >> r.data)) {
#ifdef READ_DEBUG
      std::cerr << "std::istream& operator>>(std::istream&, HashMapData<Key, Data>::reader&): "
		<< "data not found." << std::endl;
#endif
      ist.clear(std::ios::failbit);
      return ist;
    }
    return ist;
  }

  template<class Key, class Data>
  inline std::ostream& operator<<(std::ostream& ost, const HashMapData<Key, Data>& hmd) {
    return hmd.write(ost);
  }

}; // namespace topcom

#endif
// eof PlainHashMap.hh