RexxHashTable.hpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.oorexx.org/license.html */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /******************************************************************************/
39 /* REXX Kernel RexxHashTable.hpp */
40 /* */
41 /* Primitive Hash Table Class Definitions */
42 /* */
43 /******************************************************************************/
44 #ifndef Included_RexxHash
45 #define Included_RexxHash
46 
47 
48 /* The type for the reference links */
49 typedef size_t HashLink;
50 
51  typedef struct tabentry {
52  RexxObject *value; /* item value object */
53  RexxObject *index; /* item index object */
54  HashLink next; /* next item in overflow bucket */
56 
58  class RexxTable;
59 
61  public:
62  enum
63  {
68  };
69 
70  inline void * operator new(size_t size, void *objectPtr) { return objectPtr; };
71  inline void operator delete(void *) { ; }
72  inline void operator delete(void *, void *) { ; }
73 
74  inline RexxHashTable(RESTORETYPE restoreType) { ; };
75  inline RexxHashTable() { ; }
76 
77  void live(size_t);
78  void liveGeneral(int reason);
79  void flatten(RexxEnvelope *);
80  RexxArray * makeArray();
81  void empty();
82  bool isEmpty();
83  size_t items();
84  void emptySlot(HashLink);
85 
86  HashLink next(HashLink position);
87  RexxObject *value(HashLink position);
88  RexxObject *index(HashLink position);
95  size_t countAll(RexxObject *key);
96  RexxObject *get(RexxObject *key);
108  size_t totalEntries();
109  HashLink first();
116  RexxArray *allItems();
128  void reMerge(RexxHashTable *target);
129  void primitiveMerge(RexxHashTable *target);
133  inline size_t mainSlotsSize() { return this->size; };
134  inline size_t totalSlotsSize() { return this->size * 2; };
135  inline bool available(HashLink position) { return (size_t)position < this->totalSlotsSize(); };
136  inline HashLink hashIndex(RexxObject *obj) { return (HashLink)(obj->hash() % this->mainSlotsSize()); }
137  // NB: Ideally, hashPrimitiveIndex() would be best served by using the identityHash(). Unfortunately,
138  // the identity hash value is derived directly from the object reference. This means that objects that
139  // are in the saved image (or restored as part of saved programs) will have different identity hashes before
140  // and after the store, which will cause hash table lookup failures. We'll use whatever value is stored
141  // in the hashvalue field.
142  inline HashLink hashPrimitiveIndex(RexxObject *obj) { return (HashLink)(obj->getHashValue() % this->mainSlotsSize()); }
143  inline HashLink hashStringIndex(RexxObject *obj) { return (HashLink)(obj->hash() % this->mainSlotsSize()); }
144 
145  static RexxTable *newInstance(size_t, size_t, size_t);
146  static RexxHashTable *newInstance(size_t);
147 
148  protected:
149 
150  size_t size; // size of the hash table
151  HashLink free; /* first free element */
152  TABENTRY entries[1]; /* hash table entries */
153  };
154 
155 
156 inline RexxTable *new_hashCollection(size_t s, size_t s2, size_t t) { return RexxHashTable::newInstance(s, s2, t); }
157 inline RexxHashTable *new_hashtab(size_t s) { return RexxHashTable::newInstance(s); }
158 
159  #endif
RESTORETYPE
Definition: ObjectClass.hpp:82
struct tabentry TABENTRY
size_t HashLink
RexxTable * new_hashCollection(size_t s, size_t s2, size_t t)
RexxHashTable * new_hashtab(size_t s)
RexxSupplier * supplier()
size_t countAll(RexxObject *key)
RexxHashTable * add(RexxObject *value, RexxObject *key)
size_t mainSlotsSize()
RexxObject * primitiveGet(RexxObject *key)
RexxObject * primitiveRemoveItem(RexxObject *value, RexxObject *key)
RexxObject * primitiveRemove(RexxObject *key)
RexxObject * getIndex(RexxObject *value)
RexxHashTable * primitiveAdd(RexxObject *value, RexxObject *key)
RexxObject * merge(RexxHashTableCollection *target)
RexxArray * getAll(RexxObject *key)
RexxArray * primitiveGetAll(RexxObject *key)
RexxHashTable * stringPut(RexxObject *value, RexxString *key)
void reMerge(RexxHashTable *target)
RexxArray * makeArray()
RexxObject * index(HashLink position)
RexxHashTable * putNodupe(RexxObject *value, RexxObject *key)
void primitiveMerge(RexxHashTable *target)
void emptySlot(HashLink)
RexxObject * nextItem(RexxObject *, RexxObject *)
HashLink hashIndex(RexxObject *obj)
RexxObject * primitiveHasItem(RexxObject *, RexxObject *)
RexxObject * stringMerge(RexxHashTable *target)
RexxHashTable * primitivePut(RexxObject *value, RexxObject *key)
size_t totalSlotsSize()
RexxArray * allItems()
HashLink first()
RexxHashTable * stringAdd(RexxObject *value, RexxString *key)
void liveGeneral(int reason)
RexxHashTable * put(RexxObject *value, RexxObject *key)
HashLink hashPrimitiveIndex(RexxObject *obj)
TABENTRY entries[1]
RexxArray * uniqueIndexes()
RexxObject * hasItem(RexxObject *value, RexxObject *key)
RexxHashTable(RESTORETYPE restoreType)
RexxObject * mergeItem(RexxObject *value, RexxObject *index)
RexxObject * primitiveNextItem(RexxObject *, RexxObject *)
RexxObject * remove(RexxObject *key)
RexxObject * removeItem(RexxObject *value, RexxObject *key)
RexxArray * allIndexes()
HashLink hashStringIndex(RexxObject *obj)
static RexxTable * newInstance(size_t, size_t, size_t)
RexxObject * get(RexxObject *key)
RexxObject * primitiveGetIndex(RexxObject *value)
RexxObject * value(HashLink position)
RexxArray * stringGetAll(RexxString *key)
bool available(HashLink position)
void live(size_t)
RexxHashTable * insert(RexxObject *value, RexxObject *index, HashLink position, int type)
RexxObject * stringGet(RexxString *key)
RexxObject * removeAll(RexxObject *key)
RexxHashTable * reHash()
size_t totalEntries()
HashLink next(HashLink position)
RexxArray * allIndex(RexxObject *key)
void flatten(RexxEnvelope *)
RexxObject * replace(RexxObject *value, HashLink position)
virtual HashCode getHashValue()
HashCode hash()
int type
Definition: cmdparse.cpp:1888
HashLink next
RexxObject * value
RexxObject * index