Skylicht Engine
Loading...
Searching...
No Matches
CArrayUtils.h
1/*
2!@
3MIT License
4
5Copyright (c) 2022 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the Rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27namespace Skylicht
28{
29 template <class T>
30 class SKYLICHT_API CFastArray
31 {
32 protected:
33 core::array<T> m_array;
34 T* m_ptr;
35 int m_alloc;
36 int m_count;
37 public:
38 CFastArray() :
39 m_alloc(0),
40 m_count(0),
41 m_ptr(NULL)
42 {
43
44 }
45
46 inline void reset()
47 {
48 m_count = 0;
49 }
50
51 inline void clear()
52 {
53 m_count = 0;
54 m_alloc = 0;
55 m_array.clear();
56 }
57
58 inline T* pointer()
59 {
60 return m_ptr;
61 }
62
63 inline core::array<T>& getArray()
64 {
65 return m_array;
66 }
67
68 inline int count()
69 {
70 return m_count;
71 }
72
73 void push(T element)
74 {
75 if (m_count + 1 >= m_alloc)
76 {
77 m_alloc = (m_count + 1) * 2;
78 if (m_alloc < 32)
79 m_alloc = 32;
80 m_array.set_used(m_alloc);
81 m_ptr = m_array.pointer();
82 }
83
84 m_ptr[m_count++] = element;
85 }
86
87 T* getPush()
88 {
89 if (m_count + 1 >= m_alloc)
90 {
91 m_alloc = (m_count + 1) * 2;
92 if (m_alloc < 32)
93 m_alloc = 32;
94 m_array.set_used(m_alloc);
95 m_ptr = m_array.pointer();
96 }
97
98 return &(m_ptr[m_count++]);
99 }
100
101 void alloc(int count)
102 {
103 int allocCount = (m_count + 1) * 2;
104 while (allocCount < count)
105 {
106 allocCount *= 2;
107 }
108
109 m_alloc = allocCount;
110 m_array.set_used(m_alloc);
111 m_ptr = m_array.pointer();
112 }
113 };
114}
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29