1 /* 2 * PROJECT: ReactOS Cicero 3 * LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later) 4 * PURPOSE: Cicero dynamic array 5 * COPYRIGHT: Copyright 2023 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com> 6 */ 7 8 #include "precomp.h" 9 #include "cicarray.h" 10 11 CicArrayBase::CicArrayBase(size_t cbItem) 12 { 13 m_cbItem = cbItem; 14 m_pb = NULL; 15 m_cItems = m_cCapacity = 0; 16 } 17 18 CicArrayBase::~CicArrayBase() 19 { 20 cicMemFree(m_pb); 21 } 22 23 LPVOID CicArrayBase::Append(size_t cGrow) 24 { 25 if (!Insert(m_cItems, cGrow)) 26 return NULL; 27 return &m_pb[(m_cItems - cGrow) * m_cbItem]; 28 } 29 30 BOOL CicArrayBase::Insert(size_t iItem, size_t cGrow) 31 { 32 size_t cNewCapacity = m_cItems + cGrow; 33 if (m_cCapacity < cNewCapacity) 34 { 35 if (cNewCapacity <= m_cItems + m_cItems / 2) 36 cNewCapacity = m_cItems + m_cItems / 2; 37 38 LPBYTE pbNew = (LPBYTE)cicMemReAlloc(m_pb, cNewCapacity * m_cbItem); 39 if (!pbNew) 40 return FALSE; 41 42 m_pb = pbNew; 43 m_cCapacity = cNewCapacity; 44 } 45 46 if (iItem < m_cItems) 47 { 48 MoveMemory(&m_pb[(cGrow + iItem) * m_cbItem], 49 &m_pb[iItem * m_cbItem], 50 (m_cItems - iItem) * m_cbItem); 51 } 52 53 m_cItems += cGrow; 54 return TRUE; 55 } 56 57 void CicArrayBase::Remove(size_t iItem, size_t cRemove) 58 { 59 if (iItem + cRemove < m_cItems) 60 { 61 MoveMemory(&m_pb[iItem * m_cbItem], 62 &m_pb[(iItem + cRemove) * m_cbItem], 63 (m_cItems - iItem - cRemove) * m_cbItem); 64 } 65 66 m_cItems -= cRemove; 67 68 size_t cHalfCapacity = m_cCapacity / 2; 69 if (cHalfCapacity <= m_cItems) 70 return; 71 72 LPBYTE pb = (LPBYTE)cicMemReAlloc(m_pb, cHalfCapacity * m_cbItem); 73 if (pb) 74 { 75 m_pb = pb; 76 m_cCapacity = cHalfCapacity; 77 } 78 } 79