Path: blob/devel/ElmerGUI/netgen/libsrc/general/hashtabl.cpp
3206 views
/**************************************************************************/1/* File: hashtabl.cpp */2/* Author: Joachim Schoeberl */3/* Date: 01. Jun. 95 */4/**************************************************************************/56/*7Abstract data type HASHTABLE8*/910#include <algorithm>11#include <mystdlib.h>12#include <myadt.hpp>1314namespace netgen15{16//using namespace netgen;1718void INDEX_4 :: Sort ()19{20if (i[0] > i[1]) Swap (i[0], i[1]);21if (i[2] > i[3]) Swap (i[2], i[3]);22if (i[0] > i[2]) Swap (i[0], i[2]);23if (i[1] > i[3]) Swap (i[1], i[3]);24if (i[1] > i[2]) Swap (i[1], i[2]);25}26272829void INDEX_4Q :: Sort ()30{31if (min2 (i[1], i[2]) < min2 (i[0], i[3]))32{ Swap (i[0], i[1]); Swap (i[2], i[3]);}33if (i[3] < i[0])34{ Swap (i[0], i[3]); Swap (i[1], i[2]);}35if (i[3] < i[1])36{ Swap (i[1], i[3]); }37}383940ostream & operator<<(ostream & s, const INDEX_2 & i2)41{42return s << i2.I1() << ", " << i2.I2();43}4445ostream & operator<<(ostream & s, const INDEX_3 & i3)46{47return s << i3.I1() << ", " << i3.I2() << ", " << i3.I3();48}4950ostream & operator<<(ostream & s, const INDEX_4 & i4)51{52return s << i4.I1() << ", " << i4.I2() << ", " << i4.I3() << ", " << i4.I4();53}5455ostream & operator<<(ostream & s, const INDEX_4Q & i4)56{57return s << i4.I1() << ", " << i4.I2() << ", " << i4.I3() << ", " << i4.I4();58}596061int BASE_INDEX_HASHTABLE :: Position (int bnr, const INDEX & ind) const62{63int i;64for (i = 1; i <= hash.EntrySize (bnr); i++)65if (hash.Get(bnr, i) == ind)66return i;67return 0;68}69707172/*73int BASE_INDEX_2_HASHTABLE :: Position (int bnr, const INDEX_2 & ind) const74{75int i;76for (i = 1; i <= hash.EntrySize (bnr); i++)77if (hash.Get(bnr, i) == ind)78return i;79return 0;80}81*/8283void BASE_INDEX_2_HASHTABLE :: PrintStat (ostream & ost) const84{85int n = hash.Size();86int i;87int sumn = 0, sumnn = 0;8889for (i = 1; i <= n; i++)90{91sumn += hash.EntrySize(i);92sumnn += sqr (hash.EntrySize(i));93}9495ost << "Hashtable: " << endl96<< "size : " << n << endl97<< "elements per row : " << (double(sumn) / double(n)) << endl98<< "av. acces time : "99<< (sumn ? (double (sumnn) / double(sumn)) : 0) << endl;100}101102103/*104int BASE_INDEX_3_HASHTABLE :: Position (int bnr, const INDEX_3 & ind) const105{106int i;107const INDEX_3 * pi = &hash.Get(bnr, 1);108int n = hash.EntrySize(bnr);109for (i = 1; i <= n; ++i, ++pi)110{111if (*pi == ind)112return i;113}114115return 0;116}117*/118119120121122123124125126127128129130131132133134135136137138BASE_INDEX_CLOSED_HASHTABLE ::139BASE_INDEX_CLOSED_HASHTABLE (int size)140: hash(size)141{142hash.SetName ("index-hashtable, hash");143144invalid = -1;145for (int i = 1; i <= size; i++)146hash.Elem(i) = invalid;147}148149void BASE_INDEX_CLOSED_HASHTABLE ::150BaseSetSize (int size)151{152hash.SetSize(size);153for (int i = 1; i <= size; i++)154hash.Elem(i) = invalid;155}156157int BASE_INDEX_CLOSED_HASHTABLE ::158Position2 (const INDEX & ind) const159{160int i = HashValue(ind);161while (1)162{163i++;164if (i > hash.Size()) i = 1;165if (hash.Get(i) == ind) return i;166if (hash.Get(i) == invalid) return 0;167}168}169170int BASE_INDEX_CLOSED_HASHTABLE ::171PositionCreate2 (const INDEX & ind, int & apos)172{173int i = HashValue(ind);174int startpos = i;175while (1)176{177i++;178if (i > hash.Size()) i = 1;179if (hash.Get(i) == ind)180{181apos = i;182return 0;183}184if (hash.Get(i) == invalid)185{186hash.Elem(i) = ind;187apos = i;188return 1;189}190if (i == startpos)191throw NgException ("Try to set new element in full closed hashtable");192}193}194195int BASE_INDEX_CLOSED_HASHTABLE :: UsedElements () const196{197int n = hash.Size();198int cnt = 0;199for (int i = 1; i <= n; i++)200if (hash.Get(i) != invalid)201cnt++;202return cnt;203}204205206207208209210211212213214215BASE_INDEX_2_CLOSED_HASHTABLE ::216BASE_INDEX_2_CLOSED_HASHTABLE (int size)217: hash(size)218{219hash.SetName ("i2-hashtable, hash");220221invalid = -1;222for (int i = 1; i <= size; i++)223hash.Elem(i).I1() = invalid;224}225226void BASE_INDEX_2_CLOSED_HASHTABLE ::227BaseSetSize (int size)228{229hash.SetSize(size);230for (int i = 1; i <= size; i++)231hash.Elem(i).I1() = invalid;232}233234235int BASE_INDEX_2_CLOSED_HASHTABLE ::236Position2 (const INDEX_2 & ind) const237{238int i = HashValue(ind);239while (1)240{241i++;242if (i > hash.Size()) i = 1;243if (hash.Get(i) == ind) return i;244if (hash.Get(i).I1() == invalid) return 0;245}246}247248int BASE_INDEX_2_CLOSED_HASHTABLE ::249PositionCreate2 (const INDEX_2 & ind, int & apos)250{251int i = HashValue(ind);252int startpos = i;253while (1)254{255i++;256if (i > hash.Size()) i = 1;257if (hash.Get(i) == ind)258{259apos = i;260return 0;261}262if (hash.Get(i).I1() == invalid)263{264hash.Elem(i) = ind;265apos = i;266return 1;267}268if (i == startpos)269throw NgException ("Try to set new element in full closed hashtable");270}271}272273int BASE_INDEX_2_CLOSED_HASHTABLE :: UsedElements () const274{275int n = hash.Size();276int cnt = 0;277for (int i = 1; i <= n; i++)278if (hash.Get(i).I1() != invalid)279cnt++;280return cnt;281}282283284285286287288289290void BASE_INDEX_3_CLOSED_HASHTABLE ::291BaseSetSize (int size)292{293hash.SetSize(size);294for (int i = 0; i < size; i++)295hash[i].I1() = invalid;296}297298bool BASE_INDEX_3_CLOSED_HASHTABLE ::299PositionCreate2 (const INDEX_3 & ind, int & apos)300{301int i = HashValue(ind);302int startpos = i;303while (1)304{305/*306i++;307if (i >= hash.Size()) i = 0;308*/309i = (i+1) % hash.Size();310if (hash[i] == ind)311{312apos = i;313return false;314}315if (hash[i].I1() == invalid)316{317hash[i] = ind;318apos = i;319return true;320}321if (i == startpos)322throw NgException ("Try to set new element in full closed hashtable");323}324}325}326327328329