HPCToolkit
WordSet.cpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // WordSet:
50 //
51 // This table can be used to store a set of words
52 //
53 // WordSetIterator:
54 //
55 // for enumerating entries in a WordSet
56 //
57 // Author: John Mellor-Crummey January 1994
58 //
59 //***************************************************************************
60 
61 //************************** System Include Files ***************************
62 
63 //*************************** User Include Files ****************************
64 
65 #include "WordSet.hpp"
66 
67 //*************************** Forward Declarations **************************
68 
69 //***************************************************************************
70 
71 //**********************************************************************
72 // implementation of class WordSet
73 //**********************************************************************
74 
75 
77 {
78  HashTable::Create(sizeof(unsigned long), 8);
79 }
80 
81 
83 {
84  HashTable::Create(sizeof(unsigned long), 8);
85  *this |= rhs;
86 }
87 
88 
90 {
92 }
93 
94 
95 uint WordSet::HashFunct(const void *entry, const uint size)
96 {
97  return *((unsigned long *) entry) % size;
98 }
99 
100 
101 int WordSet::EntryCompare(const void *e1, const void *e2)
102 {
103  return *((unsigned long *) e1) - *((unsigned long *) e2);
104 }
105 
106 
107 void WordSet::Add(unsigned long entry)
108 {
109  HashTable::AddEntry(&entry);
110 }
111 
112 
113 void WordSet::Delete(unsigned long entry)
114 {
115  HashTable::DeleteEntry(&entry);
116 }
117 
118 
119 int WordSet::IsMember(unsigned long entry) const
120 {
121  void *found = HashTable::QueryEntry(&entry);
122  return (found != 0);
123 }
124 
125 
126 bool WordSet::Intersects(const WordSet &rhs) const
127 {
128  // identify the larger and smaller of the two set operands
129  const WordSet *larger, *smaller;
130  if (NumberOfEntries() > rhs.NumberOfEntries()) {
131  larger = this; smaller = &rhs;
132  } else {
133  larger = &rhs; smaller = this;
134  }
135 
136  // check for intersection by looking up
137  // each element of the smaller set in the larger set.
138  unsigned long *word;
139  for (WordSetIterator words(smaller); (word = words.Current()); words++) {
140  if (larger->IsMember(*word)) return true;
141  }
142  return false;
143 }
144 
145 
147 {
148  if (NumberOfEntries() > 0) {
150  HashTable::Create(sizeof(unsigned long), 8);
151  }
152 }
153 
154 
155 unsigned long WordSet::GetEntryByIndex(unsigned int indx) const
156 {
157  return *((unsigned long *) HashTable::GetEntryByIndex(indx));
158 }
159 
160 
161 int WordSet::operator==(const WordSet &rhs) const
162 {
163  //-----------------------------------------
164  // false if sets have different cardinality
165  //-----------------------------------------
166  if (rhs.NumberOfEntries() != NumberOfEntries()) return 0;
167 
168  //-----------------------------------------
169  // false if some word in rhs is not in lhs
170  //-----------------------------------------
171  WordSetIterator words(&rhs);
172  unsigned long *word;
173  for (; (word = words.Current()); words++) if (IsMember(*word) == 0) return 0;
174 
175  return 1; // equal otherwise
176 }
177 
178 
180 {
181  unsigned int nentries = rhs.NumberOfEntries();
182  for (unsigned int i = 0; i < nentries; i++) {
183  Add(rhs.GetEntryByIndex(i));
184  }
185  return *this;
186 }
187 
189 {
190  if (this != &rhs) {
191  Clear();
192  *this |= rhs;
193  }
194  return *this;
195 }
196 
197 
199 {
200  // identify the larger and smaller of the two set operands
201  const WordSet *larger, *smaller;
202  if (NumberOfEntries() > rhs.NumberOfEntries()) {
203  larger = this; smaller = &rhs;
204  } else {
205  larger = &rhs; smaller = this;
206  }
207 
208  // perform the intersection by looking up
209  // each element of the smaller set in the larger set.
210  // accumulate the intersection in a temporary set "temp"
211  WordSetIterator words(smaller);
212  unsigned long *word;
213  WordSet temp;
214  for (; (word = words.Current()); words++) {
215  if (larger->IsMember(*word)) temp.Add(*word);
216  }
217 
218  // overwrite the current set with the intersection
219  return (*this = temp);
220 }
221 
222 
224 {
225  // perform the difference by looking up
226  // each element of this set in the rhs set.
227  // Delete the members that match
228  unsigned long *word;
229  for (WordSetIterator words(this); (word = words.Current()); words++) {
230  if (rhs.IsMember(*word)) {
231  Delete(*word);
232  }
233  }
234  return *this;
235 }
236 
237 
238 void
239 WordSet::Dump(std::ostream& out, const char* name, const char* indent)
240 {
241  out << indent << "WordSet " << name << " " << this << std::endl;
242  int countThisLine = 0;
243  for (WordSetIterator step(this); step.Current(); step++) {
244  if (countThisLine == 0) {
245  out << indent << " ";
246  }
247  out << *(long *) step.Current() << std::endl;
248  if (++countThisLine == 10) {
249  out << std::endl;
250  countThisLine = 0;
251  }
252  }
253 }
254 
255 //**********************************************************************
256 // implementation of class WordSetIterator
257 //**********************************************************************
258 
260 : HashTableIterator((const HashTable *) theTable)
261 {
262 }
263 
264 
265 unsigned long *WordSetIterator::Current() const
266 {
267  return (unsigned long *) HashTableIterator::Current();
268 }
269 
271  EntryCompareFunctPtr const WSEntryCompare)
272  : HashTableSortedIterator((const HashTable *) theTable,
273  WSEntryCompare), current(0)
274 {
275 }
276 
277 unsigned long *WordSetSortedIterator::Current() const
278 {
280  ((WordSetSortedIterator *)this)->current =
281  *(unsigned long*) HashTableSortedIterator::Current();
282  return &((WordSetSortedIterator *)this)->current;
283  } else return 0;
284 }
285 
unsigned long * Current() const
Definition: WordSet.cpp:277
int(* EntryCompareFunctPtr)(const void *, const void *)
Definition: HashTable.hpp:300
WordSet & operator-=(const WordSet &rhs)
Definition: WordSet.cpp:223
void Create(const uint entrySize, uint initialSize, HashFunctFunctPtr const HashFunctCallback, RehashFunctFunctPtr const RehashFunctCallback, EntryCompareFunctPtr const EntryCompareCallback, EntryCleanupFunctPtr const EntryCleanupCallback)
Definition: HashTable.cpp:139
WordSet & operator|=(const WordSet &rhs)
Definition: WordSet.cpp:179
void Clear()
Definition: WordSet.cpp:146
int operator==(const WordSet &rhs) const
Definition: WordSet.cpp:161
int IsMember(unsigned long entry) const
Definition: WordSet.cpp:119
unsigned long * Current() const
Definition: WordSet.cpp:265
WordSetIterator(const WordSet *theTable)
Definition: WordSet.cpp:259
virtual ~WordSet()
Definition: WordSet.cpp:89
unsigned int uint
Definition: uint.h:124
WordSet & operator &=(const WordSet &rhs)
void Destroy()
Definition: HashTable.cpp:205
void DeleteEntry(void *entry, DeleteEntryFunctPtr const DeleteEntryCallback=0,...)
Definition: HashTable.cpp:296
void Dump()
Definition: HashTable.cpp:475
void AddEntry(void *entry, AddEntryFunctPtr const AddEntryCallback=0,...)
Definition: HashTable.cpp:250
WordSet()
Definition: WordSet.cpp:76
void * Current() const
Definition: HashTable.cpp:932
void Add(unsigned long entry)
Definition: WordSet.cpp:107
bool found
Definition: cct.c:129
uint NumberOfEntries() const
Definition: HashTable.cpp:460
WordSet & operator=(const WordSet &rhs)
Definition: WordSet.cpp:188
WordSetSortedIterator(WordSet const *theTable, EntryCompareFunctPtr const EntryCompare)
Definition: WordSet.cpp:270
int EntryCompare(const void *entry1, const void *entry2)
Definition: WordSet.cpp:101
bool Intersects(const WordSet &rhs) const
Definition: WordSet.cpp:126
void Delete(unsigned long entry)
Definition: WordSet.cpp:113
unsigned long GetEntryByIndex(unsigned int indx) const
Definition: WordSet.cpp:155
void * QueryEntry(const void *entry) const
Definition: HashTable.cpp:396
uint HashFunct(const void *entry, const uint size)
Definition: WordSet.cpp:95
void * GetEntryByIndex(const uint index) const
Definition: HashTable.cpp:444