Initial commit
[minnow.git] / src / aquarium / simplearray.c
blob1b2924d25e9036cd9eba8a7013704e05cdd5b3d5
1 #include <stdlib.h>
2 #include <string.h>
4 #include "simplearray.h"
6 void initialize_simplearray(SimpleArray *container, unsigned int size) {
7 container->contents = (void*)malloc(DEFAULT_SIMPLEARRAY_SIZE * size);
8 container->bufferSize = DEFAULT_SIMPLEARRAY_SIZE;
9 container->currentSize = 0;
10 container->elemSize = size;
13 void push_onto_simplearray(SimpleArray *container, void *value) {
14 if (container->currentSize >= container->bufferSize) {
15 container->bufferSize *= 2;
16 void* temp = (void*)realloc(container->contents, container->bufferSize * container->elemSize);
17 container->contents = temp;
20 switch (container->elemSize) {
21 case (4) : ((int*)(container->contents))[container->currentSize] = *(int *)value;
22 break;
23 case (2) : ((short*)(container->contents))[container->currentSize] = *(short *)value;
24 break;
25 case (1) : ((char*)(container->contents))[container->currentSize] = *(char *)value;
26 break;
27 default: memcpy (container->contents + container->currentSize * container->elemSize, value, container->elemSize);
28 break;
30 ++(container->currentSize);
33 void pop_off_simplearray(SimpleArray *container) {
34 if (container->currentSize > 0)
35 --(container->currentSize);
38 void insert_into_simplearray(SimpleArray *container, void* value, unsigned int pos) {
39 if (pos > container->currentSize) {
40 push_onto_simplearray(container, value);
42 else {
43 if (container->currentSize >= container->bufferSize) {
44 container->bufferSize *= 2;
45 void* temp = (void*)realloc(container->contents, container->bufferSize * container->elemSize);
46 container->contents = temp;
49 memmove(container->contents + (pos + 1) * container->elemSize, container->contents + pos * container->elemSize,
50 container->elemSize * (container->currentSize - pos));
52 switch (container->elemSize) {
53 case (4) : ((int*)(container->contents))[pos] = *(int *)value;
54 break;
55 case (2) : ((short*)(container->contents))[pos] = *(short *)value;
56 break;
57 case (1) : ((char*)(container->contents))[pos] = *(char *)value;
58 break;
59 default: memcpy (container->contents + pos * container->elemSize, value, container->elemSize);
60 break;
62 ++(container->currentSize);
65 void delete_from_simplearray(SimpleArray *container, unsigned int pos) {
66 if (pos > container->currentSize) {
67 pop_off_simplearray(container);
69 else if (pos == container->currentSize) {
70 if (pos > 0) {
71 --(container->currentSize);
74 else {
75 memmove(container->contents + pos * container->elemSize, container->contents + (pos+1) * container->elemSize,
76 container->elemSize * (container->currentSize - pos));
77 --(container->currentSize);
80 void delete_from_simplearray_range(SimpleArray *container, unsigned int pos, unsigned int amount) {
81 if (pos > container->currentSize) {
82 pop_off_simplearray(container);
84 else if (pos == container->currentSize) {
85 if (pos > 0) {
86 --(container->currentSize);
89 else {
90 memmove(container->contents + pos * container->elemSize, container->contents + (pos+amount) * container->elemSize,
91 container->elemSize * (container->currentSize - pos));
92 --(container->currentSize);