39#ifndef _CHOMP_STRUCT_HASHSETS_H_
40#define _CHOMP_STRUCT_HASHSETS_H_
64template <
class element,
class hashing>
66template <
class domelement,
class imgelement>
118 out <<
"hashing: " << s. hashhits <<
" hits, avg " <<
119 ((s. hashhits + s. hashmisses) / s. hashhits) <<
"." <<
120 ((s. hashhits + s. hashmisses) * 10 / s. hashhits) % 10 <<
121 " attempts (" << s. rehashcount <<
" times rehashed)";
132template <
class element>
153template <
class element>
183template <
class element,
class hashkeys = HashingGlobal<element> >
219 bool check (
const element &e)
const;
231 const element &
top ()
const;
343template <
class element,
class hashkeys>
346template <
class element,
class hashkeys>
349template <
class element,
class hashkeys>
354template <
class element,
class hashkeys>
356 nelem (0), tab ((initialsize > 0) ? initialsize :
357 int_t (InitTabSize)),
359 hashsize (initialsize + (initialsize >> 2) + InitHashSize),
360 hashcleared (0), hashtable (hashsize)
370template <
class element,
class hashkeys>
372 nelem (s. nelem), tab (s. tab),
373 hashing (s. hashing), hashsize (s. hashsize),
374 hashcleared (s. hashcleared), hashtable (s. hashtable)
383template <
class element,
class hashkeys>
394 hashing = s. hashing;
395 hashsize = s. hashsize;
396 hashcleared = s. hashcleared;
397 hashtable = s. hashtable;
402template <
class element,
class hashkeys>
407 sout <<
" " << *stat <<
'\n';
413template <
class element,
class hashkeys>
418 int_t pos = hashfind (e);
419 return (hashtable (pos));
423 for (
int_t i = 0; i < nelem; ++ i)
432template <
class element,
class hashkeys>
435 return ((n >= 0) && (n < nelem));
438template <
class element,
class hashkeys>
441 return (getnumber (e) >= 0);
444template <
class element,
class hashkeys>
447 if ((n < 0) || (n >= nelem))
448 throw "Trying to get an element out of range.";
452template <
class element,
class hashkeys>
458template <
class element,
class hashkeys>
461 return get (nelem - 1);
464template <
class element,
class hashkeys>
467 return get (nelem - 1);
470template <
class element,
class hashkeys>
475 for (
int_t i = 0; i < nelem; ++ i)
481 if (nelem >= StartHashingSize)
490 if (hashsize - hashcleared < nelem + (nelem >> 1) + 19)
495 int_t pos = hashfind (e, &addpos);
498 if (hashtable (pos) >= 0)
499 return hashtable (pos);
504 hashtable [pos] = nelem;
509template <
class element,
class hashkeys>
515template <
class element,
class hashkeys>
521template <
class element,
class hashkeys>
526 for (
int_t i = 0; i < nelem; ++ i)
530 tab [i] = tab (-- nelem);
538 int_t pos = hashfind (e);
541 if (hashtable (pos) < 0)
543 int_t n = hashtable (pos);
546 hashtable [pos] = -2;
554 pos = hashfind (tab (nelem));
556 tab [n] = tab (nelem);
560 if (nelem < StartHashingSize / 2)
566 else if (hashcleared > nelem + 19)
572template <
class element,
class hashkeys>
575 if ((n < 0) || (n >= nelem))
581 tab [n] = tab (nelem);
584 return remove (tab (n));
587template <
class element,
class hashkeys>
591 throw "Trying to remove an element from an empty set.";
592 return removenum (nelem - 1);
595template <
class element,
class hashkeys>
598 int_t n = s. size ();
599 for (
int_t i = 0; i < n; ++ i)
604template <
class element,
class hashkeys>
607 if (
this -> size () < s. size ())
609 for (
int_t i = 0; i <
this -> size (); ++ i)
611 if (s. check ((*
this) [i]))
612 this -> removenum (i --);
617 int_t n = s. size ();
618 for (
int_t i = 0; i < n; ++ i)
624template <
class element,
class hashkeys>
630template <
class element,
class hashkeys>
636template <
class element,
class hashkeys>
641 tab.
swap (other. tab);
644 std::swap (hashcleared, other. hashcleared);
645 hashtable.
swap (other. hashtable);
649template <
class element,
class hashkeys>
653 if (
this -> nelem != other. nelem)
655 for (
int_t i = 0; i < nelem; ++ i)
657 if (!other. check (
this -> tab [i]))
663template <
class element,
class hashkeys>
667 int_t size1 = s1. size ();
668 int_t size2 = s2. size ();
672 for (
int_t i = 0; i < size1; ++ i)
674 const element &elem = s1. tab [i];
675 if (s2. check (elem))
681 for (
int_t i = 0; i < size2; ++ i)
683 const element &elem = s2. tab [i];
684 if (s1. check (elem))
691template <
class element,
class hashkeys>
695 throw "Hashing is turned off.";
700 int_t pos = key % hashsize;
706 ++ (stat -> hashhits);
711 while ((number = hashtable (pos)) != -1)
713 if ((number >= 0) && (e == tab (number)))
715 if (addpos && (*addpos < 0) && (number == -2))
722 add = key % (hashsize - 1) + 1;
728 ++ (stat -> hashmisses);
736template <
class element,
class hashkeys>
740 ++ (stat -> rehashcount);
743 if ((newsize < (nelem << 1) + InitHashSize) ||
744 (newsize > (nelem << 2) + InitHashSize))
745 newsize = (nelem << 1) + nelem + InitHashSize;
753 if ((x < 0) && (newsize >= 16384))
754 throw "Set too large for this 16-bit program.";
757 if (newsize < hashsize)
768 hashtable. fill (-1, hashsize);
769 for (
int_t i = 0; i < nelem; ++ i)
771 int_t pos = hashfind (tab (i));
772 if (hashtable (pos) != -1)
773 throw "A repeated element in a set!";
780template <
class element,
class hashkeys>
797template <
class element,
class hashkeys>
813#define SMALL_SIZE true
820#define LARGE_SIZE false
825template <
class stream,
class element,
class hashkeys>
831 int_t n = s. size ();
832 for (
int_t i = 0; i < n; ++ i)
833 out << (i ?
" " :
"") << s [i];
838 int_t n = s. size ();
841 out <<
"; " << n << ((n == 1) ?
" element." :
842 " elements.") <<
'\n';
844 if (s. stat && s. stat -> hashhits)
845 out <<
';' << *(s. stat) <<
'\n';
846 for (
int_t i = 0; i < n; ++ i)
847 out << s [i] <<
'\n';
854template <
class element,
class hashkeys>
863template <
class stream,
class element,
class hashkeys>
876 throw "An opening parenthesis expected in a set.";
881 while (in. peek () != closing)
891 if (in. peek () ==
',')
907template <
class element,
class hashkeys>
914template <
class element,
class hashkeys>
923template <
class element,
class hashkeys>
943template <
class domelement,
class imgelement>
969 (
const domelement &x)
const;
1012template <
class domelement,
class imgelement>
1014 domain (0, bequiet), images ()
1019template <
class domelement,
class imgelement>
1025template <
class domelement,
class imgelement>
1028 if ((n < 0) || (n >= domain. size ()))
1029 throw "Domain element number out of range.";
1033template <
class domelement,
class imgelement>
1040template <
class domelement,
class imgelement>
1044 if ((n < 0) || (n >= domain. size ()))
1045 throw "Domain element number out of range.";
1049template <
class domelement,
class imgelement>
1052 (
const domelement &q)
const
1054 int_t n = domain. getnumber (q);
1056 throw "Element not in the domain.";
1060template <
class domelement,
class imgelement>
1064 if ((n < 0) || (n >= domain. size ()))
1065 throw "Domain element number out of range.";
1069template <
class domelement,
class imgelement>
1072 (
const domelement &q)
1074 int_t n = domain. add (q);
1078template <
class domelement,
class imgelement>
1081 return domain. size ();
1084template <
class domelement,
class imgelement>
1089 if ((n < 0) || (n >= domain. size ()))
1091 domain. removenum (n);
1092 if (n != domain. size ())
1093 images [n] = images [domain. size ()];
1095 images [domain. size ()] = empty;
1099template <
class domelement,
class imgelement>
1102 return removenum (domain. getnumber (x));
1105template <
class domelement,
class imgelement>
1109 int_t n = x. size ();
1110 for (
int_t i = 0; i < n; ++ i)
1115template <
class domelement,
class imgelement>
1119 domain.
swap (other. domain);
1120 images.
swap (other. images);
1127template <
class domelement,
class imgelement>
1131 int_t n = m. getdomain (). size ();
1132 for (
int_t i = 0; i < n; ++ i)
1140template <
class domelement,
class imgelement>
1144 for (
int_t i = 0; i < m. getdomain (). size (); ++ i)
1146 const domelement &e = m. get (i);
1148 int_t n = f. size ();
1149 for (
int_t j = 0; j < n; ++ j)
1150 graph. add (e * f [j]);
1158template <
class domelement,
class imgelement>
1163 while (in. peek () != EOF)
1173 while (in. peek () ==
'-')
1175 if (in. peek () ==
'>')
1182 while (in. peek () != closing)
1189 if (in. peek () ==
',')
1204template <
class domelement,
class imgelement>
1209 while (in. peek () != EOF)
1218 while (in. peek () ==
'-')
1220 if (in. peek () ==
'>')
1232template <
class domelement,
class imgelement>
1236 if (dom1. empty () && dom2. empty ())
1238 sout <<
"Warning: The domain of the map is empty.\n";
1243 while (in. peek () != EOF)
1252 while (in. peek () ==
'-')
1254 if (in. peek () ==
'>')
1258 if (dom1. check (e) || dom2. check (e))
1265 while (in. peek () != closing)
1272 if (in. peek () ==
',')
1288template <
class domelement,
class imgelement>
1295 sout <<
"Warning: The domain of the map is empty.\n";
1300 while (in. peek () != EOF)
1309 while (in. peek () ==
'-')
1311 if (in. peek () ==
'>')
1320 int_t n = x. size ();
1321 for (
int_t i = 0; i < n; ++ i)
1323 if (img. check (x [i]))
1332 while (in. peek () != closing)
1339 if (in. peek () ==
',')
1355template <
class domelement,
class imgelement>
1370template <
class domelement,
class imgelement>
1374 int_t n = m. getdomain (). size ();
1375 for (
int_t i = 0; i < n; ++ i)
1377 out << m. get (i) <<
" -> ";
1384template <
class domelement,
class imgelement>
1388 while (in. peek () != EOF)
1397 while (in. peek () ==
'-')
1399 if (in. peek () ==
'>')
A hashing method using globally defined functions that calculate the two required hashing keys.
static int_t hashkey2(const element &x)
Returns the second hashing key of the element.
static int_t hashkey1(const element &x)
Returns the first hashing key of the element.
A hashing method using member functions that calculate the two required hashing keys.
static int_t hashkey1(const element &x)
Returns the first hashing key of the element.
static int_t hashkey2(const element &x)
Returns the second hashing key of the element.
This is a template for a set of objects of the given type.
bool operator==(const hashedset< element, hashkeys > &other) const
Verifies whether two hashed sets have the same elements.
bool checknum(int_t n) const
Checks if the given number is an index of some element in the set, that is, if the number is non-nega...
const element & get(int_t n) const
Returns the element with the given number from the set.
hashkeys hashing_method
The type of hashing.
hashedset(int_t initialsize=0, int bequiet=1)
The default constructor for creating an empty set of objects.
const element & front() const
Returns the last element in the set.
int_t nelem
The number of elements stored in the set.
int hashing
Is the hashing table in use?
static const int_t StartHashingSize
The minimal number of elements above which hashing table is actually used.
void rehash(int_t newsize=0)
Replace the old hashing table with a new one.
void intersection(const hashedset< element, hashkeys > &s1, const hashedset< element, hashkeys > &s2)
Computes the intersection of two hashed sets and adds the result to the current set.
int removenum(int_t n)
Returns an element with the given number from the set.
static const int_t InitTabSize
The initial size of a table in a hashed set.
int_t hashfind(const element &e, int_t *addpos=NULL) const
Finds the position in the hashing table at which the number of the given element should be.
int remove(const element &e)
Removes the given element from the set.
bool empty() const
Returns true if the set is empty. Otherwise returns false.
hashedset & operator=(const hashedset< element, hashkeys > &s)
The assignment operator.
static int_t rounduptoprime(int_t n)
Rounds up the given number to a prime number.
element value_type
The type of elements stored in the set.
void swap(hashedset< element, hashkeys > &other)
Swaps two hashed sets by swapping their internal data.
multitable< element > tab
The table of these elements.
multitable< int_t > hashtable
The hashing table.
hashedset(const hashedset< element, hashkeys > &s)
The copy constructor.
static bool numberisprime(const int_t &n)
Verifies whether the given number is prime or not.
hashstat * stat
Hashed set statistics.
static const int_t InitHashSize
The default initial size of a hashing table.
hashedset< element, hashkeys > & remove(const hashedset< element, hashkeys > &s)
Removes an entire set of elements from the set.
int_t hashcleared
The number of cleared entries in the hashing table.
int_t push_back(const element &e)
Adds an element to the set (this is an equivalent of 'add').
const element & operator[](int_t n) const
Returns the element with the given number from the set.
int_t pop()
Removes the last element from the set.
hashedset< element, hashkeys > & add(const hashedset< element, hashkeys > &s)
Adds an entire set of elements to the set.
int_t hashsize
The size of the hashing table.
int_t push(const element &e)
Adds an element to the set (this is an equivalent of 'add').
bool check(const element &e) const
Checks if the given element is in the set.
int_t size() const
Returns the number of elements in the set.
const element & top() const
Returns the last element in the set.
int_t add(const element &e)
Adds the given element to the set and returns its number.
int_t getnumber(const element &e) const
Finds the given element and returns its number.
~hashedset(void)
The destructor.
This is a small class used to gather and display hashing statistics for the hashing tables in the cla...
unsigned long hashhits
The number of times that an element was found in the hashing table.
unsigned long hashmisses
The number of times that an element was not found in the hashing table, because that entry was used f...
std::time_t creationtime
The creation time of the hashed set.
hashstat()
The constructor.
unsigned long rehashcount
The number of rehashing the table when the size of the hashing table was changed and all the elements...
A container for elements placed in a table (like a vector) that is actually built of many smaller tab...
This class defines a multivalued map.
const hashedset< imgelement > & operator()(int_t n) const
Retrieve the image of the n-th element for reading only.
hashedset< domelement > domain
The domain of the map.
void swap(mvmap< domelement, imgelement > &other)
Swaps the internal data of two multivalued maps.
bool removenum(int_t n)
Removes the n-th element from the domain of the map.
const domelement & get(int_t n) const
Retrieves the n-th element from the domain for reading only.
const hashedset< domelement > & getdomain() const
Retrieves the domain of the map for reading only.
multitable< hashedset< imgelement > > images
The images of cubes from the domain.
hashedset< imgelement > & operator[](int_t n)
Returns the image of the n-th element for writing.
mvmap(int bequiet=1)
The default constructor.
void remove(const hashedset< domelement > &x)
Removes a set of elements from the domain of the map.
int quiet
This variable indicates whether the map should be quiet.
bool remove(const domelement &x)
Removes an element from the domain of the map.
int_t size() const
Returns the number of elements in the domain of the map.
This file contains some precompiler definitions which indicate the operating system and/or compiler u...
int int_t
Index type for indexing arrays, counting cubes, etc.
#define SMALL_SIZE
This constant passed to the function 'write' makes the hashed set be displayed in a way that is appro...
#define LARGE_SIZE
This constant passed to the function 'write' makes the hashed set be displayed in a way that is appro...
This file contains the definition of two hashing methods for integers.
This file contains the definition of the container "multitable" which is essentially an automatically...
std::istream & readrestriction(std::istream &in, mvmap< domelement, imgelement > &m, const hashedset< domelement > &dom, const hashedset< imgelement > &img)
Reads a restriction of a multivalued map to the two given sets.
hashedset< element, hashkeys > & operator-=(hashedset< element, hashkeys > &s, const hashedset< element, hashkeys > &u)
Operator -= removes the contents of one set from another.
outputstream sout
A replacement for standard output stream, with optional logging and other features provided by the cl...
std::ostream & operator<<(std::ostream &out, const bincube< Dim, twoPower > &b)
int write(std::ostream &out, const coordtype *c, int dim, char parenthesis=40, char closing=0)
int_t hashkey2(const hashNumber< Number > &n)
The second hashing key.
int readparenthesis(std::istream &in)
Reads an opening parenthesis from the input file.
std::istream & readimage(std::istream &in, hashedset< tCube > &img)
Reads the image of a multivalued cubical map.
int read(textfile &f, coordtype *c, int maxdim)
Reads a point from a text file and removes a pair of parentheses, braces or brackets if present.
std::istream & readselective(std::istream &in, const hashedset< tCube > &dom1, const hashedset< tCube > &dom2, mvmap< tCube, tCube > &m)
Reads the restriction of a multivalued map to the given pair of sets.
hashedset< imgelement > & retrieveimage(const mvmap< domelement, imgelement > &m, hashedset< imgelement > &img)
Adds images of all the elements from the domain of the map to 'img'.
std::istream & operator>>(std::istream &in, bincube< Dim, twoPower > &b)
int_t hashkey1(const hashNumber< Number > &n)
The first hashing key.
void ignorecomments(std::istream &in)
Ignores white characters (spaces, tabulators, CRs and LFs), as well as comments from the input text f...
std::istream & readdomain(std::istream &in, hashedset< tCube > &dom)
Reads the domain of a multivalued cubical map.
hashedset< element, hashkeys > & operator+=(hashedset< element, hashkeys > &s, const hashedset< element, hashkeys > &u)
Operator += adds one hashed set to another.
gcomplex< cell, euclidom > & creategraph(const mvmap< cell, cell > &m, gcomplex< cell, euclidom > &graph)
Add a graph of a multivalued cell map to the cell complex.
void swap(mwWorkerData &data1, mwWorkerData &data2)
This namespace contains the entire CHomP library interface.
This file contains some useful functions related to the text input/output procedures.