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
|
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "core/fxcrt/include/fx_basic.h"
#include "core/fxcrt/plex.h"
CFX_MapPtrToPtr::CFX_MapPtrToPtr(int nBlockSize)
: m_pHashTable(NULL),
m_nHashTableSize(17),
m_nCount(0),
m_pFreeList(NULL),
m_pBlocks(NULL),
m_nBlockSize(nBlockSize) {
ASSERT(m_nBlockSize > 0);
}
void CFX_MapPtrToPtr::RemoveAll() {
FX_Free(m_pHashTable);
m_pHashTable = NULL;
m_nCount = 0;
m_pFreeList = NULL;
m_pBlocks->FreeDataChain();
m_pBlocks = NULL;
}
CFX_MapPtrToPtr::~CFX_MapPtrToPtr() {
RemoveAll();
ASSERT(m_nCount == 0);
}
uint32_t CFX_MapPtrToPtr::HashKey(void* key) const {
return ((uint32_t)(uintptr_t)key) >> 4;
}
void CFX_MapPtrToPtr::GetNextAssoc(FX_POSITION& rNextPosition,
void*& rKey,
void*& rValue) const {
ASSERT(m_pHashTable);
CAssoc* pAssocRet = (CAssoc*)rNextPosition;
ASSERT(pAssocRet);
if (pAssocRet == (CAssoc*)-1) {
for (uint32_t nBucket = 0; nBucket < m_nHashTableSize; nBucket++) {
pAssocRet = m_pHashTable[nBucket];
if (pAssocRet)
break;
}
ASSERT(pAssocRet);
}
CAssoc* pAssocNext = pAssocRet->pNext;
for (uint32_t nBucket = (HashKey(pAssocRet->key) % m_nHashTableSize) + 1;
nBucket < m_nHashTableSize && !pAssocNext; nBucket++) {
pAssocNext = m_pHashTable[nBucket];
}
rNextPosition = (FX_POSITION)pAssocNext;
rKey = pAssocRet->key;
rValue = pAssocRet->value;
}
FX_BOOL CFX_MapPtrToPtr::Lookup(void* key, void*& rValue) const {
uint32_t nHash;
CAssoc* pAssoc = GetAssocAt(key, nHash);
if (!pAssoc) {
return FALSE;
}
rValue = pAssoc->value;
return TRUE;
}
void* CFX_MapPtrToPtr::GetValueAt(void* key) const {
uint32_t nHash;
CAssoc* pAssoc = GetAssocAt(key, nHash);
if (!pAssoc) {
return NULL;
}
return pAssoc->value;
}
void*& CFX_MapPtrToPtr::operator[](void* key) {
uint32_t nHash;
CAssoc* pAssoc;
if ((pAssoc = GetAssocAt(key, nHash)) == NULL) {
if (!m_pHashTable) {
InitHashTable(m_nHashTableSize);
}
pAssoc = NewAssoc();
pAssoc->key = key;
pAssoc->pNext = m_pHashTable[nHash];
m_pHashTable[nHash] = pAssoc;
}
return pAssoc->value;
}
CFX_MapPtrToPtr::CAssoc* CFX_MapPtrToPtr::GetAssocAt(void* key,
uint32_t& nHash) const {
nHash = HashKey(key) % m_nHashTableSize;
if (!m_pHashTable) {
return NULL;
}
CAssoc* pAssoc;
for (pAssoc = m_pHashTable[nHash]; pAssoc; pAssoc = pAssoc->pNext) {
if (pAssoc->key == key)
return pAssoc;
}
return NULL;
}
CFX_MapPtrToPtr::CAssoc* CFX_MapPtrToPtr::NewAssoc() {
if (!m_pFreeList) {
CFX_Plex* newBlock = CFX_Plex::Create(m_pBlocks, m_nBlockSize,
sizeof(CFX_MapPtrToPtr::CAssoc));
CFX_MapPtrToPtr::CAssoc* pAssoc =
(CFX_MapPtrToPtr::CAssoc*)newBlock->data();
pAssoc += m_nBlockSize - 1;
for (int i = m_nBlockSize - 1; i >= 0; i--, pAssoc--) {
pAssoc->pNext = m_pFreeList;
m_pFreeList = pAssoc;
}
}
CFX_MapPtrToPtr::CAssoc* pAssoc = m_pFreeList;
m_pFreeList = m_pFreeList->pNext;
m_nCount++;
ASSERT(m_nCount > 0);
pAssoc->key = 0;
pAssoc->value = 0;
return pAssoc;
}
void CFX_MapPtrToPtr::InitHashTable(uint32_t nHashSize, FX_BOOL bAllocNow) {
ASSERT(m_nCount == 0);
ASSERT(nHashSize > 0);
FX_Free(m_pHashTable);
m_pHashTable = NULL;
if (bAllocNow) {
m_pHashTable = FX_Alloc(CAssoc*, nHashSize);
}
m_nHashTableSize = nHashSize;
}
FX_BOOL CFX_MapPtrToPtr::RemoveKey(void* key) {
if (!m_pHashTable) {
return FALSE;
}
CAssoc** ppAssocPrev;
ppAssocPrev = &m_pHashTable[HashKey(key) % m_nHashTableSize];
CAssoc* pAssoc;
for (pAssoc = *ppAssocPrev; pAssoc; pAssoc = pAssoc->pNext) {
if (pAssoc->key == key) {
*ppAssocPrev = pAssoc->pNext;
FreeAssoc(pAssoc);
return TRUE;
}
ppAssocPrev = &pAssoc->pNext;
}
return FALSE;
}
void CFX_MapPtrToPtr::FreeAssoc(CFX_MapPtrToPtr::CAssoc* pAssoc) {
pAssoc->pNext = m_pFreeList;
m_pFreeList = pAssoc;
m_nCount--;
ASSERT(m_nCount >= 0);
if (m_nCount == 0) {
RemoveAll();
}
}
|