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
;
23 case (2) : ((short*)(container
->contents
))[container
->currentSize
] = *(short *)value
;
25 case (1) : ((char*)(container
->contents
))[container
->currentSize
] = *(char *)value
;
27 default: memcpy (container
->contents
+ container
->currentSize
* container
->elemSize
, value
, container
->elemSize
);
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
);
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
;
55 case (2) : ((short*)(container
->contents
))[pos
] = *(short *)value
;
57 case (1) : ((char*)(container
->contents
))[pos
] = *(char *)value
;
59 default: memcpy (container
->contents
+ pos
* container
->elemSize
, value
, container
->elemSize
);
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
) {
71 --(container
->currentSize
);
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
) {
86 --(container
->currentSize
);
90 memmove(container
->contents
+ pos
* container
->elemSize
, container
->contents
+ (pos
+amount
) * container
->elemSize
,
91 container
->elemSize
* (container
->currentSize
- pos
));
92 --(container
->currentSize
);