Skip to content

Commit be134f9

Browse files
Merge pull request #71 from dreamer-coding/add_array
2 parents 9ffea82 + a8d2aea commit be134f9

File tree

6 files changed

+1240
-2
lines changed

6 files changed

+1240
-2
lines changed

code/logic/array.c

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
/**
2+
* -----------------------------------------------------------------------------
3+
* Project: Fossil Logic
4+
*
5+
* This file is part of the Fossil Logic project, which aims to develop
6+
* high-performance, cross-platform applications and libraries. The code
7+
* contained herein is licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License. You may obtain
9+
* a copy of the License at:
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
* Author: Michael Gene Brockus (Dreamer)
20+
* Date: 04/05/2014
21+
*
22+
* Copyright (C) 2014-2025 Fossil Logic. All rights reserved.
23+
* -----------------------------------------------------------------------------
24+
*/
25+
#include "fossil/tofu/array.h"
26+
27+
// *****************************************************************************
28+
// Function prototypes
29+
// *****************************************************************************
30+
31+
fossil_array_t* fossil_array_create_container(char* type) {
32+
fossil_array_t* array = (fossil_array_t*)fossil_tofu_alloc(sizeof(fossil_array_t));
33+
if (array == NULL) {
34+
return NULL;
35+
}
36+
array->data = (fossil_tofu_t*)fossil_tofu_alloc(INITIAL_CAPACITY * sizeof(fossil_tofu_t));
37+
if (array->data == NULL) {
38+
fossil_tofu_free(array);
39+
return NULL;
40+
}
41+
array->size = 0;
42+
array->capacity = INITIAL_CAPACITY;
43+
array->type = type;
44+
return array;
45+
}
46+
47+
fossil_array_t* fossil_array_create_default(void) {
48+
return fossil_array_create_container("any");
49+
}
50+
51+
fossil_array_t* fossil_array_create_copy(const fossil_array_t* other) {
52+
fossil_array_t* array = (fossil_array_t*)fossil_tofu_alloc(sizeof(fossil_array_t));
53+
if (array == NULL) {
54+
return NULL;
55+
}
56+
array->data = (fossil_tofu_t*)fossil_tofu_alloc(other->capacity * sizeof(fossil_tofu_t));
57+
if (array->data == NULL) {
58+
fossil_tofu_free(array);
59+
return NULL;
60+
}
61+
array->size = other->size;
62+
array->capacity = other->capacity;
63+
array->type = other->type;
64+
for (size_t i = 0; i < other->size; i++) {
65+
array->data[i] = other->data[i];
66+
}
67+
return array;
68+
}
69+
70+
fossil_array_t* fossil_array_create_move(fossil_array_t* other) {
71+
fossil_array_t* array = (fossil_array_t*)fossil_tofu_alloc(sizeof(fossil_array_t));
72+
if (array == NULL) {
73+
return NULL;
74+
}
75+
array->data = other->data;
76+
array->size = other->size;
77+
array->capacity = other->capacity;
78+
array->type = other->type;
79+
other->data = NULL;
80+
other->size = 0;
81+
other->capacity = 0;
82+
other->type = NULL;
83+
return array;
84+
}
85+
86+
void fossil_array_destroy(fossil_array_t* array) {
87+
if (array == NULL) {
88+
return;
89+
}
90+
fossil_tofu_free(array->data);
91+
fossil_tofu_free(array);
92+
}
93+
94+
void fossil_array_push_back(fossil_array_t* array, char *element) {
95+
if (array == NULL) {
96+
return;
97+
}
98+
if (array->size == array->capacity) {
99+
fossil_tofu_t* new_data = (fossil_tofu_t*)fossil_tofu_alloc(2 * array->capacity * sizeof(fossil_tofu_t));
100+
if (new_data == NULL) {
101+
return;
102+
}
103+
for (size_t i = 0; i < array->size; i++) {
104+
new_data[i] = array->data[i];
105+
}
106+
fossil_tofu_free(array->data);
107+
array->data = new_data;
108+
array->capacity *= 2;
109+
}
110+
array->data[array->size++] = fossil_tofu_create(array->type, element);
111+
}
112+
113+
void fossil_array_push_front(fossil_array_t* array, char *element) {
114+
if (array == NULL) {
115+
return;
116+
}
117+
if (array->size == array->capacity) {
118+
fossil_tofu_t* new_data = (fossil_tofu_t*)fossil_tofu_alloc(2 * array->capacity * sizeof(fossil_tofu_t));
119+
if (new_data == NULL) {
120+
return;
121+
}
122+
for (size_t i = 0; i < array->size; i++) {
123+
new_data[i + 1] = array->data[i];
124+
}
125+
fossil_tofu_free(array->data);
126+
array->data = new_data;
127+
array->capacity *= 2;
128+
} else {
129+
for (size_t i = array->size; i > 0; i--) {
130+
array->data[i] = array->data[i - 1];
131+
}
132+
}
133+
array->data[0] = fossil_tofu_create(array->type, element);
134+
array->size++;
135+
}
136+
137+
void fossil_array_push_at(fossil_array_t* array, size_t index, char *element) {
138+
if (array == NULL || index > array->size) {
139+
return;
140+
}
141+
if (array->size == array->capacity) {
142+
fossil_tofu_t* new_data = (fossil_tofu_t*)fossil_tofu_alloc(2 * array->capacity * sizeof(fossil_tofu_t));
143+
if (new_data == NULL) {
144+
return;
145+
}
146+
for (size_t i = 0; i < index; i++) {
147+
new_data[i] = array->data[i];
148+
}
149+
for (size_t i = index; i < array->size; i++) {
150+
new_data[i + 1] = array->data[i];
151+
}
152+
fossil_tofu_free(array->data);
153+
array->data = new_data;
154+
array->capacity *= 2;
155+
} else {
156+
for (size_t i = array->size; i > index; i--) {
157+
array->data[i] = array->data[i - 1];
158+
}
159+
}
160+
array->data[index] = fossil_tofu_create(array->type, element);
161+
array->size++;
162+
}
163+
164+
void fossil_array_pop_back(fossil_array_t* array) {
165+
if (array == NULL || array->size == 0) {
166+
return;
167+
}
168+
fossil_tofu_destroy(&array->data[--array->size]);
169+
}
170+
171+
void fossil_array_pop_front(fossil_array_t* array) {
172+
if (array == NULL || array->size == 0) {
173+
return;
174+
}
175+
fossil_tofu_destroy(&array->data[0]);
176+
for (size_t i = 0; i < array->size - 1; i++) {
177+
array->data[i] = array->data[i + 1];
178+
}
179+
array->size--;
180+
}
181+
182+
void fossil_array_pop_at(fossil_array_t* array, size_t index) {
183+
if (array == NULL || index >= array->size) {
184+
return;
185+
}
186+
fossil_tofu_destroy(&array->data[index]);
187+
for (size_t i = index; i < array->size - 1; i++) {
188+
array->data[i] = array->data[i + 1];
189+
}
190+
array->size--;
191+
}
192+
193+
void fossil_array_erase(fossil_array_t* array) {
194+
if (array == NULL) {
195+
return;
196+
}
197+
for (size_t i = 0; i < array->size; i++) {
198+
fossil_tofu_destroy(&array->data[i]);
199+
}
200+
array->size = 0;
201+
}
202+
203+
bool fossil_array_is_cnullptr(const fossil_array_t* array) {
204+
return array == NULL;
205+
}
206+
207+
bool fossil_array_not_cnullptr(const fossil_array_t* array) {
208+
return array != NULL;
209+
}
210+
211+
bool fossil_array_is_empty(const fossil_array_t* array) {
212+
return array == NULL || array->size == 0;
213+
}
214+
215+
bool fossil_array_not_empty(const fossil_array_t* array) {
216+
return array != NULL && array->size > 0;
217+
}
218+
219+
size_t fossil_array_size(const fossil_array_t* array) {
220+
return array == NULL ? 0 : array->size;
221+
}
222+
223+
size_t fossil_array_capacity(const fossil_array_t* array) {
224+
return array == NULL ? 0 : array->capacity;
225+
}
226+
227+
char *fossil_array_get(const fossil_array_t* array, size_t index) {
228+
return array == NULL || index >= array->size ? NULL : fossil_tofu_get_value(&array->data[index]);
229+
}
230+
231+
char *fossil_array_get_front(const fossil_array_t* array) {
232+
return array == NULL || array->size == 0 ? NULL : fossil_tofu_get_value(&array->data[0]);
233+
}
234+
235+
char *fossil_array_get_back(const fossil_array_t* array) {
236+
return array == NULL || array->size == 0 ? NULL : fossil_tofu_get_value(&array->data[array->size - 1]);
237+
}
238+
239+
char *fossil_array_get_at(const fossil_array_t* array, size_t index) {
240+
return array == NULL || index >= array->size ? NULL : fossil_tofu_get_value(&array->data[index]);
241+
}
242+
243+
void fossil_array_set(fossil_array_t* array, size_t index, char *element) {
244+
if (array == NULL || index >= array->size) {
245+
return;
246+
}
247+
fossil_tofu_set_value(&array->data[index], element);
248+
}
249+
250+
void fossil_array_set_front(fossil_array_t* array, char *element) {
251+
if (array == NULL || array->size == 0) {
252+
return;
253+
}
254+
fossil_tofu_set_value(&array->data[0], element);
255+
}
256+
257+
void fossil_array_set_back(fossil_array_t* array, char *element) {
258+
if (array == NULL || array->size == 0) {
259+
return;
260+
}
261+
fossil_tofu_set_value(&array->data[array->size - 1], element);
262+
}
263+
264+
void fossil_array_set_at(fossil_array_t* array, size_t index, char *element) {
265+
if (array == NULL || index >= array->size) {
266+
return;
267+
}
268+
fossil_tofu_set_value(&array->data[index], element);
269+
}

0 commit comments

Comments
 (0)