-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.cpp
205 lines (162 loc) · 4.64 KB
/
list.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "list.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
// internal helper functions
char list_is_full(list *listo)
{
return(listo->item_count == listo->current_max_size);
}
void list_grow(list *old_listo)
{
int i;
list new_listo;
list_make(&new_listo, old_listo->current_max_size*2, old_listo->growable++);
for(i=0; i<old_listo->current_max_size; i++)
list_add_item(&new_listo, old_listo->items[i] , old_listo->names[i]);
list_free(old_listo);
//copy new structure to old list
old_listo->names = new_listo.names;
old_listo->items = new_listo.items;
old_listo->item_count = new_listo.item_count;
old_listo->current_max_size = new_listo.current_max_size;
old_listo->growable = new_listo.growable;
}
//end helpers
void list_make(list *listo, int start_size, char growable)
{
listo->names = (char**) malloc(sizeof(char*) * start_size);
listo->items = (void**) malloc(sizeof(void*) * start_size);
listo->item_count = 0;
listo->current_max_size = start_size;
listo->growable = growable;
}
int list_add_item(list *listo, void *item, char *name)
{
int name_length;
char *new_name;
if( list_is_full(listo) )
{
if( listo->growable )
list_grow(listo);
else
return -1;
}
listo->names[listo->item_count] = NULL;
if(name != NULL)
{
name_length = strlen(name);
new_name = (char*) malloc(sizeof(char) * name_length + 1);
strncpy(new_name, name, name_length);
listo->names[listo->item_count] = new_name;
}
listo->items[listo->item_count] = item;
listo->item_count++;
return listo->item_count-1;
}
char* list_print_items(list *listo)
{
int i;
for(i=0; i < listo->item_count; i++)
{
printf("%s\n", listo->names[i]);
}
return NULL;
}
void* list_get_index(list *listo, int indx)
{
if(indx < listo->item_count)
return listo->items[indx];
return NULL;
}
void* list_get_item(list *listo, void *item_to_find)
{
int i = 0;
for(i=0; i < listo->item_count; i++)
{
if(listo->items[i] == item_to_find)
return listo->items[i];
}
return NULL;
}
void* list_get_name(list *listo, char *name_to_find)
{
int i = 0;
for(i=0; i < listo->item_count; i++)
{
if(strncmp(listo->names[i], name_to_find, strlen(name_to_find)) == 0)
return listo->items[i];
}
return NULL;
}
int list_find(list *listo, char *name_to_find)
{
int i = 0;
for(i=0; i < listo->item_count; i++)
{
if(strncmp(listo->names[i], name_to_find, strlen(name_to_find)) == 0)
return i;
}
return -1;
}
void list_delete_item(list *listo, void *item)
{
int i;
for(i=0; i < listo->item_count; i++)
{
if( listo->items[i] == item )
list_delete_index(listo, i);
}
}
void list_delete_name(list *listo, char *name)
{
int i;
//int j;
//char remove = 0;
// int length_name = strlen(name);
int item_name;
if(name == NULL)
return;
for(i=0; i < listo->item_count; i++)
{
item_name = strlen(name);
if( name != NULL && (strncmp(listo->names[i], name, strlen(name)) == 0) )
list_delete_index(listo, i);
}
}
void list_delete_index(list *listo, int indx)
{
int j;
//remove item
if(listo->names[indx] != NULL)
free(listo->names[indx]);
//restructure
for(j=indx; j < listo->item_count-1; j++)
{
listo->names[j] = listo->names[j+1];
listo->items[j] = listo->items[j+1];
}
listo->item_count--;
return;
}
void list_delete_all(list *listo)
{
int i;
for(i=listo->item_count-1; i>=0; i--)
list_delete_index(listo, i);
}
void list_free(list *listo)
{
list_delete_all(listo);
free(listo->names);
free(listo->items);
}
void list_print_list(list *listo)
{
int i;
printf("count: %i/%i\n", listo->item_count, listo->current_max_size);
for(i=0; i < listo->item_count; i++)
{
printf("list[%i]: %s\n", i, listo->names[i]);
}
}