-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructs.c
231 lines (216 loc) · 5.97 KB
/
Structs.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
/**
* @file TreeAnalyzer.c
* @author Adi Bamberger Edri <[email protected]>
* @version 1.0
* @date 12 Dec 2019
*
* @brief System which uses the generic RBTree and implements the functions needed for it
*
* @section DESCRIPTION
* The system holds the casted function to be used in the generic RBtree, for data of type string
* and of type of a vector.
*/
// ------------------------------ includes ------------------------------
#include "RBTree.h"
#include "Structs.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
// -------------------------- const definitions -------------------------
// Stands for the being less than, equal to or greater then what we are comparing to
#define LESS (-1)
#define EQUAL (0)
#define GREATER (1)
// The error massage that is to be printed if an error occurred when using malloc
#define ERR_MALLOC "Memory allocation failed\n"
// Stands for success or failure of a function
#define FAILURE (0)
#define SUCCESS (1)
// ------------------------------ functions -----------------------------
/**
* CompFunc for strings (assumes strings end with "\0")
* @param a - char* pointer
* @param b - char* pointer
* @return equal to 0 iff a == b. lower than 0 if a < b. Greater than 0 iff b < a. (lexicographic
* order)
*/
int stringCompare(const void *a, const void *b)
{
return strcmp((char *) a, (char *) b);
}
/**
* ForEach function that concatenates the given word to pConcatenated. pConcatenated is already allocated with
* enough space.
* @param word - char* to add to pConcatenated
* @param pConcatenated - char*
* @return 0 on failure, other on success
*/
int concatenate(const void *word, void *pConcatenated)
{
char *copyTo = (char *) pConcatenated;
char *toCopy = (char *) word;
strcat(strcat(copyTo, toCopy), "\n");
return SUCCESS;
}
/**
* FreeFunc for strings
*/
void freeString(void *s)
{
if (s != NULL)
{
free((char *) s);
}
}
/**
* CompFunc for Vectors, compares element by element, the vector that has the first larger
* element is considered larger. If vectors are of different lengths and identify for the length
* of the shorter vector, the shorter vector is considered smaller.
* @param a - first vector
* @param b - second vector
* @return equal to 0 iff a == b. lower than 0 if a < b. Greater than 0 iff b < a.
*/
int vectorCompare1By1(const void *a, const void *b)
{
if (a == NULL || b == NULL)
{
return FAILURE;
}
Vector *va = (Vector *) a;
Vector *vb = (Vector *) b;
if (vb->vector == NULL || va->vector == NULL)
{
return FAILURE;
}
int i = 0;
while (vb->len > i && va->len > i)
{
double vaElement = (va->vector)[i];
double vbElement = (vb->vector)[i];
if (vaElement < vbElement)
{
return LESS;
}
if (vaElement > vbElement)
{
return GREATER;
}
i++;
}
if (va->len < vb->len)
{
return LESS;
}
if (va->len > vb->len)
{
return GREATER;
}
return EQUAL;
}
/**
* FreeFunc for vectors
*/
void freeVector(void *pVector)
{
Vector *theVector = (Vector *) pVector;
if (theVector != NULL)
{
if (theVector->vector != NULL)
{
free(theVector->vector);
theVector->vector = NULL;
}
free(theVector);
}
}
/**
* @brief calculate the norm (without the root) of the vector given
* @param pVector the vector to calculate it's norm
* @return the squared norm of the given vector
*/
double calculateNorm(const Vector *pVector)
{
double norm = 0;
for (int i = 0; i < pVector->len; i++)
{
norm += ((pVector->vector)[i]) * ((pVector->vector)[i]);
}
return norm;
}
/**
* @brief copies the information from the current vector to the max vector
* @param curVector the vector to copy the information from
* @param maxVector the vector to copy the information to
* @return 1 on success, 0 on failure
*/
int copyLarger(const Vector *curVector, Vector *maxVector)
{
maxVector->len = curVector->len;
for (int i = 0; i < curVector->len; i++)
{
(maxVector->vector)[i] = (curVector->vector)[i];
}
return SUCCESS;
}
/**
* copy pVector to pMaxVector if : 1. The norm of pVector is greater then the norm of pMaxVector.
* 2. pMaxVector->vector == NULL.
* @param pVector pointer to Vector
* @param pMaxVector pointer to Vector
* @return 1 on success, 0 on failure (if pVector == NULL: failure).
*/
int copyIfNormIsLarger(const void *pVector, void *pMaxVector)
{
Vector *curVector = (Vector *) pVector;
Vector *maxVector = (Vector *) pMaxVector;
if (curVector == NULL || maxVector == NULL)
{
return FAILURE;
}
if (maxVector->vector == NULL)
{
maxVector->vector = (double *) malloc((curVector->len) * sizeof(double));
if (maxVector->vector == NULL)
{
fprintf(stderr, "%s", ERR_MALLOC);
return FAILURE;
}
return copyLarger(curVector, maxVector);
}
if (calculateNorm(curVector) > calculateNorm(maxVector))
{
maxVector->vector = (double *) realloc(maxVector->vector,
(curVector->len) * sizeof(double));
if (maxVector->vector == NULL)
{
fprintf(stderr, "%s", ERR_MALLOC);
return FAILURE;
}
return copyLarger(curVector, maxVector);
}
return SUCCESS;
}
/**
* @param tree a pointer to a tree of Vectors
* @return pointer to a *copy* of the vector that has the largest norm (L2 Norm).
*/
Vector *findMaxNormVectorInTree(RBTree *tree)
{
if (tree == NULL)
{
return NULL;
}
Vector *newVector = (Vector *) malloc(sizeof(Vector));
if (newVector == NULL)
{
fprintf(stderr, "%s", ERR_MALLOC);
return NULL;
}
newVector->len = 0;
newVector->vector = NULL;
if (forEachRBTree(tree, copyIfNormIsLarger, newVector) == 0)
{
return NULL;
}
return newVector;
}