This repository was archived by the owner on Dec 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttributes.c
More file actions
298 lines (281 loc) · 12.7 KB
/
Attributes.c
File metadata and controls
298 lines (281 loc) · 12.7 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "Attributes.h"
#include "Books.h"
/*
* @author - Patrick Quinlan
* SID - 575275
* Last Edit - 4/3/24
*
* File that defines the hashtable of attributes that a book can have to
* characterise and group the books. Also contains other useful functions for
* interacting with the array
*/
AttributeTable create_hashtable(int n)
{
AttributeTable newtable;
newtable.size = n;
newtable.table = malloc(n * sizeof(Books));
for (int i = 0; i < n; i++)
{
newtable.table[i] = new_books_list();
}
return newtable;
}
//Hashing will be done by placing the books in their years using seperaret chaining (approximately 200 year old and enough room for 200 years in future
//Then a book will be placed in its Title letter using seperate chaining (26 spaces)
//then a book will be placed in its author letter using seperate chaining (26 spaces)
//then a book will be palced into its genre that it matches, each genre will be ordered in the hash table by alphabetical (100 genres should be sufficient)
//order using linear probing so new genres can be added to the table
int hash(AttributeTable* self, String book, int index)
{
if (index == 0)
{
return atoi(book) - 1800;
}
else if (index == 1)
{
return 400 + tolower(book[0])-97; //makes a = 0 and z = 25
}
else if (index == 2)
{
return 400 + 26 + tolower(book[0])-97; // a = 0, z = 25
}
else if (index == 3)
{
int starting_point = 400 + 26 + 26;
String low_genre = malloc(strlen(book+1));
low_genre = to_lowercase(book);
int genre_let = 452 + tolower(book[0]) - 97;
BookNodePtr bucket = (&self->table[genre_let])->head;
if (bucket == NULL)
{
return genre_let;
}
int i = genre_let;
while ( i < starting_point + 99)
{
BookNodePtr curr_bucket = (&self->table[i])->head;
if (curr_bucket == NULL)
{
return i;
}
String curr_genre = get_genre(curr_bucket);
String low_curr_genre = malloc(strlen(curr_genre + 1));
low_curr_genre = to_lowercase(curr_genre);
if (strcmp(low_curr_genre, low_genre) == 0)
{
return i;
}
else
{
i++;
}
}
}
printf("Table is FULL");
return NULL;
}
//insert a book into the attribute table (the database)
void insert_book_to_table(AttributeTable* self, BookNodePtr book)
{
String year_string = (String)malloc(5*sizeof(char));
int hash_pos;
//must insert into each different list in hash table
for (int i = 0; i <= 3; i++)
{
switch (i)
{
case(0):
sprintf(year_string, "%d", get_year(book));
hash_pos = hash(self, year_string, i);
add_ordered(&(self->table[hash_pos]), book, i);
break;
case(1):
hash_pos = hash(self, get_title(book), i);
add_ordered(&(self->table[hash_pos]), book, i);
break;
case(2):
hash_pos = hash(self, get_author(book), i);
add_ordered(&(self->table[hash_pos]), book, i);
break;
case(3):
hash_pos = hash(self, get_genre(book), i);
add_ordered(&(self->table[hash_pos]), book, i);
break;
}
}
}
//remove a book from the attribute table (remove from database)
void remove_book_from_table(AttributeTable* self, BookNodePtr book)
{
String year_string = (String)malloc(5 * sizeof(char));
int hash_pos;
for (int i = 0; i <= 3; i++)
{
switch (i)
{
case(0):
sprintf(year_string, "%d", get_year(book));
hash_pos = hash(self, year_string, i);
break;
case(1):
hash_pos = hash(self, get_title(book), i);
break;
case(2):
hash_pos = hash(self, get_author(book), i);
break;
case(3):
hash_pos = hash(self, get_genre(book), i);
break;
}
delete_book(&(self->table[hash_pos]), book);
}
}
//print the contents of the hash table
void print_table(AttributeTable* self)
{
for (int i = 0; i < self->size; i++) {
printf("%d: ", i);
print_list(&(self->table[i]));
}
}
void find_book_in_table(AttributeTable* self, String book,int search_by)
{
int table_index = hash(self, book, search_by);
find_book(&self->table[table_index], book);
}
//Test bench for attribute methods
void attribute_table_test()
{
AttributeTable test_table = create_hashtable(552);
BookNodePtr book1 = create_book("Echoes of Eternity", "Sarah Montgomery", "Fantasy", 2023);
BookNodePtr book2 = create_book("The Crimson Tide", "David Anderson", "Thriller", 2022);
BookNodePtr book3 = create_book("Quantum Entanglement", "Emily Hawkins", "Science Fiction", 2021);
BookNodePtr book4 = create_book("Unraveling Secrets", "Michael Thompson", "Mystery", 2020);
BookNodePtr book5 = create_book("The Enchanted Forest", "Sophia Wilson", "Fantasy", 2019);
BookNodePtr book6 = create_book("Cyber Warriors", "Daniel Davis", "Techno-Thriller", 2018);
BookNodePtr book7 = create_book("Eternal Embrace", "Emma Taylor", "Romance", 2017);
BookNodePtr book8 = create_book("The Quantum Puzzle", "William Martin", "Science Fiction", 2016);
BookNodePtr book9 = create_book("Whispers of the Past", "Isabella Anderson", "Historical Fiction", 2015);
BookNodePtr book10 = create_book("The Crimson Conquest", "Alexander Thompson", "Fantasy", 2014);
BookNodePtr book11 = create_book("Cyber Shadows", "Olivia Davis", "Techno-Thriller", 2013);
BookNodePtr book12 = create_book("Eternal Love", "Ava Wilson", "Romance", 2012);
BookNodePtr book13 = create_book("The Quantum Enigma", "Jacob Martin", "Science Fiction", 2011);
BookNodePtr book14 = create_book("Whispers of the Ancients", "Mia Thompson", "Historical Fiction", 2010);
BookNodePtr book15 = create_book("The Enchanted Realm", "Sophia Davis", "Fantasy", 2009);
BookNodePtr book16 = create_book("Cyber Conspiracy", "Michael Wilson", "Techno-Thriller", 2008);
BookNodePtr book17 = create_book("Eternal Passion", "Emily Martin", "Romance", 2007);
BookNodePtr book18 = create_book("The Quantum Paradox", "William Thompson", "Science Fiction", 2006);
BookNodePtr book19 = create_book("Whispers of the Sands", "Isabella Davis", "Historical Fiction", 2005);
BookNodePtr book20 = create_book("The Crimson Empire", "Alexander Wilson", "Fantasy", 2004);
BookNodePtr book21 = create_book("Cyber Infiltration", "Olivia Martin", "Techno-Thriller", 2003);
BookNodePtr book22 = create_book("Eternal Desire", "Ava Thompson", "Romance", 2002);
BookNodePtr book23 = create_book("The Quantum Conundrum", "Jacob Davis", "Science Fiction", 2001);
BookNodePtr book24 = create_book("Whispers of the Pyramids", "Mia Wilson", "Historical Fiction", 2000);
BookNodePtr book25 = create_book("The Enchanted Citadel", "Sophia Martin", "Fantasy", 1999);
BookNodePtr book26 = create_book("Cyber Sabotage", "Michael Thompson", "Techno-Thriller", 1998);
BookNodePtr book27 = create_book("Eternal Bliss", "Emily Davis", "Romance", 1997);
BookNodePtr book28 = create_book("The Quantum Dilemma", "William Wilson", "Science Fiction", 1996);
BookNodePtr book29 = create_book("Whispers of the Desert", "Isabella Martin", "Historical Fiction", 1995);
BookNodePtr book30 = create_book("The Crimson Prophecy", "Alexander Thompson", "Fantasy", 1994);
BookNodePtr book31 = create_book("Cyber Warfare", "Olivia Davis", "Techno-Thriller", 1993);
BookNodePtr book32 = create_book("Eternal Embrace", "Ava Wilson", "Romance", 1992);
BookNodePtr book33 = create_book("The Quantum Riddle", "Jacob Martin", "Science Fiction", 1991);
BookNodePtr book34 = create_book("Whispers of the Nile", "Mia Thompson", "Historical Fiction", 1990);
BookNodePtr book35 = create_book("The Enchanted Kingdom", "Sophia Davis", "Fantasy", 1989);
BookNodePtr book36 = create_book("Cyber Espionage", "Michael Wilson", "Techno-Thriller", 1988);
BookNodePtr book37 = create_book("Eternal Passion", "Emily Martin", "Romance", 1987);
BookNodePtr book38 = create_book("The Quantum Enigma", "William Thompson", "Science Fiction", 1986);
BookNodePtr book39 = create_book("Whispers of the Pharaohs", "Isabella Davis", "Historical Fiction", 1985);
BookNodePtr book40 = create_book("The Crimson Citadel", "Alexander Wilson", "Fantasy", 1984);
BookNodePtr book41 = create_book("Cyber Insurgency", "Olivia Martin", "Techno-Thriller", 1983);
BookNodePtr book42 = create_book("Eternal Desire", "Ava Thompson", "Romance", 1982);
BookNodePtr book43 = create_book("The Quantum Paradox", "Jacob Davis", "Science Fiction", 1981);
BookNodePtr book44 = create_book("Whispers of the Ancients", "Mia Wilson", "Historical Fiction", 1980);
BookNodePtr book45 = create_book("The Enchanted Fortress", "Sophia Martin", "Fantasy", 1979);
BookNodePtr book46 = create_book("Cyber Infiltration", "Michael Thompson", "Techno-Thriller", 1978);
BookNodePtr book47 = create_book("Eternal Bliss", "Emily Davis", "Romance", 1977);
BookNodePtr book48 = create_book("The Quantum Conundrum", "William Wilson", "Science Fiction", 1976);
BookNodePtr book49 = create_book("Whispers of the Pyramids", "Isabella Martin", "Historical Fiction", 1975);
BookNodePtr book50 = create_book("The Crimson Warrior", "Alexander Thompson", "Fantasy", 1974);
BookNodePtr book51 = create_book("Cyber Terrorism", "Olivia Davis", "Techno-Thriller", 1973);
BookNodePtr book52 = create_book("Eternal Embrace", "Ava Wilson", "Romance", 1972);
BookNodePtr book53 = create_book("The Quantum Dilemma", "Jacob Martin", "Science Fiction", 1971);
BookNodePtr book54 = create_book("Whispers of the Sphinx", "Mia Thompson", "Historical Fiction", 1970);
BookNodePtr book55 = create_book("The Enchanted Realm", "Sophia Davis", "Fantasy", 1969);
BookNodePtr book56 = create_book("Cyber Espionage", "Michael Wilson", "Techno-Thriller", 1968);
BookNodePtr book57 = create_book("Eternal Passion", "Emily Martin", "Romance", 1967);
BookNodePtr book58 = create_book("The Quantum Riddle", "William Thompson", "Science Fiction", 1966);
BookNodePtr book59 = create_book("Whispers of the Oasis", "Isabella Davis", "Historical Fiction", 1965);
BookNodePtr book60 = create_book("The Crimson Sorcerer", "Alexander Wilson", "Fantasy", 1964);
BookNodePtr book61 = create_book("Cyber Insurgency", "Olivia Martin", "Techno-Thriller", 1963);
BookNodePtr book62 = create_book("Eternal Desire", "Ava Thompson", "Romance", 1962);
BookNodePtr book63 = create_book("The Quantum Paradox", "Jacob Davis", "Science Fiction", 1961);
BookNodePtr book64 = create_book("Whispers of the Dunes", "Mia Wilson", "Historical Fiction", 1960);
printf("Testing inserting and hashing into hashtable");
insert_book_to_table(&test_table, book1);
insert_book_to_table(&test_table, book2);
insert_book_to_table(&test_table, book3);
insert_book_to_table(&test_table, book4);
insert_book_to_table(&test_table, book5);
insert_book_to_table(&test_table, book6);
insert_book_to_table(&test_table, book7);
insert_book_to_table(&test_table, book8);
insert_book_to_table(&test_table, book9);
insert_book_to_table(&test_table, book10);
insert_book_to_table(&test_table, book11);
insert_book_to_table(&test_table, book12);
insert_book_to_table(&test_table, book13);
insert_book_to_table(&test_table, book14);
insert_book_to_table(&test_table, book15);
insert_book_to_table(&test_table, book16);
insert_book_to_table(&test_table, book17);
insert_book_to_table(&test_table, book18);
insert_book_to_table(&test_table, book19);
insert_book_to_table(&test_table, book20);
insert_book_to_table(&test_table, book21);
insert_book_to_table(&test_table, book22);
insert_book_to_table(&test_table, book23);
insert_book_to_table(&test_table, book24);
insert_book_to_table(&test_table, book25);
insert_book_to_table(&test_table, book26);
insert_book_to_table(&test_table, book27);
insert_book_to_table(&test_table, book28);
insert_book_to_table(&test_table, book29);
insert_book_to_table(&test_table, book30);
insert_book_to_table(&test_table, book31);
insert_book_to_table(&test_table, book32);
insert_book_to_table(&test_table, book33);
insert_book_to_table(&test_table, book34);
insert_book_to_table(&test_table, book35);
insert_book_to_table(&test_table, book36);
insert_book_to_table(&test_table, book37);
insert_book_to_table(&test_table, book38);
insert_book_to_table(&test_table, book39);
insert_book_to_table(&test_table, book40);
insert_book_to_table(&test_table, book41);
insert_book_to_table(&test_table, book42);
insert_book_to_table(&test_table, book43);
insert_book_to_table(&test_table, book48);
insert_book_to_table(&test_table, book49);
insert_book_to_table(&test_table, book50);
insert_book_to_table(&test_table, book51);
insert_book_to_table(&test_table, book52);
insert_book_to_table(&test_table, book53);
insert_book_to_table(&test_table, book54);
insert_book_to_table(&test_table, book55);
insert_book_to_table(&test_table, book56);
insert_book_to_table(&test_table, book57);
insert_book_to_table(&test_table, book58);
insert_book_to_table(&test_table, book59);
insert_book_to_table(&test_table, book60);
insert_book_to_table(&test_table, book61);
insert_book_to_table(&test_table, book62);
insert_book_to_table(&test_table, book63);
insert_book_to_table(&test_table, book64);
print_table(&test_table);
find_book_in_table(&test_table, "the", 1);
}