/usr/include/givaro/givhashtable.inl is in libgivaro-dev 4.0.2-8ubuntu1.
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 | // ==========================================================================
// $Source: /var/lib/cvs/Givaro/src/kernel/bstruct/givhashtable.inl,v $
// Copyright(c)'1994-2009 by The Givaro group
// This file is part of Givaro.
// Givaro is governed by the CeCILL-B license under French law
// and abiding by the rules of distribution of free software.
// see the COPYRIGHT file for more details.
// Author: T. Gautier
// $Id: givhashtable.inl,v 1.3 2011-02-02 16:23:55 briceboyer Exp $
// ==========================================================================
// Description:
// - hash table implementation
#ifndef __GIVARO_hashtable_INL
#define __GIVARO_hashtable_INL
namespace Givaro {
// Size of the table :
template<class T, class Key>
HashTable<T,Key>::HashTable ( int n )
{
allocate(n) ;
}
template<class T, class Key>
void HashTable<T,Key>::allocate( int n )
{
num = n ;
if (n ==0) {
tabH = tabE = 0 ;
return ;
}
tabH = new _E*[n] ;
tabE = new _E*[n] ;
for (int i=0 ; i<num ; i++) tabH[i] = tabE[i] = 0 ;
}
template<class T, class Key>
HashTable<T,Key>::~HashTable()
{
freeing() ;
}
template<class T, class Key>
void HashTable<T,Key>::freeing()
{
if (tabH ==0) return ;
for (int i=0 ; i<num ; i++)
{
_E* curr = tabH[i] ;
while (curr !=0)
{
_E* tmp = curr->next ;
delete curr ;
curr = tmp ;
}
}
delete [] tabH ;
delete [] tabE ;
tabH = 0 ;
tabE = 0 ;
}
template<class T, class Key>
void HashTable<T,Key>::removeall ( )
{
if (tabH ==0) return ;
int i ;
for (i=0 ; i<num ; i++)
{
_E* curr = tabH[i] ;
while (curr !=0)
{
_E* tmp = curr->next ;
delete curr ;
curr = tmp ;
}
}
for (i=0 ; i<num ; i++) tabH[i] = tabE[i] = 0 ;
}
template<class T, class Key>
void HashTable<T,Key>::insert( const T& item, const Key& k )
{
if ((tabE ==0) || (tabH ==0)) return ;
insertLast( item, k ) ;
}
template<class T, class Key>
void HashTable<T,Key>::insertLast( const T& item, const Key& k )
{
if ((tabE ==0) || (tabH ==0)) return ;
unsigned int e = ((unsigned long)k.godel()) % ((unsigned long)num) ;
_E* cur = new _E ( item, k) ;
if (tabE[e] == 0)
{
tabH[e] = tabE[e] = cur ;
cur->next = cur->pred = 0 ;
return ;
}
tabE[e]->next = cur ;
cur->pred = tabE[e] ;
cur->next = 0 ;
tabE[e] = cur ;
}
template<class T, class Key>
void HashTable<T,Key>::insertFront( const T& item, const Key& k )
{
if ((tabE ==0) || (tabH ==0)) return ;
unsigned int e = ((unsigned long)k.godel()) % ((unsigned long)num) ;
_E* cur = new _E ( item, k) ;
if (tabH[e] == 0)
{
tabH[e] = tabE[e] = cur ;
cur->next = cur->pred = 0 ;
return ;
}
tabH[e]->pred = cur ;
cur->next = tabE[e] ;
cur->pred = 0 ;
tabH[e] = cur ;
}
// Remove entry with key k
template<class T, class Key>
void HashTable<T,Key>::remove ( const Key& k)
{
if ((tabE ==0) || (tabH ==0)) return ;
unsigned int e = ((unsigned long)k.godel()) % ((unsigned long)num) ;
_E* cur = tabH[e] ;
while (cur !=0)
{
if (k == cur->key)
{
if (cur->pred ==0) tabH[e] = cur->next ;
else cur->pred->next = cur->next ;
if (cur->next ==0) tabE[e] = cur->pred ;
else cur->next->pred = cur->pred ;
delete cur ;
}
cur = cur->next ;
}
}
template<class T, class Key>
int HashTable<T,Key>::get ( T& item, const Key& k ) const
{
if ((tabE ==0) || (tabH ==0)) return 0 ;
unsigned int e = ((unsigned long)k.godel()) % ((unsigned long)num) ;
_E* cur = tabH[e] ;
while (cur !=0)
{
if (k == cur->key)
{
item = cur->oneitem ;
return 1 ;
}
cur = cur->next ;
}
return 0 ;
}
template<class T, class Key>
int HashTable<T,Key>::getrmv ( T& item, const Key& k )
{
if ((tabE ==0) || (tabH ==0)) return 0 ;
unsigned int e = ((unsigned long)k.godel()) % ((unsigned long)num) ;
_E* cur = tabH[e] ;
while (cur !=0)
{
if (k == cur->key)
{
item = cur->oneitem ;
if (cur->pred ==0) tabH[e] = cur->next ;
else cur->pred->next = cur->next ;
if (cur->next ==0) tabE[e] = cur->pred ;
else cur->next->pred = cur->pred ;
delete cur ;
return 1 ;
}
cur = cur->next ;
}
return 0 ;
}
// Returns the number of items of the same key :
template<class T, class Key>
int HashTable<T,Key>::num_item( const Key& k ) const
{
if ((tabE ==0) || (tabH ==0)) return 0 ;
int count = 0 ;
unsigned int e = ((unsigned long)k.godel()) % ((unsigned long)num) ;
_E* cur = tabH[e] ;
while (cur !=0)
{
if (k == cur->key) count++ ;
cur = cur->next ;
}
return count ;
}
} // namespace Givaro
#endif // __GIVARO_hashtable_INL
|