Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1168)

Side by Side Diff: core/src/fxcrt/fx_basic_maps.cpp

Issue 1580573002: Replace more CFX_MapPtrToPtr and remove it. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: And remove it. Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/include/fxcrt/fx_basic.h"
8 #include "plex.h"
9
10 CFX_MapPtrToPtr::CFX_MapPtrToPtr(int nBlockSize)
11 : m_pHashTable(NULL),
12 m_nHashTableSize(17),
13 m_nCount(0),
14 m_pFreeList(NULL),
15 m_pBlocks(NULL),
16 m_nBlockSize(nBlockSize) {
17 ASSERT(m_nBlockSize > 0);
18 }
19 void CFX_MapPtrToPtr::RemoveAll() {
20 FX_Free(m_pHashTable);
21 m_pHashTable = NULL;
22 m_nCount = 0;
23 m_pFreeList = NULL;
24 m_pBlocks->FreeDataChain();
25 m_pBlocks = NULL;
26 }
27 CFX_MapPtrToPtr::~CFX_MapPtrToPtr() {
28 RemoveAll();
29 ASSERT(m_nCount == 0);
30 }
31 FX_DWORD CFX_MapPtrToPtr::HashKey(void* key) const {
32 return ((FX_DWORD)(uintptr_t)key) >> 4;
33 }
34 void CFX_MapPtrToPtr::GetNextAssoc(FX_POSITION& rNextPosition,
35 void*& rKey,
36 void*& rValue) const {
37 ASSERT(m_pHashTable);
38 CAssoc* pAssocRet = (CAssoc*)rNextPosition;
39 ASSERT(pAssocRet);
40 if (pAssocRet == (CAssoc*)-1) {
41 for (FX_DWORD nBucket = 0; nBucket < m_nHashTableSize; nBucket++) {
42 if ((pAssocRet = m_pHashTable[nBucket]) != NULL)
43 break;
44 }
45 ASSERT(pAssocRet);
46 }
47 CAssoc* pAssocNext;
48 if ((pAssocNext = pAssocRet->pNext) == NULL) {
49 for (FX_DWORD nBucket = (HashKey(pAssocRet->key) % m_nHashTableSize) + 1;
50 nBucket < m_nHashTableSize; nBucket++) {
51 if ((pAssocNext = m_pHashTable[nBucket]) != NULL) {
52 break;
53 }
54 }
55 }
56 rNextPosition = (FX_POSITION)pAssocNext;
57 rKey = pAssocRet->key;
58 rValue = pAssocRet->value;
59 }
60 FX_BOOL CFX_MapPtrToPtr::Lookup(void* key, void*& rValue) const {
61 FX_DWORD nHash;
62 CAssoc* pAssoc = GetAssocAt(key, nHash);
63 if (!pAssoc) {
64 return FALSE;
65 }
66 rValue = pAssoc->value;
67 return TRUE;
68 }
69 void* CFX_MapPtrToPtr::GetValueAt(void* key) const {
70 FX_DWORD nHash;
71 CAssoc* pAssoc = GetAssocAt(key, nHash);
72 if (!pAssoc) {
73 return NULL;
74 }
75 return pAssoc->value;
76 }
77 void*& CFX_MapPtrToPtr::operator[](void* key) {
78 FX_DWORD nHash;
79 CAssoc* pAssoc;
80 if ((pAssoc = GetAssocAt(key, nHash)) == NULL) {
81 if (!m_pHashTable) {
82 InitHashTable(m_nHashTableSize);
83 }
84 pAssoc = NewAssoc();
85 pAssoc->key = key;
86 pAssoc->pNext = m_pHashTable[nHash];
87 m_pHashTable[nHash] = pAssoc;
88 }
89 return pAssoc->value;
90 }
91 CFX_MapPtrToPtr::CAssoc* CFX_MapPtrToPtr::GetAssocAt(void* key,
92 FX_DWORD& nHash) const {
93 nHash = HashKey(key) % m_nHashTableSize;
94 if (!m_pHashTable) {
95 return NULL;
96 }
97 CAssoc* pAssoc;
98 for (pAssoc = m_pHashTable[nHash]; pAssoc; pAssoc = pAssoc->pNext) {
99 if (pAssoc->key == key)
100 return pAssoc;
101 }
102 return NULL;
103 }
104 CFX_MapPtrToPtr::CAssoc* CFX_MapPtrToPtr::NewAssoc() {
105 if (!m_pFreeList) {
106 CFX_Plex* newBlock = CFX_Plex::Create(m_pBlocks, m_nBlockSize,
107 sizeof(CFX_MapPtrToPtr::CAssoc));
108 CFX_MapPtrToPtr::CAssoc* pAssoc =
109 (CFX_MapPtrToPtr::CAssoc*)newBlock->data();
110 pAssoc += m_nBlockSize - 1;
111 for (int i = m_nBlockSize - 1; i >= 0; i--, pAssoc--) {
112 pAssoc->pNext = m_pFreeList;
113 m_pFreeList = pAssoc;
114 }
115 }
116 CFX_MapPtrToPtr::CAssoc* pAssoc = m_pFreeList;
117 m_pFreeList = m_pFreeList->pNext;
118 m_nCount++;
119 ASSERT(m_nCount > 0);
120 pAssoc->key = 0;
121 pAssoc->value = 0;
122 return pAssoc;
123 }
124 void CFX_MapPtrToPtr::InitHashTable(FX_DWORD nHashSize, FX_BOOL bAllocNow) {
125 ASSERT(m_nCount == 0);
126 ASSERT(nHashSize > 0);
127 FX_Free(m_pHashTable);
128 m_pHashTable = NULL;
129 if (bAllocNow) {
130 m_pHashTable = FX_Alloc(CAssoc*, nHashSize);
131 }
132 m_nHashTableSize = nHashSize;
133 }
134 FX_BOOL CFX_MapPtrToPtr::RemoveKey(void* key) {
135 if (!m_pHashTable) {
136 return FALSE;
137 }
138 CAssoc** ppAssocPrev;
139 ppAssocPrev = &m_pHashTable[HashKey(key) % m_nHashTableSize];
140 CAssoc* pAssoc;
141 for (pAssoc = *ppAssocPrev; pAssoc; pAssoc = pAssoc->pNext) {
142 if (pAssoc->key == key) {
143 *ppAssocPrev = pAssoc->pNext;
144 FreeAssoc(pAssoc);
145 return TRUE;
146 }
147 ppAssocPrev = &pAssoc->pNext;
148 }
149 return FALSE;
150 }
151 void CFX_MapPtrToPtr::FreeAssoc(CFX_MapPtrToPtr::CAssoc* pAssoc) {
152 pAssoc->pNext = m_pFreeList;
153 m_pFreeList = pAssoc;
154 m_nCount--;
155 ASSERT(m_nCount >= 0);
156 if (m_nCount == 0) {
157 RemoveAll();
158 }
159 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698