OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "../../include/fxcrt/fx_basic.h" | 7 #include "../../include/fxcrt/fx_basic.h" |
8 #include "plex.h" | 8 #include "plex.h" |
9 | 9 |
10 CFX_MapPtrToPtr::CFX_MapPtrToPtr(int nBlockSize) | 10 CFX_MapPtrToPtr::CFX_MapPtrToPtr(int nBlockSize) |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 int count = 0; | 341 int count = 0; |
342 int size = m_Buffer.GetSize(); | 342 int size = m_Buffer.GetSize(); |
343 for (int i = 0; i < size; i++) { | 343 for (int i = 0; i < size; i++) { |
344 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(i); | 344 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(i); |
345 if (pKey->m_CompactLen != 0xfe) { | 345 if (pKey->m_CompactLen != 0xfe) { |
346 count++; | 346 count++; |
347 } | 347 } |
348 } | 348 } |
349 return count; | 349 return count; |
350 } | 350 } |
351 extern "C" { | |
352 static int _CompareDWord(const void* p1, const void* p2) { | |
353 return (*(FX_DWORD*)p1) - (*(FX_DWORD*)p2); | |
354 } | |
355 }; | |
356 struct _DWordPair { | |
357 FX_DWORD key; | |
358 FX_DWORD value; | |
359 }; | |
360 FX_BOOL CFX_CMapDWordToDWord::Lookup(FX_DWORD key, FX_DWORD& value) const { | |
361 void* pResult = FXSYS_bsearch(&key, m_Buffer.GetBuffer(), | |
362 m_Buffer.GetSize() / sizeof(_DWordPair), | |
363 sizeof(_DWordPair), _CompareDWord); | |
364 if (pResult == NULL) { | |
365 return FALSE; | |
366 } | |
367 value = ((FX_DWORD*)pResult)[1]; | |
368 return TRUE; | |
369 } | |
370 FX_POSITION CFX_CMapDWordToDWord::GetStartPosition() const { | |
371 FX_DWORD count = m_Buffer.GetSize() / sizeof(_DWordPair); | |
372 if (count == 0) { | |
373 return NULL; | |
374 } | |
375 return (FX_POSITION)1; | |
376 } | |
377 void CFX_CMapDWordToDWord::GetNextAssoc(FX_POSITION& pos, | |
378 FX_DWORD& key, | |
379 FX_DWORD& value) const { | |
380 if (pos == 0) { | |
381 return; | |
382 } | |
383 FX_DWORD index = ((FX_DWORD)(uintptr_t)pos) - 1; | |
384 FX_DWORD count = m_Buffer.GetSize() / sizeof(_DWordPair); | |
385 _DWordPair* buf = (_DWordPair*)m_Buffer.GetBuffer(); | |
386 key = buf[index].key; | |
387 value = buf[index].value; | |
388 if (index == count - 1) { | |
389 pos = 0; | |
390 } else { | |
391 pos = (FX_POSITION)((uintptr_t)pos + 1); | |
392 } | |
393 } | |
394 void CFX_CMapDWordToDWord::SetAt(FX_DWORD key, FX_DWORD value) { | |
395 FX_DWORD count = m_Buffer.GetSize() / sizeof(_DWordPair); | |
396 _DWordPair* buf = (_DWordPair*)m_Buffer.GetBuffer(); | |
397 _DWordPair pair = {key, value}; | |
398 if (count == 0 || key > buf[count - 1].key) { | |
399 m_Buffer.AppendBlock(&pair, sizeof(_DWordPair)); | |
400 return; | |
401 } | |
402 int low = 0, high = count - 1; | |
403 while (low <= high) { | |
404 int mid = (low + high) / 2; | |
405 if (buf[mid].key < key) { | |
406 low = mid + 1; | |
407 } else if (buf[mid].key > key) { | |
408 high = mid - 1; | |
409 } else { | |
410 buf[mid].value = value; | |
411 return; | |
412 } | |
413 } | |
414 m_Buffer.InsertBlock(low * sizeof(_DWordPair), &pair, sizeof(_DWordPair)); | |
415 } | |
416 void CFX_CMapDWordToDWord::EstimateSize(FX_DWORD size, FX_DWORD grow_by) { | |
417 m_Buffer.EstimateSize(size, grow_by); | |
418 } | |
OLD | NEW |