|
| 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/arraylist.h" |
| 26 | + |
| 27 | +// ***************************************************************************** |
| 28 | +// Function prototypes |
| 29 | +// ***************************************************************************** |
| 30 | + |
| 31 | +fossil_arraylist_t* fossil_arraylist_create_container(char* type, size_t capacity) { |
| 32 | + fossil_arraylist_t* alist = (fossil_arraylist_t*)fossil_tofu_alloc(sizeof(fossil_arraylist_t)); |
| 33 | + if (alist == NULL) { |
| 34 | + return NULL; |
| 35 | + } |
| 36 | + alist->type = fossil_tofu_strdup(type); |
| 37 | + alist->size = 0; |
| 38 | + alist->capacity = capacity; |
| 39 | + alist->items = (fossil_arraylist_node_t*)fossil_tofu_alloc(sizeof(fossil_arraylist_node_t) * capacity); |
| 40 | + if (alist->items == NULL) { |
| 41 | + fossil_tofu_free(alist->type); |
| 42 | + fossil_tofu_free(alist); |
| 43 | + return NULL; |
| 44 | + } |
| 45 | + return alist; |
| 46 | +} |
| 47 | + |
| 48 | +fossil_arraylist_t* fossil_arraylist_create_default(void) { |
| 49 | + return fossil_arraylist_create_container("any", 8); |
| 50 | +} |
| 51 | + |
| 52 | +fossil_arraylist_t* fossil_arraylist_create_copy(const fossil_arraylist_t* other) { |
| 53 | + if (other == NULL) return NULL; |
| 54 | + fossil_arraylist_t* alist = fossil_arraylist_create_container(other->type, other->capacity); |
| 55 | + if (alist == NULL) return NULL; |
| 56 | + alist->size = other->size; |
| 57 | + for (size_t i = 0; i < other->size; ++i) { |
| 58 | + alist->items[i].data = fossil_tofu_create(other->type, fossil_tofu_get_value(&other->items[i].data)); |
| 59 | + } |
| 60 | + return alist; |
| 61 | +} |
| 62 | + |
| 63 | +fossil_arraylist_t* fossil_arraylist_create_move(fossil_arraylist_t* other) { |
| 64 | + if (other == NULL) return NULL; |
| 65 | + fossil_arraylist_t* alist = (fossil_arraylist_t*)fossil_tofu_alloc(sizeof(fossil_arraylist_t)); |
| 66 | + if (alist == NULL) return NULL; |
| 67 | + alist->type = other->type; |
| 68 | + alist->size = other->size; |
| 69 | + alist->capacity = other->capacity; |
| 70 | + alist->items = other->items; |
| 71 | + other->type = NULL; |
| 72 | + other->items = NULL; |
| 73 | + other->size = 0; |
| 74 | + other->capacity = 0; |
| 75 | + return alist; |
| 76 | +} |
| 77 | + |
| 78 | +void fossil_arraylist_destroy(fossil_arraylist_t* alist) { |
| 79 | + if (alist == NULL) return; |
| 80 | + for (size_t i = 0; i < alist->size; ++i) { |
| 81 | + fossil_tofu_destroy(&alist->items[i].data); |
| 82 | + } |
| 83 | + fossil_tofu_free(alist->items); |
| 84 | + fossil_tofu_free(alist->type); |
| 85 | + fossil_tofu_free(alist); |
| 86 | +} |
| 87 | + |
| 88 | +// ***************************************************************************** |
| 89 | +// Utility functions |
| 90 | +// ***************************************************************************** |
| 91 | + |
| 92 | +int32_t fossil_arraylist_insert(fossil_arraylist_t* alist, char *data) { |
| 93 | + if (alist == NULL) return FOSSIL_TOFU_FAILURE; |
| 94 | + if (alist->size >= alist->capacity) { |
| 95 | + size_t new_capacity = alist->capacity * 2; |
| 96 | + fossil_arraylist_node_t* new_items = (fossil_arraylist_node_t*)fossil_tofu_alloc(sizeof(fossil_arraylist_node_t) * new_capacity); |
| 97 | + if (new_items == NULL) return FOSSIL_TOFU_FAILURE; |
| 98 | + for (size_t i = 0; i < alist->size; ++i) { |
| 99 | + new_items[i] = alist->items[i]; |
| 100 | + } |
| 101 | + fossil_tofu_free(alist->items); |
| 102 | + alist->items = new_items; |
| 103 | + alist->capacity = new_capacity; |
| 104 | + } |
| 105 | + alist->items[alist->size].data = fossil_tofu_create(alist->type, data); |
| 106 | + alist->size++; |
| 107 | + return FOSSIL_TOFU_SUCCESS; |
| 108 | +} |
| 109 | + |
| 110 | +int32_t fossil_arraylist_remove(fossil_arraylist_t* alist, size_t index) { |
| 111 | + if (alist == NULL || index >= alist->size) return FOSSIL_TOFU_FAILURE; |
| 112 | + fossil_tofu_destroy(&alist->items[index].data); |
| 113 | + for (size_t i = index; i < alist->size - 1; ++i) { |
| 114 | + alist->items[i] = alist->items[i + 1]; |
| 115 | + } |
| 116 | + alist->size--; |
| 117 | + return FOSSIL_TOFU_SUCCESS; |
| 118 | +} |
| 119 | + |
| 120 | +size_t fossil_arraylist_size(const fossil_arraylist_t* alist) { |
| 121 | + return (alist != NULL) ? alist->size : 0; |
| 122 | +} |
| 123 | + |
| 124 | +size_t fossil_arraylist_capacity(const fossil_arraylist_t* alist) { |
| 125 | + return (alist != NULL) ? alist->capacity : 0; |
| 126 | +} |
| 127 | + |
| 128 | +bool fossil_arraylist_not_empty(const fossil_arraylist_t* alist) { |
| 129 | + return (alist != NULL) && (alist->size > 0); |
| 130 | +} |
| 131 | + |
| 132 | +bool fossil_arraylist_not_cnullptr(const fossil_arraylist_t* alist) { |
| 133 | + return alist != NULL; |
| 134 | +} |
| 135 | + |
| 136 | +bool fossil_arraylist_is_empty(const fossil_arraylist_t* alist) { |
| 137 | + return (alist == NULL) || (alist->size == 0); |
| 138 | +} |
| 139 | + |
| 140 | +bool fossil_arraylist_is_cnullptr(const fossil_arraylist_t* alist) { |
| 141 | + return alist == NULL; |
| 142 | +} |
| 143 | + |
| 144 | +// ***************************************************************************** |
| 145 | +// Getter and setter functions |
| 146 | +// ***************************************************************************** |
| 147 | + |
| 148 | +char *fossil_arraylist_get(const fossil_arraylist_t* alist, size_t index) { |
| 149 | + if (alist == NULL || index >= alist->size) return NULL; |
| 150 | + return fossil_tofu_get_value(&alist->items[index].data); |
| 151 | +} |
| 152 | + |
| 153 | +char *fossil_arraylist_get_front(const fossil_arraylist_t* alist) { |
| 154 | + if (alist == NULL || alist->size == 0) return NULL; |
| 155 | + return fossil_tofu_get_value(&alist->items[0].data); |
| 156 | +} |
| 157 | + |
| 158 | +char *fossil_arraylist_get_back(const fossil_arraylist_t* alist) { |
| 159 | + if (alist == NULL || alist->size == 0) return NULL; |
| 160 | + return fossil_tofu_get_value(&alist->items[alist->size - 1].data); |
| 161 | +} |
| 162 | + |
| 163 | +void fossil_arraylist_set(fossil_arraylist_t* alist, size_t index, char *element) { |
| 164 | + if (alist == NULL || index >= alist->size) return; |
| 165 | + fossil_tofu_set_value(&alist->items[index].data, element); |
| 166 | +} |
| 167 | + |
| 168 | +void fossil_arraylist_set_front(fossil_arraylist_t* alist, char *element) { |
| 169 | + if (alist == NULL || alist->size == 0) return; |
| 170 | + fossil_tofu_set_value(&alist->items[0].data, element); |
| 171 | +} |
| 172 | + |
| 173 | +void fossil_arraylist_set_back(fossil_arraylist_t* alist, char *element) { |
| 174 | + if (alist == NULL || alist->size == 0) return; |
| 175 | + fossil_tofu_set_value(&alist->items[alist->size - 1].data, element); |
| 176 | +} |
0 commit comments