-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_array.c
More file actions
140 lines (113 loc) · 2.65 KB
/
dynamic_array.c
File metadata and controls
140 lines (113 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// written by Sam Goodrick
// contact at sdg31@zips.uakron.edu
#include "dynamic_array.h"
#include <malloc.h>
void construct( dynamic_array* arr )
{
arr->capacity = 4;
arr->array = malloc( arr->capacity * sizeof( double ) );
arr->size = 0;
arr->first = arr->array;
arr->last = arr->array;
arr->limit = arr->array + arr->capacity;
}
//
void destruct( dynamic_array* arr )
{
free( arr->array );
arr->array = arr->first = arr->last = arr->limit = NULL;
}
void resize( dynamic_array* arr )
{
double* temp;
arr->capacity *= 2;
temp = realloc( arr->array, arr->capacity * sizeof( double ) );
if( temp == NULL )
{
printf( "unable to reallocate array %p\n", arr );
return;
}
arr->first = arr->array;
arr->last = arr->array + arr->size;
arr->limit = arr->array + arr->capacity;
}
void reserve( dynamic_array* arr, int n )
{
double* temp;
temp = realloc( arr->array, arr->capacity * n * sizeof( double ) );
if( temp == NULL )
{
printf( "unable to reserve %d elements in array %p\n", n, arr );
return;
}
arr->first = arr->array;
arr->last = arr->array + arr->size;
arr->limit = arr->array + arr->capacity;
}
double* push_back( dynamic_array* arr, double data )
{
// if the array has run out of space, double it
if( arr->last == arr->limit )
{
resize( arr );
}
*(arr->last) = data;
++arr->size;
return arr->last++;
}
// remove the last element and decrement the last pointer
double pop_back( dynamic_array* arr )
{
double r = *back( arr );
*(arr->last - 1) = 0.0;
--arr->last;
return r;
}
double* insert( dynamic_array* arr, int pos, double data )
{
// data can be inserted anywhere within the array
// as well as at the front or back
if( pos > arr->size )
{
printf( "error: insertion into unitialized memory.\n" );
return NULL;
}
// if the array has run out of space, double it
if( arr->last == arr->limit )
{
resize( arr );
}
double* temp = malloc( arr->capacity * sizeof( double ) );
int i;
// copy everything before the insertion point into the new array
for( i = 0; i < pos; ++i )
{
temp[i] = arr->array[i];
}
// make the insertion at the specified point
temp[pos] = data;
// copy the remainder of the array into the new array
for( i = pos + 1; i <= arr->last - arr->first; ++i )
{
temp[i] = arr->array[i - 1];
}
arr->array = temp;
++arr->size;
arr->first = arr->array;
arr->last = arr->array + arr->size;
arr->limit = arr->array + arr->capacity;
// return a pointer to the inserted element
return arr->array + pos;
}
double index( dynamic_array* arr, int n )
{
return arr->array[n];
}
double* front( dynamic_array* arr )
{
return arr->array;
}
double* back( dynamic_array* arr )
{
return arr->last - 1;
}