Design Pattern Examples
Overview of object-oriented design patterns
uintarray.c
Go to the documentation of this file.
1
5
6#include <stdlib.h>
7#include <memory.h>
8#include "uintarray.h"
9
11// UIntArray_Initialize()
14{
15 if (array != NULL)
16 {
17 array->data = NULL;
18 array->length = 0;
19 array->allocatedLength = 0;
20 }
21}
22
24// UIntArray_Clear()
27{
28 if (array != NULL)
29 {
30 free(array->data);
32 }
33}
34
36// UIntArray_AddInt()
38bool UIntArray_AddInt(UIntArray* array, uint32_t value)
39{
40 bool success = false;
41
42 if (array != NULL)
43 {
44 uint32_t* new_list = NULL;
45 if (array->data == NULL)
46 {
47 new_list = calloc(1, sizeof(uint32_t));
48 array->allocatedLength = 1;
49 }
50 else if (array->length < array->allocatedLength)
51 {
52 new_list = array->data;
53 }
54 else
55 {
56 size_t newCount = (array->length + 1);
57 new_list = realloc(array->data, newCount * sizeof(uint32_t));
58 array->allocatedLength = newCount;
59 }
60 if (new_list != NULL)
61 {
62 array->data = new_list;
63 array->data[array->length] = value;
64 array->length++;
65 success = true;
66 }
67 }
68
69 return success;
70}
71
73// UIntArray_RemoveInt()
75void UIntArray_RemoveInt(UIntArray* array, int removeIndex)
76{
77 if (array != NULL && array->data != NULL)
78 {
79 if (removeIndex >= 0 && (size_t)removeIndex < array->length)
80 {
81 for (size_t index = removeIndex; index < array->allocatedLength - 1; index++)
82 {
83 array->data[index] = array->data[index + 1];
84 }
85 array->length--;
86 }
87 }
88}
89
91// UIntArray_Find()
93int UIntArray_Find(UIntArray* array, uint32_t value)
94{
95 int foundIndex = -1;
96
97 if (array != NULL && array->data != NULL)
98 {
99 for (size_t index = 0; index < array->length; index++)
100 {
101 if (array->data[index] == value)
102 {
103 foundIndex = (int)index;
104 break;
105 }
106 }
107 }
108
109 return foundIndex;
110}
111
112
114// UIntArray_Copy()
116bool UIntArray_Copy(UIntArray* sourceArray, UIntArray* destinationArray)
117{
118 bool success = false;
119
120 if (sourceArray != NULL && destinationArray != NULL)
121 {
122 if (sourceArray->data != NULL)
123 {
124 UIntArray_Clear(destinationArray);
125 size_t new_size = sourceArray->allocatedLength * sizeof(uint32_t);
126 uint32_t* new_list = malloc(new_size);
127 if (new_list != NULL)
128 {
129 memcpy(new_list, sourceArray->data, new_size);
130 destinationArray->data = new_list;
131 destinationArray->allocatedLength = new_size;
132 destinationArray->length = sourceArray->length;
133 success = true;
134 }
135 }
136 }
137
138 return success;
139}
Represents an array of 32-bit unsigned integers. The data field points to a block of memory allocated...
Definition: uintarray.h:24
size_t allocatedLength
Number of elements that the data array can hold.
Definition: uintarray.h:27
uint32_t * data
Pointer to array of 32-bit unsigned integers.
Definition: uintarray.h:25
size_t length
Number of 32-bit unsigned integers actually in the data array.
Definition: uintarray.h:26
void UIntArray_Initialize(UIntArray *array)
Initialize the given UIntArray object.
Definition: uintarray.c:13
int UIntArray_Find(UIntArray *array, uint32_t value)
Search the given UIntArray object for the specified value and return the index of that found value.
Definition: uintarray.c:93
bool UIntArray_Copy(UIntArray *sourceArray, UIntArray *destinationArray)
Copy the source UIntArray to the destination UIntArray. The destination UIntArray is erased before ge...
Definition: uintarray.c:116
void UIntArray_Clear(UIntArray *array)
Clear the given UIntArray object so it can be reused again. Releases the list of integers.
Definition: uintarray.c:26
bool UIntArray_AddInt(UIntArray *array, uint32_t value)
Add an unsigned 32-bit integer to the given UIntArray object.
Definition: uintarray.c:38
void UIntArray_RemoveInt(UIntArray *array, int removeIndex)
Remove the unsigned 32-bit integer from the given UIntArray object at the given index....
Definition: uintarray.c:75
Declaration of the UIntArray structure and the supporting functions that represents an array of 32-bi...