-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.h
628 lines (606 loc) · 17.7 KB
/
array.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/*
================================================================================
Name : array.h
Author : Randu Karisa
Version :
Copyright : Your copyright notice
Description : Set of functions to create, populate and print an array.
: Contains structures IntegerArray, DoubleArray, StringArray,
: String2DArray
================================================================================
*/
#ifndef ARRAY_H
#define ARRAY_H
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define ARRAY_LENGTH 100 //The block of memory allocate to the array
#define FALSE 0
#define TRUE 1
/***************************************************************************************
* AN ARRAY OF INTEGERS
* ************************************************************************************/
/**
*IntegerArray Structure
*@attrib size, Pointer to an integer holding the size of the array
*@attrib length, Pointer to an integer holding the allocated
* memory size in heap.
*@attrib array, Pointer to an integer holding the array of integers
*/
struct IntegerArray{
int size;
int length;
int* array;
};
/**
* getIntegerArraySize function, gets the size of the array of an
* holding integers
* @param array, the array to determine its size
* @return size, the size of the array
*/
const int* getIntegerArraySize(const struct IntegerArray* array)
{
return &(array->size);
}
/**
* getIntegerValue function, Gets the integer of the array at the
* given index
*@param index, the index of the integer
*@return pointer to integer value in the array
*Note: The application exits if the index is out of range
*/
const int* getIntegerValue(const struct IntegerArray* array,const int index)
{
if(index > -1 && index < array->size)
return (array->array+(index));
else
{
printf("RuntimeError: Array Index %d is out of bounds, Accepted indices 0-%d\n",index,array->size-1);
exit(1);
}
}
/**
*setIntegerValue function, Sets the value at the given index of the array
*@param array, The array to set the value at the given index
*@param index, The value to set in the array
*/
void setIntegerValue(struct IntegerArray* array,const int value, const int index)
{
if(index < array->size)
*(array->array+index) = value;
}
/**
*reverseIntegerArray function, Reverses the array
*@param array, the array to reverse
*/
void reverseIntegerArray(struct IntegerArray* array)
{
int end = array->size;
for (int i = 0; i < end; ++i) {
int temp = *getIntegerValue(array, i);
setIntegerValue(array, *getIntegerValue(array, end-1),i);
setIntegerValue(array, temp,end-1);
end--;
}
}
/**
* newIntegerArray function, create a new array of integers and allocates
* memory in the heap
* @return array, an array of integers
*/
struct IntegerArray *newIntegerArray()
{
struct IntegerArray *array =(struct IntegerArray*) malloc(sizeof(struct IntegerArray));
array->array = malloc(sizeof(int)*ARRAY_LENGTH);
array->length = ARRAY_LENGTH;
array->size = 0;
return array;
}
/**
*addToIntegerArray function, Adds an integer value to an array
*@param array, the array to and the value at the end
*@param value, the integer value to store in the array
*/
void addToIntegerArray(struct IntegerArray* array,const int value)
{
if(array->size == array->length)
{
int length = array->length;
int* newPointer = malloc(sizeof(int)* (length+ARRAY_LENGTH));
for(int i=0; i<array->size; i++)
*(newPointer+i) = *(array->array+i);
free(array->array);
array->array = newPointer;
array->length = length+ARRAY_LENGTH;
}
int size = array->size;
*(array->array+size) = value;
array->size = (size + 1);
}
/**
*printIntegerArray function, Prints the array to the console
*@param array, The array to traverse and print its contents
*/
void printIntegerArray(const struct IntegerArray* array)
{
printf("[");
for (int i = 0; i < array->size; ++i) {
if(i == 20 && array->size > 100){
i = (array->size) - 20;
printf("... ");
}
printf("%d",*(array->array+i));
if(i != array->size-1)
printf(", ");
}
printf("]\n");
}
/**
* freeIntegerArray function, Frees the memory to an array of integers
*@param array, The array to free its memory
*/
void freeIntegerArray(struct IntegerArray* array)
{
free(array->array);
free(array);
}
/***************************************************************************************
* AN ARRAY OF DOUBLES
* ************************************************************************************/
/**
*DoubleArray structure, A structure to hold the array and its attributes
*@attrib size, Stores the size of the array
*@attrib length, Stores the allocated block length in heap
*@attrib array, A pointer to an array of doubles
*/
struct DoubleArray{
int size;
int length;
double* array;
};
/**
*getDoubleArraySize function, gets the size of the array
*@param array, The array to get its size
*/
const int* getDoubleArraySize(const struct DoubleArray* array)
{
return &array->size;
}
/*
*getDoubleValue function, Gets the value at the given index
*@param array, The array to get the value from
*@param index, The index of the value
*/
const double* getDoubleValue(const struct DoubleArray* array, const int index)
{
if(index > -1 && index < array->size)
return (array->array+index);
else
{
printf("RuntimeError: Array Index %d is out of bounds, Accepted indices 0-%d\n",index,array->size-1);
exit(1);
}
}
/**
*setDoubleValue function, sets the value in the array at the given index
*@param array, The array to set its value
*@param index, The index to set the value in the array
*@param value, The value to be set
*/
void setDoubleValue(struct DoubleArray* array,const double value,const int index)
{
if(index > -1 && index < array->size)
*(array->array+index) = value;
else
{
printf("RuntimeError: Array Index %d is out of bounds, Accepted indices 0-%d\n",index,array->size-1);
exit(1);
}
}
/**
*reverseDoubleArray function, Reverses the array
*@param array, The array to reverse
*/
void reverseDoubleArray(struct DoubleArray* array)
{
int end = array->size;
for (int i = 0; i < end; ++i) {
double temp = *getDoubleValue(array, i);
setDoubleValue(array, i, *getDoubleValue(array, end-1));
setDoubleValue(array, end-1, temp);
end--;
}
}
/**
*newDoubleArray function, Creates a new array
*@return array, Pointer to the newly created array of doubles
*/
struct DoubleArray *newDoubleArray()
{
struct DoubleArray *array =(struct DoubleArray*) malloc(sizeof(struct DoubleArray));
array->array = malloc(sizeof(double)*ARRAY_LENGTH);
array->length = ARRAY_LENGTH;
array->size = 0;
return array;
}
/**
*addToDoubleArray function, Adds a value to the array
*@param array, The array to add values
*@param value, The value to be added
*/
void addToDoubleArray(struct DoubleArray* array,const double value)
{
if(array->size == array->length)
{
int length = array->length;
double* newPointer = malloc(sizeof(double)* (length+ARRAY_LENGTH));
for(int i=0; i<array->size; i++)
*(newPointer+i) = *(array->array+i);
free(array->array);
array->array = newPointer;
array->length = length+ARRAY_LENGTH;
}
int size = array->size;
*(array->array+size) = value;
array->size = (size + 1);
}
/**
*printDoubleArray function, Prints the array to the console
*@param array, The array to print its contents
*/
void printDoubleArray(const struct DoubleArray* array)
{
printf("[");
for (int i = 0; i < array->size; ++i) {
if(i == 20 && array->size > 100){
i = (array->size) - 20;
printf("... ");
}
printf("%.2f",*(array->array+i));
if(i != array->size-1)
printf(", ");
}
printf("]\n");
}
/**
*freeDoubleArray function, Frees memory allocated to the array in heap
*/
void freeDoubleArray(struct DoubleArray* array)
{
free(array->array);
free(array);
}
/***************************************************************************************
* AN ARRAY OF CHARACTERS ==> <String>
* ************************************************************************************/
/**
* String structure, A structure representing a string
*Attrib size,The length of the string
*Attrib string, Pointer to an array of chars
*/
struct String{
int size;
char* string;
};
/**
*newString function, Creates a new string and allocate memory in heap
*@param stringLiteral, Pointer to characters to create the string from
*@return string, Pointer to the string
*/
struct String* newString(char* stringLiteral)
{
char* string = malloc(sizeof(char)*(strlen(stringLiteral)+1));
struct String* str = (struct String*) malloc( sizeof(struct String));
str->size = strlen(stringLiteral);
strcpy(string,stringLiteral);
str->string = string;
return str;
}
/**
*changeString function, Changes the string contents to the newly provides
*@param string, The string to change
*@param stringLiteral, The string to change too
*/
void changeString(struct String* string,const char* stringLiteral)
{
char* str = malloc(sizeof(char)*(strlen(stringLiteral)+1));
free(string->string);
strcpy(str,stringLiteral);
string->string = str;
string->size = strlen(stringLiteral);
}
/**
*freeString function, The string to free its memory
*/
void freeString(struct String* string)
{
free(string->string);
free(string);
}
/***************************************************************************************
* AN ARRAY OF STRINGS ==> ArrayList<String>
* ************************************************************************************/
/**
*StringArray structure, represents an array of strings
*@Attrib size, The size of the array
*@Attrib length, The allocated block of memory in heap
*@Attrib array, The array of strings
*/
struct StringArray{
int size;
int length;
char** array;
};
/**
*getStringArraySize function, Gets the size of the array
*@param array, The array to get its size
*@return size, Reference to the size
*/
const int *getStringArraySize(const struct StringArray* array)
{
return &array->size;
}
/*
*getString function, Gets the string at the specified index
*@param array, The array to get its the string from
*@param index, The location where the string is
*@return string, Pointer to the string
*Note: Application exits with a message if index is out of bounds
*/
const char* getString(const struct StringArray *array,const int index)
{
if(index > -1 && index < array->size)
return *(array->array+index);
else
{
printf("RuntimeError: Array Index %d is out of bounds, Accepted indices 0-%d\n",index,array->size-1);
exit(1);
}
}
/**
*changeStringArray function, Changes the value of the array at a given index
*@param array, The array to change its value
*@param stringLiteral, The value to change
*@param index, The location to change the string
*/
void changeStringArray(struct StringArray* array,const char* stringLiteral,const int index)
{
if(index > -1 && index < array->size){
free(*(array->array+index));
char* string = malloc(sizeof(char)*(strlen(stringLiteral)+1));
strcpy(string,stringLiteral);
*(array->array+index) = string;
}else
{
printf("RuntimeError: Array Index %d is out of bounds, Accepted indices 0-%d\n",index,array->size-1);
exit(1);
}
}
/**
* reverseStringArray function, Reverses the array
*/
void reverseStringArray(struct StringArray* array)
{
int end = array->size;
for (int i = 0; i < end; ++i) {
const char* temp = getString(array, i);
changeStringArray(array, getString(array, end-1),i);
changeStringArray(array,temp, end-1);
end--;
}
}
/**
*newStringArray function, Create a new array and allocate memory in heap
*@return array, The newly created array
*/
struct StringArray *newStringArray()
{
struct StringArray *array =(struct StringArray*) malloc(sizeof(struct StringArray));
array->array = malloc(sizeof(char*)*ARRAY_LENGTH);
array->length = ARRAY_LENGTH;
array->size = 0;
return array;
}
/**
*addToStringArray function, Adds a string to the array
*@param array, The array to populate
*@param string, The string to be added
*/
void addToStringArray(struct StringArray* array,const char* string)
{
if(array->size == array->length)
{
int length = array->length;
char** newPointer = malloc(sizeof(char*)* (length+ARRAY_LENGTH));
for(int i=0; i<array->size; i++)
*(newPointer+i) = *(array->array+i);
free(array->array);
array->array = newPointer;
array->length = length+ARRAY_LENGTH;
}
int size = array->size;
char* s = malloc(sizeof(char)*strlen(string)+1);
strcpy(s,string);
*(array->array+size) = s;
array->size = (size + 1);
}
/**
*printStringArray function, Prints the array to the console
*@param array, The array to print
*/
void printStringArray(const struct StringArray* array)
{
printf("[");
for (int i = 0; i < array->size; ++i) {
if(i == 20 && array->size > 100){
i = (array->size) - 20;
printf("... ");
}
printf("%s",*(array->array+i));
if(i != array->size-1)
printf(", ");
}
printf("]\n");
}
/**
*freeStringArray function, Frees the array in memory
*@param array, The array to free its memory
*/
void freeStringArray(struct StringArray* array)
{
for (int i = 0; i < array->size; ++i)
free(*(array->array+i));
free(array->array);
free(array);
}
/***************************************************
* STRING 2D ARRAY
**************************************************/
/**
*String2DArray structure, Represents a two dimensional array of strings
*@Attrib length, The block length of the memory allocation in heap
*@Attrib size, The size of the array i.e size of rows
*@Attrib sets, A string representing a set of data belonging to an index
*@Attrib setSize, The size of the blocks/sets of the array
*@Attrib indexColumn, The column to create the block from
*/
struct String2DArray{
struct StringArray** stringArray;
int length;
int size;
struct String** sets;
int setsSize;
int indexColumn;
};
/**
*newString2DArray function, Creates a new two dimensional array and allocate
* memory in heap
*@return array, The two dimensional array
*/
struct String2DArray *newString2DArray()
{
struct String2DArray *array =(struct String2DArray*) malloc(sizeof(struct String2DArray));
array->stringArray = (struct StringArray**) malloc(sizeof(struct StringArray*)*ARRAY_LENGTH);
array->sets = (struct String**) malloc(sizeof(char*)*ARRAY_LENGTH);
array->length = ARRAY_LENGTH;
array->size = 0;
array->setsSize = 0;
array->indexColumn = 0;
return array;
}
/**
*addToString2DArray function, Adds an array of strings to the 2D array
*@param array, The 2D array
*@param pointer, The one dimension array
*@param indexColumn, Specifies the column to be used as index,
* Important for fast retrieval of data.
*/
void addToString2DArray(struct String2DArray* array, struct StringArray* pointer,const int indexColumn)
{
if(array->size == array->length)
{
int length = array->length;
struct StringArray** newPointer = (struct StringArray**) malloc(sizeof(struct StringArray*)* (length+ARRAY_LENGTH));
for(int i=0; i<array->size; i++)
{
*(newPointer+i) = *(array->stringArray+i);
}
free(array->stringArray);
array->stringArray = newPointer;
array->length = length+ARRAY_LENGTH;
struct String** newStringPointer = (struct String**) malloc(sizeof(struct String*)* (array->length));
for (int i = 0; i < array->setsSize; ++i) {
*(newStringPointer+i) = *(array->sets+i);
}
free(array->sets);
array->sets = newStringPointer;
}
int size = array->size;
*(array->stringArray+size) = pointer;
int setsSize = array->setsSize;
if(size == 0){
if(indexColumn >= pointer->size || indexColumn < 0)
{
printf("The index column %d is out of Column Bounds, Highest index is %d\n",indexColumn,(pointer->size-1));
exit(1);
}
array->indexColumn = indexColumn;
int length = strlen(getString(pointer, array->indexColumn)) + 3;
char start[length];
strcpy(start,getString(pointer, array->indexColumn));
strcat(start,",0");
*(array->sets+setsSize) = newString(start);
array->setsSize++;
}
else
{
if(strcmp(getString(pointer, indexColumn),getString(*(array->stringArray+size-1), indexColumn)) != 0){
int len = (int) log10(array->size) + 3;
char indice[len];
struct String* s = *(array->sets+setsSize-1);
char currentKeys[len+s->size];
strcpy(currentKeys,s->string);
sprintf(indice,",%ld",array->size);
strcat(currentKeys,indice);
struct String* ns = *(array->sets+setsSize-1);
changeString(ns, currentKeys);
char next[len];
sprintf(next,",%ld",array->size);
int length = strlen(getString(pointer, array->indexColumn)) + len;
char start[length];
strcpy(start,getString(pointer, array->indexColumn));
strcat(start,next);
*(array->sets+array->setsSize) = newString(start);
array->setsSize++;
}
}
array->size++;
}
/**
*printString2DArray function, Prints the 2D array
*@param array, The 2D array to print
*/
void printString2DArray(const struct String2DArray* data)
{
for (int i = 0; i < data->size; ++i) {
if(i == 20 && data->size > 100){
i = (data->size) - 20;
printf("\n");
}
printf("%-10d",i);
struct StringArray* innerArray = *(data->stringArray+i);
for( int j=0; j<innerArray->size; j++)
printf("%-10s ", *(innerArray->array+j) );
printf("\n");
}
}
/**
*freeString2DArray function, Frees the memory for the array
*@param data, The 2D array to free its memory
*/
void freeString2DArray(struct String2DArray* data)
{
for (int i = 0; i < data->size; ++i)
freeStringArray(*(data->stringArray+i));
for (int i = 0; i < data->setsSize; ++i)
freeString( (struct String*) *(data->sets+i));
free(data->sets);
free(data->stringArray);
free(data);
}
/**
*freePartString2DArray function, Frees a part of the 2D array since some
* of its data points to the parent array
* @param data, The array to free its memory
*/
void freePartString2DArray(struct String2DArray* data)
{
for (int i = 0; i < data->setsSize; ++i) {
freeString( (struct String*) *(data->sets+i));
}
free(data->stringArray);
free(data->sets);
free(data);
}
#endif