/usr/include/crystalspace-2.0/csutil/set.h is in libcrystalspace-dev 2.0+dfsg-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*
Copyright (C) 2003 by Mat Sutcliffe <oktal@gmx.co.uk>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __CS_UTIL_SET_H__
#define __CS_UTIL_SET_H__
#include "csutil/hash.h"
/**\file
* Basic set for objects.
*/
/**\addtogroup util_containers
* @{ */
/**
* This class implements a basic set for objects.
* You can basically use this to test for the occurrence
* of some object quickly.
*/
template <class T, class Allocator = CS::Memory::AllocatorMalloc>
class csSet
{
public:
typedef csHash<bool, T, Allocator> HashType;
private:
typedef typename HashType::ConstGlobalIterator ParentIter;
HashType map;
public:
/* Unfortunately, MSVC6 barfs if we derive this from ParentIter. */
/// An iterator class for the set.
class GlobalIterator
{
protected:
ParentIter iter;
GlobalIterator (const csSet<T>* s) : iter(s->map.GetIterator()) {}
public:
friend class csSet<T>;
GlobalIterator () : iter() {}
GlobalIterator (const GlobalIterator& o) : iter(o.iter) {}
GlobalIterator& operator=(const GlobalIterator& o)
{ iter = o.iter; return *this; }
/// Returns a boolean indicating whether or not the set has more elements.
bool HasNext () const
{ return iter.HasNext(); }
/// Get the next element's value.
T Next()
{
T key;
iter.Next(key);
return key;
}
};
friend class GlobalIterator;
/**
* Construct a new empty set.
* \a size, \a grow_rate, and \a max_size allow fine-tuning of how the set
* manages its internal allocations.
*/
csSet (int size = 23, int grow_rate = 5, int max_size = 20000)
: map (size, grow_rate, max_size)
{
}
/**
* Add an object to this set.
* This will do nothing if the object is already present.
*/
void Add (const T& object)
{
if (!Contains (object))
AddNoTest (object);
}
/**
* Add an object to this set.
* This function does not test if the object is already
* there. This is used for efficiency reasons. But use
* with care!
*/
void AddNoTest (const T& object)
{
map.Put (object, true);
}
/**
* Test if an object is in this set.
*/
bool Contains (const T& object) const
{
return map.Contains (object);
}
/**
* Test if an object is in this set.
* \remarks This is rigidly equivalent to Contains(object), but may be
* considered more idiomatic by some.
*/
bool In (const T& object) const
{ return Contains(object); }
/**
* Delete all elements in the set.
*/
void DeleteAll ()
{
map.DeleteAll ();
}
/// Delete all elements in the set. (Idiomatic alias for DeleteAll().)
void Empty() { DeleteAll(); }
/**
* Delete an object from the set. This function
* does nothing if the object is not in the set.
* Return true if the object was present.
*/
bool Delete (const T& object)
{
return map.Delete (object, true);
}
/**
* Calculate the union of two sets and put the result in
* this set.
*/
void Union (const csSet& otherSet)
{
GlobalIterator it = otherSet.GetIterator ();
while (it.HasNext ())
Add (it.Next ());
}
/**
* Calculate the union of two sets and put the result in
* a new set.
*/
inline friend csSet Union (const csSet& s1, const csSet& s2)
{
csSet un (s1);
un.Union (s2);
return un;
}
/**
* Test if this set intersects with another set (i.e. they have
* common elements).
*/
bool TestIntersect (const csSet& other) const
{
if (GetSize () < other.GetSize ())
{
// Call TestIntersect() on the set with most elements
// so that we iterate over the set with fewest elements.
return other.TestIntersect (*this);
}
GlobalIterator it = other.GetIterator ();
while (it.HasNext ())
{
if (Contains (it.Next ())) return true;
}
return false;
}
/**
* Calculate the intersection of two sets and put the result
* in a new set.
*/
inline friend csSet Intersect (const csSet& s1, const csSet& s2)
{
csSet intersection;
GlobalIterator it = s1.GetIterator ();
while (it.HasNext ())
{
T item = it.Next ();
if (s2.Contains (item))
intersection.AddNoTest (item);
}
return intersection;
}
/**
* Subtract a set from this set and put the result in this set.
*/
void Subtract (const csSet& otherSet)
{
GlobalIterator it = otherSet.GetIterator ();
while (it.HasNext ())
Delete (it.Next ());
}
/**
* Subtract two sets and return the result in a new set.
*/
inline friend csSet Subtract (const csSet& s1, const csSet& s2)
{
csSet subtraction;
GlobalIterator it = s1.GetIterator ();
while (it.HasNext ())
{
T item = it.Next ();
if (!s2.Contains (item))
subtraction.AddNoTest (item);
}
return subtraction;
}
/// Get the number of elements in the set.
size_t GetSize () const
{
return map.GetSize ();
}
/**
* Return true if the set is empty.
* \remarks Rigidly equivalent to <tt>return GetSize() == 0</tt>, but more
* idiomatic.
*/
bool IsEmpty() const
{
return GetSize() == 0;
}
/**
* Return an iterator for the set which iterates over all elements.
* \warning Modifying the set while you have open iterators will cause
* undefined behaviour.
*/
GlobalIterator GetIterator () const
{
return GlobalIterator(this);
}
};
/** @} */
#endif // __CS_UTIL_SET_H__
|