-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathArrayQueue.c
122 lines (94 loc) · 2.91 KB
/
ArrayQueue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (c) scott.cgi All Rights Reserved.
*
* This source code belongs to project Mojoc, which is a pure C Game Engine hosted on GitHub.
* The Mojoc Game Engine is licensed under the MIT License, and will continue to be iterated with coding passion.
*
* License : https://github.com/scottcgi/Mojoc/blob/master/LICENSE
* GitHub : https://github.com/scottcgi/Mojoc
* CodeStyle: https://github.com/scottcgi/Mojoc/blob/master/Docs/CodeStyle.md
*
* Since : 2015-6-5
* Update : 2019-1-17
* Author : scott.cgi
*/
#include <stdlib.h>
#include "Engine/Toolkit/Utils/ArrayQueue.h"
#include "Engine/Toolkit/Platform/Log.h"
static void* Enqueue(ArrayQueue* arrayQueue, void* elementPtr)
{
if (arrayQueue->headIndex > 0 && arrayQueue->headIndex == arrayQueue->elementList->elementArr->length)
{
AArrayList->RemoveRange(arrayQueue->elementList, 0, arrayQueue->headIndex - 1);
arrayQueue->headIndex = 0;
}
return AArrayList->Add(arrayQueue->elementList, elementPtr);
}
static void* Dequeue(ArrayQueue* arrayQueue, void* defaultElementPtr)
{
if (arrayQueue->headIndex == arrayQueue->elementList->size)
{
return defaultElementPtr;
}
return (char*) arrayQueue->elementList->elementArr->data +
arrayQueue->elementList->elementTypeSize *
(arrayQueue->headIndex++);
}
static void RemoveAt(ArrayQueue* arrayQueue, int index)
{
ALog_A
(
index >= arrayQueue->headIndex && index < arrayQueue->elementList->size,
"AArrayQueue RemoveAt index = %d, out of range [%d, %d]",
index, arrayQueue->headIndex, arrayQueue->elementList->size - 1
);
AArrayList->Remove(arrayQueue->elementList, index);
}
static void Release(ArrayQueue* arrayQueue)
{
arrayQueue->headIndex = 0;
AArrayList->Release(arrayQueue->elementList);
}
static void InitWithCapacity(int elementTypeSize, int capacity, ArrayQueue* outArrayQueue)
{
if (capacity == 0)
{
AArrayList->Init(elementTypeSize, outArrayQueue->elementList);
}
else
{
AArrayList->InitWithCapacity(elementTypeSize, capacity, outArrayQueue->elementList);
}
outArrayQueue->headIndex = 0;
}
static ArrayQueue* CreateWithCapacity(int elementTypeSize, int capacity)
{
ArrayQueue* arrayQueue = malloc(sizeof(ArrayQueue));
InitWithCapacity(elementTypeSize, capacity, arrayQueue);
return arrayQueue;
}
static void Init(int elementTypeSize, ArrayQueue* outArrayQueue)
{
InitWithCapacity(elementTypeSize, 0, outArrayQueue);
}
static ArrayQueue* Create(int elementTypeSize)
{
return CreateWithCapacity(elementTypeSize, 0);
}
static void Clear(ArrayQueue* arrayQueue)
{
arrayQueue->headIndex = 0;
AArrayList->Clear(arrayQueue->elementList);
}
struct AArrayQueue AArrayQueue[1] =
{{
Create,
Init,
CreateWithCapacity,
InitWithCapacity,
Release,
Enqueue,
Dequeue,
RemoveAt,
Clear,
}};