MCPcopy Create free account
hub / github.com/diasurgical/devilution / SortBuffer

Function SortBuffer

3rdParty/PKWare/implode.cpp:139–183  ·  view source on GitHub ↗

Builds the "hash_to_index" table and "pair_hash_offsets" table. Every element of "hash_to_index" will contain lowest index to the "pair_hash_offsets" table, effectively giving offset of the first occurence of the given PAIR_HASH in the input data.

Source from the content-addressed store, hash-verified

137// "pair_hash_offsets" table, effectively giving offset of the first
138// occurence of the given PAIR_HASH in the input data.
139static void PKWAREAPI SortBuffer(TCmpStruct * pWork, unsigned char * buffer_begin, unsigned char * buffer_end)
140{
141 unsigned short * phash_to_index;
142 unsigned char * buffer_ptr;
143 unsigned short total_sum = 0;
144 unsigned long byte_pair_hash; // Hash value of the byte pair
145 unsigned short byte_pair_offs; // Offset of the byte pair, relative to "work_buff"
146
147 // Zero the entire "phash_to_index" table
148 memset(pWork->phash_to_index, 0, sizeof(pWork->phash_to_index));
149
150 // Step 1: Count amount of each PAIR_HASH in the input buffer
151 // The table will look like this:
152 // offs 0x000: Number of occurences of PAIR_HASH 0
153 // offs 0x001: Number of occurences of PAIR_HASH 1
154 // ...
155 // offs 0x8F7: Number of occurences of PAIR_HASH 0x8F7 (the highest hash value)
156 for(buffer_ptr = buffer_begin; buffer_ptr < buffer_end; buffer_ptr++)
157 pWork->phash_to_index[BYTE_PAIR_HASH(buffer_ptr)]++;
158
159 // Step 2: Convert the table to the array of PAIR_HASH amounts.
160 // Each element contains count of PAIR_HASHes that is less or equal
161 // to element index
162 // The table will look like this:
163 // offs 0x000: Number of occurences of PAIR_HASH 0 or lower
164 // offs 0x001: Number of occurences of PAIR_HASH 1 or lower
165 // ...
166 // offs 0x8F7: Number of occurences of PAIR_HASH 0x8F7 or lower
167 for(phash_to_index = pWork->phash_to_index; phash_to_index < &pWork->phash_to_index_end; phash_to_index++)
168 {
169 total_sum = total_sum + phash_to_index[0];
170 phash_to_index[0] = total_sum;
171 }
172
173 // Step 3: Convert the table to the array of indexes.
174 // Now, each element contains index to the first occurence of given PAIR_HASH
175 for(buffer_end--; buffer_end >= buffer_begin; buffer_end--)
176 {
177 byte_pair_hash = BYTE_PAIR_HASH(buffer_end);
178 byte_pair_offs = (unsigned short)(buffer_end - pWork->work_buff);
179
180 pWork->phash_to_index[byte_pair_hash]--;
181 pWork->phash_offs[pWork->phash_to_index[byte_pair_hash]] = byte_pair_offs;
182 }
183}
184
185static void PKWAREAPI FlushBuf(TCmpStruct * pWork)
186{

Callers 1

WriteCmpDataFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected