ReactOS 0.4.15-dev-8408-g466a198
cicarray.cpp
Go to the documentation of this file.
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
12{
13 m_cbItem = cbItem;
14 m_pb = NULL;
16}
17
19{
21}
25 if (!Insert(m_cItems, cGrow))
26 return NULL;
27 return &m_pb[(m_cItems - cGrow) * m_cbItem];
28}
29
30BOOL 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
57void CicArrayBase::Remove(size_t iItem, size_t cRemove)
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}
static void cicMemFree(LPVOID ptr)
Definition: cicbase.h:27
static LPVOID cicMemReAlloc(LPVOID ptr, SIZE_T newSize)
Definition: cicbase.h:20
LPVOID Append(size_t cGrow)
Definition: cicarray.cpp:23
void Remove(size_t iItem, size_t cRemove=1)
Definition: cicarray.cpp:57
BOOL Insert(size_t iItem, size_t cGrow)
Definition: cicarray.cpp:30
size_t m_cCapacity
Definition: cicarray.h:16
virtual ~CicArrayBase()
Definition: cicarray.cpp:18
size_t m_cbItem
Definition: cicarray.h:16
CicArrayBase(size_t cbItem)
Definition: cicarray.cpp:11
size_t m_cItems
Definition: cicarray.h:16
LPBYTE m_pb
Definition: cicarray.h:15
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned char * LPBYTE
Definition: typedefs.h:53
#define MoveMemory
Definition: winbase.h:1709