Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
HASHTABLELIST=./hashTable/hashTableList
HASHTABLE=./hashTable
PARSING=./hashTable
PARSING=./parsing
VECTOR=./Vector

CC=gcc
CFLAGS= -g -Wall -I$(HASHTABLELIST) -I$(HASHTABLE) -I$(PARSING)
CFLAGS= -g -Wall -I$(HASHTABLELIST) -I$(HASHTABLE) -I$(PARSING) -I$(VECTOR)

OBJ1= main.o $(HASHTABLE)/hashTable.o $(HASHTABLELIST)/hashTableList.o $(PARSING)/parsing.o
OBJ1= main.o $(HASHTABLE)/hashTable.o $(HASHTABLELIST)/hashTableList.o $(PARSING)/parsing.o $(VECTOR)/vector.o

EXEC= demo

Expand Down
33 changes: 33 additions & 0 deletions Vector/vector.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>
#include <stdlib.h>

extern int d;

typedef struct vec_node{
int* coords;
}vec;
typedef vec *Vector;


Vector initVector(int *vec){
Vector v=malloc(sizeof(struct vec_node));
v->coords = malloc(d*sizeof(int));
for(int i=0;i<d;i++){
(v->coords)[i] = vec[i];
}
return v;
}

void deleteVector(Vector v){
free(v->coords);
free(v);
}


void printVector(Vector v){
printf("\n[");
for(int i=0;i<d;i++){
printf(" %d",v->coords[i]);
}
printf(" ]\n");
}
12 changes: 12 additions & 0 deletions Vector/vector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef VECTOR_H
#define VECTOR_H

typedef struct vec_node *Vector;

Vector initVector(int *);

void deleteVector(Vector);

void printVector(Vector );

#endif
Binary file added Vector/vector.o
Binary file not shown.
Binary file added demo
Binary file not shown.
69 changes: 46 additions & 23 deletions hashTable/hashTable.c
Original file line number Diff line number Diff line change
@@ -1,45 +1,66 @@
#include "hashTable.h"
#include <stdio.h>
#include <stdlib.h>
#include "../Vector/vector.h"
#include "./hashTableList/hashTableList.h"


int hashFunction(const hashtable* ht,int id){
struct hashtable_node{
int key;
List head;
};
typedef struct hashtable_node *hashtable_nodePtr;


struct hashtable_head{
hashtable_nodePtr table;
int buckets;
int numberOfVectors;
};
typedef struct hashtable_head *HashTable;



int hashFunction(const HashTable ht,int id){
return (id % ht->buckets);
}


void htInitialize(hashtable* ht,int buckets) {
ht->table=malloc(sizeof(hashtable_node)*buckets);
HashTable htInitialize(int buckets) {
HashTable ht=malloc(sizeof(struct hashtable_head));
ht->table=malloc(sizeof(struct hashtable_node)*buckets);
ht->buckets=buckets;
for (int i=0;i<buckets;i++){
ht->table[i].key=i;
ht->table[i].head=NULL;
ht->table[i].head=initializeList();
}
ht->numberOfNodes=0; // nodes counter
ht->numberOfVectors=0; // nodes counter
return ht;
}


node* htSearch(const hashtable* ht, int id){
int index=hashFunction(ht, id);
listNode* ln=listSearchId(ht->table[index].head,id);
if (ln != NULL){
// found
return &(ln->n);
}
else {
// not found
return NULL;
}
}
// hashtable_nodePtr htSearch(const HashTable ht, int id){
// int index=hashFunction(ht, id);
// listNode* ln=listSearchId(ht->table[index].head,id);
// if (ln != NULL){
// // found
// return &(ln->n);
// }
// else {
// // not found
// return NULL;
// }
// }


int htUniqueInsert(hashtable* ht, vector v){
int index=hashFunction(ht,v.id);
int htInsert(HashTable ht, Vector v){
int index=hashFunction(ht,1);
ht->table[index].head=listInsert(ht->table[index].head,v);
ht->numberOfNodes++;
ht->numberOfVectors++;
return 1;
}


void htPrint(const hashtable* ht){
void htPrint(const HashTable ht){
for (int i=0;i<ht->buckets;i++){
printf("\n>>Bucket %d: ",i+1);
listPrint(ht->table[i].head);
Expand All @@ -48,10 +69,12 @@ void htPrint(const hashtable* ht){
}


void htDelete(hashtable* ht){
HashTable htDelete(HashTable ht){
// delete whole hash table
for (int i=0;i<ht->buckets;i++){
ht->table[i].head=listDelete(ht->table[i].head);
}
free(ht->table);
free(ht);
return NULL;
}
34 changes: 8 additions & 26 deletions hashTable/hashTable.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,15 @@
#ifndef HASH_TABLE_H
#define HASH_TABLE_H

#include <stdio.h>
#include <stdlib.h>
#include "./hashTableList/hashTableList.h"

typedef struct hashtable_node hashtable_node;
typedef struct hashtable hashtable;
typedef struct hashtable_head *HashTable;

// hash table with separate chaining
// each bucket of hash table point to a linked list (bucket)

struct hashtable_node{
int key;
listNode* head;
};

struct hashtable{
hashtable_node* table;
int buckets;
int numberOfNodes;
};

int hashFunction(const hashtable*,int);
void htInitialize(hashtable *,int);
int htUniqueInsert(hashtable* ,vector);
node* htSearch(const hashtable*,int);
void htPrint(const hashtable*);
void htDelete(hashtable*);
int htDeleteNode(hashtable*,int);
int hashFunction(const HashTable,int);
HashTable htInitialize(int);
int htInsert(HashTable ,Vector);
// hashtable_nodePtr htSearch(const HashTable,int);
void htPrint(const HashTable);
HashTable htDelete(HashTable);
int htDeleteNode(HashTable,int);

#endif
Binary file added hashTable/hashTable.o
Binary file not shown.
91 changes: 46 additions & 45 deletions hashTable/hashTableList/hashTableList.c
Original file line number Diff line number Diff line change
@@ -1,75 +1,76 @@
#include "hashTableList.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../Vector/vector.h"


typedef struct listNode *List;
struct listNode {
Vector v;
List next;
};


List initializeList(){
return NULL;
}


// hash table's linked list

listNode* allocateListNode(vector v){
listNode* node=malloc(sizeof(listNode));
node->n.id=v.id;
node->n.bit=v.bit;
List allocateListNode(Vector v){
List node=malloc(sizeof(struct listNode));
node->v=v;
node->next=NULL;
return node;
}


listNode* listInsert(listNode* list,vector v){

List listInsert(List list,Vector v){
if (list==NULL){
// list is empty
listNode* node=allocateListNode(v);
list=node;
return list;
List node=allocateListNode(v);
return node;
}
//////////////////////////////////////////////////
// Just push
// listNode* node=allocateListNode(v);
// node->next=list;
// list=node;
//////////////////////////////////////////////////
// insert a new node at the end of the list
listNode* temp = list;
while (temp->next!=NULL){
temp = temp->next;
}

listNode* node=allocateListNode(v);
list->next=node;
//////////////////////////////////////////////////

List newnode = allocateListNode(v);
newnode->next = list;


return list;
}


listNode* listSearchId(listNode *list, int id){
listNode* current=list;
while (current!=NULL){
if (current->n.id==id)
return current;
current=current->next;
}
return NULL;
return newnode;
}


void listPrint(listNode *list){
if(list==NULL) return;
listNode *temp=list;
// List listSearchId(List list, int id){
// List current=list;
// while (current!=NULL){
// if (current->n.id==id)
// return current;
// current=current->next;
// }
// return NULL;
// }
//
//
void listPrint(List list){
if(list==NULL){ printf("List Empty!\n"); return;}
List temp=list;
while(temp!=NULL){
printf(" --Id:%d-Bit:%d-- ",temp->n.id,temp->n.bit);
printVector(temp->v);
temp=temp->next;
}
}


listNode* listDelete(listNode *list){
List listDelete(List list){
// delete whole list
if(list==NULL) return NULL;

listNode *current = list;
listNode *next;
List current = list;
List next;

while(current!=NULL){
next=current->next;
deleteVector(current->v);
free(current);
current=next;
}
Expand Down
34 changes: 7 additions & 27 deletions hashTable/hashTableList/hashTableList.h
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
#ifndef LIST_H
#define LIST_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct listNode listNode;
typedef struct node node;
typedef struct vector vector;

// hash table's linked list
typedef struct listNode *List;

struct vector{
int id;
unsigned int bit;
};

struct node{
int id;
unsigned int bit;
};

struct listNode {
node n;
listNode* next;
};

listNode* allocateListNode(vector);
listNode* listInsert(listNode *, vector);
listNode* listSearchId(listNode * ,int );
void listPrint(listNode *);
listNode* listDelete(listNode *);
List initializeList();
List allocateListNode(Vector);
List listInsert(List, Vector);
List listSearchId(List ,int );
void listPrint(List );
List listDelete(List );

#endif
Binary file added hashTable/hashTableList/hashTableList.o
Binary file not shown.
11 changes: 8 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "Vector/vector.h"
#include "./hashTable/hashTable.h"
#include "./parsing.h"
#include "./parsing/parsing.h"

int main(int argc, char const *argv[]) {
int d;

// test
int main(int argc, char const *argv[]) {

// test
srand(time(NULL));
readFile("testing.txt");



Expand Down
Binary file added main.o
Binary file not shown.
Loading