PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/cde/lib/DtSvc/DtUtil2/Hash.c

https://bitbucket.org/tifan/cde
C | 292 lines | 204 code | 33 blank | 55 comment | 29 complexity | 66c67157046ca6e7a475757f42cdadef MD5 | raw file
Possible License(s): LGPL-2.1, IPL-1.0, 0BSD
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these librararies and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: Hash.c /main/4 1995/10/26 15:22:41 rswiston $ */
  24. /*
  25. * (c) Copyright 1993, 1994 Hewlett-Packard Company *
  26. * (c) Copyright 1993, 1994 International Business Machines Corp. *
  27. * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
  28. * (c) Copyright 1993, 1994 Novell, Inc. *
  29. */
  30. /***********************************************************
  31. Copyright 1987, 1988, 1990 by Digital Equipment Corporation, Maynard,
  32. Massachusetts, and the Massachusetts Institute of Technology, Cambridge,
  33. Massachusetts.
  34. All Rights Reserved
  35. Permission to use, copy, modify, and distribute this software and its
  36. documentation for any purpose and without fee is hereby granted,
  37. provided that the above copyright notice appear in all copies and that
  38. both that copyright notice and this permission notice appear in
  39. supporting documentation, and that the names of Digital or MIT not be
  40. used in advertising or publicity pertaining to distribution of the
  41. software without specific, written prior permission.
  42. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  43. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  44. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  45. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  46. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  47. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  48. SOFTWARE.
  49. ******************************************************************/
  50. #include "HashP.h"
  51. /******** Static Function Declarations ********/
  52. static unsigned int GetTableIndex(
  53. register DtHashTable tab,
  54. register DtHashKey key,
  55. #if NeedWidePrototypes
  56. register int new) ;
  57. #else
  58. register Boolean new) ;
  59. #endif /* NeedWidePrototypes */
  60. static void ExpandHashTable(
  61. register DtHashTable tab) ;
  62. /******** End Static Function Declarations ********/
  63. typedef unsigned long Signature;
  64. typedef struct _DtHashTableRec {
  65. unsigned int mask; /* size of hash table - 1 */
  66. unsigned int rehash; /* mask - 2 */
  67. unsigned int occupied; /* number of occupied entries */
  68. unsigned int fakes; /* number occupied by DTHASHfake */
  69. DtHashEntryType *types; /* lookup methods for key */
  70. unsigned short numTypes; /* number of lookup methods */
  71. Boolean keyIsString; /* whether the hash key is a string */
  72. DtHashEntry *entries; /* the entries */
  73. }DtHashTableRec;
  74. static DtHashEntryRec DtHashfake; /* placeholder for deletions */
  75. #define HASH(tab,sig) ((sig) & tab->mask)
  76. #define REHASHVAL(tab, idx) ((((idx) % tab->rehash) + 2) | 1)
  77. #define REHASH(tab,idx,rehash) ((idx + rehash) & tab->mask)
  78. #define KEY(tab, entry) \
  79. ((* (tab->types[entry->hash.type]->hash.getKeyFunc) ) \
  80. (entry, tab->types[entry->hash.type]->hash.getKeyClientData))
  81. #define RELEASE_KEY(tab, entry, key) \
  82. {\
  83. if (tab->types[entry->hash.type]->hash.releaseKeyProc) \
  84. (* (tab->types[entry->hash.type]->hash.releaseKeyProc)) \
  85. (entry, key); \
  86. }
  87. static unsigned int
  88. GetTableIndex(
  89. register DtHashTable tab,
  90. register DtHashKey key,
  91. #if NeedWidePrototypes
  92. register int new)
  93. #else
  94. register Boolean new)
  95. #endif /* NeedWidePrototypes */
  96. {
  97. register DtHashEntry *entries = tab->entries;
  98. register int len, idx, i, rehash = 0;
  99. register char c;
  100. register Signature sig = 0;
  101. register DtHashEntry entry;
  102. String s1, s2;
  103. DtHashKey compKey;
  104. if (tab->keyIsString) {
  105. s1 = (String)key;
  106. for (s2 = (char *)s1; c = *s2++; )
  107. sig = (sig << 1) + c;
  108. len = s2 - s1 - 1;
  109. }
  110. else
  111. sig = (Signature)key;
  112. idx = HASH(tab, sig);
  113. while (entry = entries[idx]) {
  114. if (entries[idx] == &DtHashfake) {
  115. if (new)
  116. return idx;
  117. else
  118. goto nomatch;
  119. }
  120. if (tab->keyIsString) {
  121. compKey = KEY(tab, entry);
  122. for (i = len, s1 = (String)key, s2 = (String) compKey;
  123. --i >= 0; ) {
  124. if (*s1++ != *s2++)
  125. goto nomatch;
  126. }
  127. }
  128. else {
  129. if ((compKey = KEY(tab, entry)) != key)
  130. s2 = " ";
  131. else
  132. s2 = "";
  133. }
  134. if (*s2) {
  135. nomatch:
  136. RELEASE_KEY(tab, entry, compKey);
  137. if (!rehash)
  138. rehash = REHASHVAL(tab, idx);
  139. idx = REHASH(tab, idx, rehash);
  140. continue;
  141. }
  142. else
  143. RELEASE_KEY(tab, entry, compKey);
  144. break;
  145. }
  146. return idx;
  147. }
  148. void
  149. _DtRegisterHashEntry(
  150. register DtHashTable tab,
  151. register DtHashKey key,
  152. register DtHashEntry entry )
  153. {
  154. unsigned int idx;
  155. if ((tab->occupied + (tab->occupied >> 2)) > tab->mask)
  156. ExpandHashTable(tab);
  157. idx = GetTableIndex(tab, key, True);
  158. if (tab->entries[idx] == &DtHashfake)
  159. tab->fakes--;
  160. tab->occupied++;
  161. tab->entries[idx] = entry;
  162. }
  163. void
  164. _DtUnregisterHashEntry(
  165. register DtHashTable tab,
  166. register DtHashEntry entry )
  167. {
  168. register int idx, rehash;
  169. register DtHashEntry *entries = tab->entries;
  170. DtHashKey key = KEY(tab, entry);
  171. idx = GetTableIndex(tab, key, False);
  172. RELEASE_KEY(tab, entry, key);
  173. entries[idx] = &DtHashfake;
  174. tab->fakes++;
  175. tab->occupied--;
  176. }
  177. static void
  178. ExpandHashTable(
  179. register DtHashTable tab )
  180. {
  181. unsigned int oldmask;
  182. register DtHashEntry *oldentries, *entries;
  183. register int oldidx, newidx, rehash, len;
  184. register DtHashEntry entry;
  185. register DtHashKey key;
  186. oldmask = tab->mask;
  187. oldentries = tab->entries;
  188. tab->fakes = 0;
  189. if ((tab->occupied + (tab->occupied >> 2)) > tab->mask) {
  190. tab->mask = (tab->mask << 1) + 1;
  191. tab->rehash = tab->mask - 2;
  192. }
  193. entries = tab->entries = (DtHashEntry *) XtCalloc(tab->mask+1, sizeof(DtHashEntry));
  194. for (oldidx = 0; oldidx <= oldmask; oldidx++) {
  195. if ((entry = oldentries[oldidx]) && entry != &DtHashfake) {
  196. newidx = GetTableIndex(tab, key = KEY(tab, entry), True);
  197. RELEASE_KEY(tab, entry, key);
  198. entries[newidx] = entry;
  199. }
  200. }
  201. XtFree((char *)oldentries);
  202. }
  203. DtHashEntry
  204. _DtEnumerateHashTable(
  205. register DtHashTable tab,
  206. register DtHashEnumerateFunc enumFunc,
  207. register XtPointer clientData )
  208. {
  209. register unsigned int i;
  210. for (i = 0; i <= tab->mask; i++)
  211. if (tab->entries[i] &&
  212. tab->entries[i] != &DtHashfake &&
  213. ((*enumFunc) (tab->entries[i], clientData)))
  214. return tab->entries[i];
  215. return NULL;
  216. }
  217. DtHashEntry
  218. _DtKeyToHashEntry(
  219. register DtHashTable tab,
  220. register DtHashKey key )
  221. {
  222. register int idx, rehash, len;
  223. register DtHashEntry entry, *entries = tab->entries;
  224. if (!key) return NULL;
  225. idx = GetTableIndex(tab, key, False);
  226. return entries[idx];
  227. }
  228. DtHashTable
  229. _DtAllocHashTable(DtHashEntryType *hashEntryTypes,
  230. Cardinal numHashEntryTypes,
  231. #if NeedWidePrototypes
  232. int keyIsString)
  233. #else
  234. Boolean keyIsString)
  235. #endif /* NeedWidePrototypes */
  236. {
  237. register DtHashTable tab;
  238. tab = (DtHashTable) XtMalloc(sizeof(struct _DtHashTableRec));
  239. tab->types = hashEntryTypes;
  240. tab->numTypes = numHashEntryTypes;
  241. tab->keyIsString = keyIsString;
  242. tab->mask = 0x7f;
  243. tab->rehash = tab->mask - 2;
  244. tab->entries = (DtHashEntry *) XtCalloc(tab->mask+1, sizeof(DtHashEntry));
  245. tab->occupied = 0;
  246. tab->fakes = 0;
  247. return tab;
  248. }
  249. void
  250. _DtFreeHashTable(
  251. DtHashTable hashTable )
  252. {
  253. XtFree((char *)hashTable->entries);
  254. XtFree((char *)hashTable);
  255. }