doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HashIndex.cpp
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #include "../precompiled.h"
30 #pragma hdrstop
31 
32 int idHashIndex::INVALID_INDEX[1] = { -1 };
33 
34 /*
35 ================
36 idHashIndex::Init
37 ================
38 */
39 void idHashIndex::Init( const int initialHashSize, const int initialIndexSize ) {
40  assert( idMath::IsPowerOfTwo( initialHashSize ) );
41 
42  hashSize = initialHashSize;
44  indexSize = initialIndexSize;
47  hashMask = hashSize - 1;
48  lookupMask = 0;
49 }
50 
51 /*
52 ================
53 idHashIndex::Allocate
54 ================
55 */
56 void idHashIndex::Allocate( const int newHashSize, const int newIndexSize ) {
57  assert( idMath::IsPowerOfTwo( newHashSize ) );
58 
59  Free();
60  hashSize = newHashSize;
61  hash = new int[hashSize];
62  memset( hash, 0xff, hashSize * sizeof( hash[0] ) );
63  indexSize = newIndexSize;
64  indexChain = new int[indexSize];
65  memset( indexChain, 0xff, indexSize * sizeof( indexChain[0] ) );
66  hashMask = hashSize - 1;
67  lookupMask = -1;
68 }
69 
70 /*
71 ================
72 idHashIndex::Free
73 ================
74 */
75 void idHashIndex::Free( void ) {
76  if ( hash != INVALID_INDEX ) {
77  delete[] hash;
79  }
80  if ( indexChain != INVALID_INDEX ) {
81  delete[] indexChain;
83  }
84  lookupMask = 0;
85 }
86 
87 /*
88 ================
89 idHashIndex::ResizeIndex
90 ================
91 */
92 void idHashIndex::ResizeIndex( const int newIndexSize ) {
93  int *oldIndexChain, mod, newSize;
94 
95  if ( newIndexSize <= indexSize ) {
96  return;
97  }
98 
99  mod = newIndexSize % granularity;
100  if ( !mod ) {
101  newSize = newIndexSize;
102  } else {
103  newSize = newIndexSize + granularity - mod;
104  }
105 
106  if ( indexChain == INVALID_INDEX ) {
107  indexSize = newSize;
108  return;
109  }
110 
111  oldIndexChain = indexChain;
112  indexChain = new int[newSize];
113  memcpy( indexChain, oldIndexChain, indexSize * sizeof(int) );
114  memset( indexChain + indexSize, 0xff, (newSize - indexSize) * sizeof(int) );
115  delete[] oldIndexChain;
116  indexSize = newSize;
117 }
118 
119 /*
120 ================
121 idHashIndex::GetSpread
122 ================
123 */
124 int idHashIndex::GetSpread( void ) const {
125  int i, index, totalItems, *numHashItems, average, error, e;
126 
127  if ( hash == INVALID_INDEX ) {
128  return 100;
129  }
130 
131  totalItems = 0;
132  numHashItems = new int[hashSize];
133  for ( i = 0; i < hashSize; i++ ) {
134  numHashItems[i] = 0;
135  for ( index = hash[i]; index >= 0; index = indexChain[index] ) {
136  numHashItems[i]++;
137  }
138  totalItems += numHashItems[i];
139  }
140  // if no items in hash
141  if ( totalItems <= 1 ) {
142  delete[] numHashItems;
143  return 100;
144  }
145  average = totalItems / hashSize;
146  error = 0;
147  for ( i = 0; i < hashSize; i++ ) {
148  e = abs( numHashItems[i] - average );
149  if ( e > 1 ) {
150  error += e - 1;
151  }
152  }
153  delete[] numHashItems;
154  return 100 - (error * 100 / totalItems);
155 }
void ResizeIndex(const int newIndexSize)
Definition: HashIndex.cpp:92
assert(prefInfo.fullscreenBtn)
int lookupMask
Definition: HashIndex.h:98
int hashSize
Definition: HashIndex.h:92
int hashMask
Definition: HashIndex.h:97
void Allocate(const int newHashSize, const int newIndexSize)
Definition: HashIndex.cpp:56
int i
Definition: process.py:33
void Free(void)
Definition: HashIndex.cpp:75
void Init(const int initialHashSize, const int initialIndexSize)
Definition: HashIndex.cpp:39
int * hash
Definition: HashIndex.h:93
GLuint index
Definition: glext.h:3476
int indexSize
Definition: HashIndex.h:94
static int INVALID_INDEX[1]
Definition: HashIndex.h:100
static bool IsPowerOfTwo(int x)
Definition: Math.h:754
#define DEFAULT_HASH_GRANULARITY
Definition: HashIndex.h:42
int granularity
Definition: HashIndex.h:96
int GetSpread(void) const
Definition: HashIndex.cpp:124
int * indexChain
Definition: HashIndex.h:95