| 1 | /*
|
|---|
| 2 | * Copyright (c) 2023 SimonJRiddix
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include "include/vector.h"
|
|---|
| 30 |
|
|---|
| 31 | int vector_total(vector* v)
|
|---|
| 32 | {
|
|---|
| 33 | int totalCount = UNDEFINE;
|
|---|
| 34 | if (v)
|
|---|
| 35 | {
|
|---|
| 36 | totalCount = v->vectorList.total;
|
|---|
| 37 | }
|
|---|
| 38 | return totalCount;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | int vector_resize(vector* v, int capacity)
|
|---|
| 42 | {
|
|---|
| 43 | int status = UNDEFINE;
|
|---|
| 44 | if (v)
|
|---|
| 45 | {
|
|---|
| 46 | void** items = realloc(v->vectorList.items, sizeof(void*) * capacity);
|
|---|
| 47 | if (items)
|
|---|
| 48 | {
|
|---|
| 49 | v->vectorList.items = items;
|
|---|
| 50 | v->vectorList.capacity = capacity;
|
|---|
| 51 | status = SUCCESS;
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 | return status;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | int vector_push_back(vector* v, void* item)
|
|---|
| 58 | {
|
|---|
| 59 | int status = UNDEFINE;
|
|---|
| 60 | if (v)
|
|---|
| 61 | {
|
|---|
| 62 | if (v->vectorList.capacity == v->vectorList.total)
|
|---|
| 63 | {
|
|---|
| 64 | status = vector_resize(v, v->vectorList.capacity * 2);
|
|---|
| 65 | if (status != UNDEFINE)
|
|---|
| 66 | {
|
|---|
| 67 | v->vectorList.items[v->vectorList.total++] = item;
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 | else
|
|---|
| 71 | {
|
|---|
| 72 | v->vectorList.items[v->vectorList.total++] = item;
|
|---|
| 73 | status = SUCCESS;
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 | return status;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | int vector_set(vector* v, int index, void* item)
|
|---|
| 80 | {
|
|---|
| 81 | int status = UNDEFINE;
|
|---|
| 82 | if (v)
|
|---|
| 83 | {
|
|---|
| 84 | if ((index >= 0) && (index < v->vectorList.total))
|
|---|
| 85 | {
|
|---|
| 86 | v->vectorList.items[index] = item;
|
|---|
| 87 | status = SUCCESS;
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 | return status;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | void* vector_get(vector* v, int index)
|
|---|
| 94 | {
|
|---|
| 95 | void* readData = NULL;
|
|---|
| 96 | if (v)
|
|---|
| 97 | {
|
|---|
| 98 | if ((index >= 0) && (index < v->vectorList.total))
|
|---|
| 99 | {
|
|---|
| 100 | readData = v->vectorList.items[index];
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | return readData;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | int vector_delete(vector* v, int index)
|
|---|
| 107 | {
|
|---|
| 108 | int status = UNDEFINE;
|
|---|
| 109 | int i = 0;
|
|---|
| 110 | if (v)
|
|---|
| 111 | {
|
|---|
| 112 | if ((index < 0) || (index >= v->vectorList.total))
|
|---|
| 113 | return status;
|
|---|
| 114 | v->vectorList.items[index] = NULL;
|
|---|
| 115 | for (i = index; (i < v->vectorList.total - 1); ++i)
|
|---|
| 116 | {
|
|---|
| 117 | v->vectorList.items[i] = v->vectorList.items[i + 1];
|
|---|
| 118 | v->vectorList.items[i + 1] = NULL;
|
|---|
| 119 | }
|
|---|
| 120 | v->vectorList.total--;
|
|---|
| 121 | if ((v->vectorList.total > 0) && ((v->vectorList.total) == (v->vectorList.capacity / 4)))
|
|---|
| 122 | {
|
|---|
| 123 | vector_resize(v, v->vectorList.capacity / 2);
|
|---|
| 124 | }
|
|---|
| 125 | status = SUCCESS;
|
|---|
| 126 | }
|
|---|
| 127 | return status;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | int vector_clear(vector* v)
|
|---|
| 131 | {
|
|---|
| 132 | int status = UNDEFINE;
|
|---|
| 133 | if (v)
|
|---|
| 134 | {
|
|---|
| 135 | free(v->vectorList.items);
|
|---|
| 136 | v->vectorList.items = NULL;
|
|---|
| 137 | status = SUCCESS;
|
|---|
| 138 | }
|
|---|
| 139 | return status;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | void vector_init(vector* v)
|
|---|
| 143 | {
|
|---|
| 144 | //init function pointers
|
|---|
| 145 | v->Count = vector_total;
|
|---|
| 146 | v->Resize = vector_resize;
|
|---|
| 147 | v->Add = vector_push_back;
|
|---|
| 148 | v->Set = vector_set;
|
|---|
| 149 | v->Get = vector_get;
|
|---|
| 150 | v->Clear = vector_clear;
|
|---|
| 151 | v->Delete = vector_delete;
|
|---|
| 152 | //v->Clear = vector_clear;
|
|---|
| 153 |
|
|---|
| 154 | //initialize the capacity and allocate the memory
|
|---|
| 155 | v->vectorList.capacity = VECTOR_INIT_CAPACITY;
|
|---|
| 156 | v->vectorList.total = 0;
|
|---|
| 157 | v->vectorList.items = malloc(sizeof(void*) * v->vectorList.capacity);
|
|---|
| 158 | }
|
|---|