File: RawHashTable.h

package info (click to toggle)
trafficserver 6.2.0-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 45,456 kB
  • sloc: cpp: 271,894; ansic: 80,740; sh: 6,032; makefile: 3,364; python: 2,135; perl: 2,040; java: 277; lex: 128; sql: 94; yacc: 68; sed: 8
file content (383 lines) | stat: -rw-r--r-- 11,389 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
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
/** @file

  C++ wrapper around libts hash tables

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  @section details Details

  These C++ RawHashTables are a C++ wrapper around libts hash tables.
  They expose an interface very analogous to ink_hash_table, for better
  or for worse. See HashTable for a more C++-oriented hash table.

*/

#ifndef _RawHashTable_h_
#define _RawHashTable_h_

#include "ts/ink_apidefs.h"
#include "ts/ink_hash_table.h"

//////////////////////////////////////////////////////////////////////////////
//
//      Constants and Type Definitions
//
//////////////////////////////////////////////////////////////////////////////

typedef enum {
  RawHashTable_KeyType_String = InkHashTableKeyType_String,
  RawHashTable_KeyType_Word   = InkHashTableKeyType_Word
} RawHashTable_KeyType;

typedef InkHashTableKey RawHashTable_Key;
typedef InkHashTableValue RawHashTable_Value;
typedef InkHashTableEntry RawHashTable_Binding;
typedef InkHashTableIteratorState RawHashTable_IteratorState;

//////////////////////////////////////////////////////////////////////////////
//
//      The RawHashTable Class
//
//////////////////////////////////////////////////////////////////////////////

class RawHashTable
{
private:
  InkHashTable *ht;
  RawHashTable_KeyType key_type;
  bool deallocate_values_on_destruct;

public:
  inkcoreapi RawHashTable(RawHashTable_KeyType key_type, bool deallocate_values_on_destruct = false);
  virtual ~RawHashTable();

  //
  // these are the simplest accessor functions
  //

  bool getValue(RawHashTable_Key key, RawHashTable_Value *value_ptr);
  void setValue(RawHashTable_Key key, RawHashTable_Value value_ptr);
  bool isBound(RawHashTable_Key key);
  bool unbindKey(RawHashTable_Key key);
  void replaceString(char *key, char *string);

  //
  // these functions allow you to manipulate the (key,value) bindings directly
  //

  RawHashTable_Binding *getCurrentBinding(RawHashTable_Key key);
  RawHashTable_Binding *getOrCreateBinding(RawHashTable_Key key, bool *was_new = NULL);

  void setBindingValue(RawHashTable_Binding *binding, RawHashTable_Value value);
  RawHashTable_Key getKeyFromBinding(RawHashTable_Binding *binding);
  RawHashTable_Value getValueFromBinding(RawHashTable_Binding *binding);

  //
  // these functions allow you to iterate through RawHashTable bindings
  //

  RawHashTable_Binding *firstBinding(RawHashTable_IteratorState *state_ptr);
  RawHashTable_Binding *nextBinding(RawHashTable_IteratorState *state_ptr);
};

//////////////////////////////////////////////////////////////////////////////
//
//      Inline Methods
//
//////////////////////////////////////////////////////////////////////////////

/**
  This routine gets the value associated with key. If the key has a
  binding, the value is stored through value_ptr and true is returned. If
  the key DOES NOT have a binding, false is returned.

*/
inline bool
RawHashTable::getValue(RawHashTable_Key key, RawHashTable_Value *value_ptr)
{
  int is_bound;

  is_bound = ink_hash_table_lookup(ht, (InkHashTableKey)key, (InkHashTableValue *)value_ptr);
  return (is_bound ? true : false);
}

/**
  This routine sets the value associated with key to the value. If
  a value is previously bound to the key, the previous value is left
  dangling. The caller is responsible to freeing any previous binding
  values needing freeing before calling setValue.

  If the key has a binding, the value is stored through value_ptr and
  true is returned. If the key DOES NOT have a binding, false is returned.

*/
inline void
RawHashTable::setValue(RawHashTable_Key key, RawHashTable_Value value)
{
  ink_hash_table_insert(ht, (InkHashTableKey)key, (InkHashTableValue)value);
}

/**
  This routine sets the value associated with key to the value pointed to
  by value_ptr. If a value is previously bound to the key, the previous
  value is left dangling. The caller is responsible to freeing any
  previous value before setValue.

  If the key has a binding, the value is stored through value_ptr and
  true is returned. If the key DOES NOT have a binding, false is returned.

*/
inline bool
RawHashTable::isBound(RawHashTable_Key key)
{
  int status = ink_hash_table_isbound(ht, (InkHashTableKey)key);
  return (status ? true : false);
}

/**
  This routine removes any association for key from the hash table. If
  data was bound to key, the binding will be deleted, but the value will
  not be deallocated. The caller is responsible to freeing any previous
  value before unbindKey.

  @return true if the key was previously bound, false otherwise.

*/
inline bool
RawHashTable::unbindKey(RawHashTable_Key key)
{
  int status;

  status = ink_hash_table_delete(ht, (InkHashTableKey)key);
  return (status ? true : false);
}

/**
  This rather specialized routine binds a malloc-allocated string value
  to the key, freeing any previous value. The key must be a string,
  and the hash table must have been constructed as having key_type
  RawHashTable_KeyType_String.

*/
inline void
RawHashTable::replaceString(char *key, char *string)
{
  //    if (key_type != RawHashTable_KeyType_String)
  //    {
  //      throw BadKeyType();
  //    }

  ink_hash_table_replace_string(ht, key, string);
}

/**
  This function looks up a binding for key in the hash table, and returns
  a pointer to the binding data structure directly inside the hash table,
  or NULL if there is no binding.

*/
inline RawHashTable_Binding *
RawHashTable::getCurrentBinding(RawHashTable_Key key)
{
  InkHashTableEntry *he_ptr;

  he_ptr = ink_hash_table_lookup_entry(ht, (InkHashTableKey)key);
  return ((RawHashTable_Binding *)he_ptr);
}

/**
  This function looks up a binding for key in the hash table, creates
  a binding if one doesn't exist, and returns a pointer to the binding
  data structure directly inside the hash table.

  If was_new is not NULL, true is stored through was_new. If no binding
  previously existed, false is stored through was_new if a binding
  previously existed.

*/
inline RawHashTable_Binding *
RawHashTable::getOrCreateBinding(RawHashTable_Key key, bool *was_new)
{
  int _was_new;
  InkHashTableEntry *he_ptr;

  he_ptr   = ink_hash_table_get_entry(ht, (InkHashTableKey)key, &_was_new);
  *was_new = (_was_new ? true : false);
  return ((RawHashTable_Binding *)he_ptr);
}

/**
  This function looks up a binding for key in the hash table, creates
  a binding if one doesn't exist, and returns a pointer to the binding
  data structure directly inside the hash table.

  If was_new is not NULL, true is stored through was_new. If no binding
  previously existed, false is stored through was_new if a binding
  previously existed.

*/
inline void
RawHashTable::setBindingValue(RawHashTable_Binding *binding, RawHashTable_Value value)
{
  ink_hash_table_set_entry(ht, (InkHashTableEntry *)binding, (InkHashTableValue)value);
}

/**
  This function takes a binding and extracts the key.

*/
inline RawHashTable_Key
RawHashTable::getKeyFromBinding(RawHashTable_Binding *binding)
{
  InkHashTableKey ht_key;

  ht_key = ink_hash_table_entry_key(ht, (InkHashTableEntry *)binding);
  return ((RawHashTable_Key)ht_key);
}

/**
  This function takes a binding and extracts the value.

*/
inline RawHashTable_Value
RawHashTable::getValueFromBinding(RawHashTable_Binding *binding)
{
  InkHashTableValue ht_value;

  ht_value = ink_hash_table_entry_value(ht, (InkHashTableEntry *)binding);
  return ((RawHashTable_Value)ht_value);
}

/**
  This function takes a hash table, initializes an interator data
  structure to point to the first binding in the hash table, and returns
  the first binding, or NULL if there are none.

*/
inline RawHashTable_Binding *
RawHashTable::firstBinding(RawHashTable_IteratorState *state_ptr)
{
  InkHashTableEntry *he_ptr;

  he_ptr = ink_hash_table_iterator_first(ht, (InkHashTableIteratorState *)state_ptr);
  return ((RawHashTable_Binding *)he_ptr);
}

inline RawHashTable::RawHashTable(RawHashTable_KeyType akey_type, bool adeallocate_values_on_destruct)
{
  RawHashTable::key_type                      = akey_type;
  RawHashTable::deallocate_values_on_destruct = adeallocate_values_on_destruct;
  ht                                          = ink_hash_table_create((InkHashTableKeyType)key_type);
}

inline RawHashTable::~RawHashTable()
{
  if (deallocate_values_on_destruct)
    ink_hash_table_destroy_and_free_values(ht);
  else
    ink_hash_table_destroy(ht);
}

/**
  This function takes a hash table and a pointer to iterator state,
  and advances to the next binding in the hash table, if any. If there
  in a next binding, a pointer to the binding is returned, else NULL.

*/
inline RawHashTable_Binding *
RawHashTable::nextBinding(RawHashTable_IteratorState *state_ptr)
{
  InkHashTableEntry *he_ptr;

  he_ptr = ink_hash_table_iterator_next(ht, (InkHashTableIteratorState *)state_ptr);
  return ((RawHashTable_Binding *)he_ptr);
}

//////////////////////////////////////////////////////////////////////////////
//
//      The RawHashTableIter Class
//
//////////////////////////////////////////////////////////////////////////////

class RawHashTableIter
{
public:
  RawHashTableIter(RawHashTable &ht);
  ~RawHashTableIter();

  RawHashTable_Value &operator++();       // get next
  RawHashTable_Value &operator()() const; // get current
  operator const void *() const;          // is valid

  RawHashTable_Value &value() const; // get current value
  const char *key() const;           // get current key

private:
  RawHashTable &m_ht;
  RawHashTable_Binding *m_currentBinding;
  RawHashTable_IteratorState m_hashIterState;
};

//////////////////////////////////////////////////////////////////////////////
//
//      Inline Methods
//
//////////////////////////////////////////////////////////////////////////////

inline RawHashTable_Value &
RawHashTableIter::operator()() const
{
  return (m_currentBinding->clientData);
}

inline RawHashTable_Value &RawHashTableIter::operator++()
{
  m_currentBinding = m_ht.nextBinding(&m_hashIterState);
  return (m_currentBinding->clientData);
}

inline RawHashTableIter::operator const void *() const
{
  return ((m_currentBinding != 0) ? this : 0);
}

inline RawHashTable_Value &
RawHashTableIter::value() const
{
  return (m_currentBinding->clientData);
}

inline const char *
RawHashTableIter::key() const
{
  return (m_currentBinding->key.string);
}

inline RawHashTableIter::RawHashTableIter(RawHashTable &ht) : m_ht(ht), m_currentBinding(0)
{
  m_currentBinding = m_ht.firstBinding(&m_hashIterState);
  return;
}

inline RawHashTableIter::~RawHashTableIter()
{
  return;
}

#endif /*_RawHashTable_h_*/