-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.c
293 lines (257 loc) · 7.3 KB
/
List.c
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright (C) 2010 University of Tartu
* Authors: Reina Käärik, Siim Orasmaa, Kristo Tammeoja, Jaak Vilo
* Contact: siim . orasmaa {at} ut . ee
*
* This file is part of Generalized Edit Distance Tool.
*
* Generalized Edit Distance Tool is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Generalized Edit Distance Tool is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Generalized Edit Distance Tool.
* If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "List.h"
// makes a copy of given string
wchar_t *copy_wchar_t(wchar_t *str, int strLen){
wchar_t *res;
int i;
// allocate memory and copy
if((res = (wchar_t *)malloc(sizeof(wchar_t)*(strLen+1))) == NULL){
puts("Error: Could not allocate memory");
exit(1);
}
res[strLen] = L'\0';
for(i = 0; i < strLen; i++)
res[i] = str[i];
return res;
}
// measures length of given string
int wchar_len(wchar_t *str){
int len;
len = 0;
while(str[len] != L'\0')
len++;
return len;
}
// creates new list
List *createList(){
List *list;
list = (List*)malloc(sizeof(List));
if(list == NULL)
abort();
list->firstItem = NULL;
return list;
}
// creates new index
Index *createIndex(int i, int j){
Index *index;
index = (Index*)malloc(sizeof(Index));
if(index == NULL)
abort();
index->i = i;
index->j = j;
index->nextIndex = NULL;
return index;
}
// creates new list item
ListItem *createListItem(double v, int i, int j){
ListItem *item;
item = (ListItem *)malloc(sizeof(ListItem));
if(item == NULL)
abort();
item->value = v;
item->index = createIndex(i, j);
item->nextItem = NULL;
return item;
}
// creates new upper-case to lower-case transformation
IgnoreCaseListElement *createIgnoreCaseElement(wchar_t *l, wchar_t *r){
IgnoreCaseListElement *e;
e = (IgnoreCaseListElement *)malloc(sizeof(IgnoreCaseListElement));
if(e == NULL)
abort();
e->left = (wchar_t *)copy_wchar_t(l, wchar_len(l));
e->right = (wchar_t *)copy_wchar_t(r, wchar_len(r));
e->next = NULL;
return e;
}
// removes last element, the first element will never be removed
int removeLastItem(ListItem *li){
ListItem *nextItem;
Index *index;
Index *nextI;
int find = 1;
while( find && li->nextItem != NULL ){
if(li->nextItem->nextItem == NULL){ // this is the item that should be removed
nextItem = li->nextItem;
li->nextItem = NULL;
lastBest = li->value;
index = nextItem->index;
nextItem->index = NULL;
while(index != NULL){
nextI = index->nextIndex;
index->nextIndex = NULL;
free(index);
index = nextI;
}
free(nextItem);
return 0;
}
li = li->nextItem;
}
return 0;
}
// Adds position indexes to given list item
int insertValues(ListItem * li, int i, int j){
Index *index;
index = li->index;
while(index->nextIndex != NULL){
index = index->nextIndex;
}
index->nextIndex = createIndex(i, j);
return 0;
}
// insert new item into the list
int insertListItem(List *list, double value, int i, int j, int isFull, int inserted){
ListItem *item;
ListItem *nextItem;
ListItem *newFirst;
int searchPlace = 1;
double v;
item = list->firstItem;
// the list is empty
if(item == NULL){
list->firstItem = createListItem(value, i, j);
lastBest = value;
inserted--;
return inserted;
}
// this listItem should be the first?
if(value < item->value){
newFirst = createListItem(value, i, j);
newFirst->nextItem = item;
list->firstItem = newFirst;
if(isFull){
removeLastItem(list->firstItem);
}
inserted--;
return inserted;
}
/* search the right place */
while(searchPlace){
// insert in the same list item
v = item->value;
if(item->value == value){
insertValues(item, i, j);
return inserted;
}
nextItem = item->nextItem;
// there is no next item
if(nextItem == NULL){
item->nextItem = createListItem(value, i, j);
lastBest = value;
inserted--;
return inserted;
}
// should we add a new list item between two existing items?
if(nextItem->value > value){
item->nextItem = createListItem(value, i, j);
item->nextItem->nextItem = nextItem;
if(isFull){
removeLastItem(list->firstItem);
}
inserted--;
return inserted;
}
item = item->nextItem;
}
return 0;
}
// Releases memory under the list
void freeList(List *list){
ListItem *item;
item = list->firstItem;
// until the list is empty
while(item != NULL){
Index *index;
index = item->index;
while(index != NULL){
Index *nextIndex;
nextIndex = index->nextIndex;
free(index);
index = nextIndex;
}
ListItem *nextItem;
nextItem = item->nextItem;
free(item);
item = nextItem;
}
free(list);
}
// adds new upper-case to lower-case transformation into the ignore case list
int insertIgnoreCaseElement(wchar_t *l, wchar_t *r){
IgnoreCaseListElement *current;
// add as first element
if(ignoreCase == NULL){
if(debug)
puts("Inserting first element to ignore case list");
ignoreCase = createIgnoreCaseElement(l, r);
return 0;
}
// first element already exists, add to the end
else{
current = ignoreCase;
while(current->next != NULL)
current = current->next;
// current->next == NULL
if(debug)
puts("Inserted element to ignore case list");
current->next = createIgnoreCaseElement(l, r);
return 0;
}
}
// frees memory under the list ignoreCase
void freeIgnoreCaseList(){
IgnoreCaseListElement *current;
current = ignoreCase;
while (current != NULL){
IgnoreCaseListElement *next;
next = current -> next;
if (current->left != NULL){
free(current->left);
}
if (current->right != NULL){
free(current->right);
}
free(current);
current = next;
}
ignoreCase = NULL;
}
// prints given list
int printList(List *list){
ListItem *item;
Index *index;
item = list->firstItem;
while(item != NULL){
printf("\n %f \n", item->value);
index = item->index;
while(index != NULL){
printf(" i: %i j: %i\n", index->i, index->j);
index = index->nextIndex;
}
puts("------------------------\n");
item = item->nextItem;
}
return 0;
}