-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.h
158 lines (133 loc) · 3.47 KB
/
tree.h
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdlib.h>
#include "generic_vector.h"
#include "my_string.h"
//The KNOWN TYPE
struct vector
{
int size;
int capacity;
MY_STRING* data;
};
typedef struct vector Vector;
GENERIC_VECTOR generic_vector_init_default()
{
int i;
Vector* pVector = (Vector*) malloc(sizeof(Vector));
//worked
pVector->size = 0;
pVector->capacity = 1;
pVector->data = (MY_STRING*)malloc(sizeof(MY_STRING) * pVector->capacity);
if (pVector->data == NULL)
{
free(pVector);
return NULL;
}
for (i = 0; i < pVector->capacity; i++)
{
pVector->data[i] = my_string_init_default();
}
return (GENERIC_VECTOR)pVector;
}
Status generic_vector_push_back(GENERIC_VECTOR hVector, MY_STRING item)
{
Vector* pVector = (Vector*) hVector;
//MY_STRING* temp;
int i;
if (pVector->size >= pVector->capacity) //no room
{
pVector->data = (MY_STRING*) realloc(pVector->data, sizeof(MY_STRING) * pVector->capacity * 2);
if(pVector->data == NULL){
return FAILURE;
}
for(i = pVector->size; i < pVector->capacity * 2; i++){
pVector->data[i] = my_string_init_default();
}
/*temp = (MY_STRING*) malloc(sizeof(MY_STRING)* pVector->capacity * 2);
if (temp == NULL)
{
return FAILURE;
}
for (i = 0; i < pVector->size; i++)
{
temp[i] = pVector->data[i];
}*/ //BUG IS HERE. We have copied all the old data but what is in the
//rest of the array? We expect it to be filled with NULL values but
//they are uninitialized instead.
/*for (i; i < pVector->capacity * 2; i++)
{
temp[i] = NULL;
}*/
//free(pVector->data);
//pVector->data = temp;
pVector->capacity *= 2;
}
my_string_assignment(&pVector->data[pVector->size], item);
pVector->size += 1;
return SUCCESS;
}
int generic_vector_get_size(GENERIC_VECTOR hVector)
{
Vector* pVector = (Vector*)hVector;
return pVector->size;
}
int generic_vector_get_capacity(GENERIC_VECTOR hVector)
{
Vector* pVector = (Vector*)hVector;
return pVector->capacity;
}
Boolean generic_vector_empty(GENERIC_VECTOR hVector)
{
Vector* pVector = (Vector*)hVector;
return (Boolean) (pVector->size == 0);
}
MY_STRING* generic_vector_at(GENERIC_VECTOR hVector, unsigned int index)
{
Vector* pVector = (Vector*)hVector;
if (index >= pVector->size) //out of bounds!
{
//printf("vector subscript out of range.\n");
return NULL;
}
//my_string_insertion(pVector->data[index], stdout);
return &(pVector->data[index]);
}
Status generic_vector_pop_back(GENERIC_VECTOR hVector)
{
Vector* pVector = (Vector*)hVector;
if (pVector->size == 0)
{
return FAILURE;
}
free(pVector->data[pVector->size-1]);
pVector->size--;
return SUCCESS;
}
void generic_vector_destroy(GENERIC_VECTOR* phVector)
{
Vector* pVector = (Vector*) *phVector;
int i;
for(i = 0; i < pVector->size; i++){
my_string_destroy(&pVector->data[i]);
}
free(pVector->data);
free(pVector);
*phVector = NULL;
return;
}
void generic_vector_assignment(GENERIC_VECTOR* leftVector, GENERIC_VECTOR rightVector){
Vector* pVector1 = (Vector*)*leftVector;
Vector* pVector2 = (Vector*)rightVector;
int i;
pVector1->size = pVector2->size;
pVector1->capacity = pVector2->capacity;
pVector1->data = (MY_STRING*) realloc(pVector1->data, sizeof(MY_STRING) * pVector2->capacity);
for(i = 0; i < pVector1->size; i++){
if(pVector1->data[i] == NULL){
pVector1->data[i] = my_string_init_default();
}
}
for(i = 0; i < pVector1->size; i++){
my_string_assignment(&pVector1->data[i], pVector2->data[i]);
}
return;
}