Skip to content
Snippets Groups Projects
Commit 7bcd4731 authored by emsouza's avatar emsouza
Browse files

[ADD

] MAKEFILE
parent f6098b5c
No related branches found
No related tags found
No related merge requests found
all: t2.c
gcc t2.c -o mydht -Wall -lm
purge:
rm -f mydht
File deleted
...@@ -16,20 +16,15 @@ typedef struct Node { ...@@ -16,20 +16,15 @@ typedef struct Node {
struct Node* prev; struct Node* prev;
} Node; } Node;
typedef struct DHT {
Node* head;
int max_id;
} DHT;
void redistribute_keys(Node *dht, Node* new_node) { void redistribute_keys(Node *dht, Node* new_node) {
Node *start = dht; Node *start = dht;
do { do {
Node* current = new_node; Node* current = new_node;
printf("Start:%d Next:%d\n", start->id, current->id); // printf("Start:%d Next:%d\n", start->id, current->id);
printf("Start->key_count:%d\n", start->key_count); // printf("Start->key_count:%d\n", start->key_count);
//O novo nodo deve ter as chaves EXISTENTES que são maiores que o antecessor e menores ou iguais a ele //O novo nodo deve ter as chaves EXISTENTES que são maiores que o antecessor e menores ou iguais a ele
for (int i = 0; i < start->key_count; i++) { for (int i = 0; i < start->key_count; i++) {
printf("Start->keys[%d]:%d\n", i, start->keys[i]); // printf("Start->keys[%d]:%d\n", i, start->keys[i]);
if (start->keys[i] > start->id && start->keys[i] <= current->id) { if (start->keys[i] > start->id && start->keys[i] <= current->id) {
current->keys[current->key_count++] = start->keys[i]; current->keys[current->key_count++] = start->keys[i];
start->keys[i] = 0; start->keys[i] = 0;
...@@ -166,14 +161,14 @@ Node* remove_node(Node* head, int id) { ...@@ -166,14 +161,14 @@ Node* remove_node(Node* head, int id) {
// Função para calcular a tabela finger // Função para calcular a tabela finger
void calculate_finger_table(Node* head, int max_id) { void calculate_finger_table(Node* head, int max_id) {
Node* current = head; Node* current = head;
printf("Max_id:%d\n", max_id); // printf("Max_id:%d\n", max_id);
int log2_max_id = ceil(log2(max_id)); int log2_max_id = ceil(log2(max_id));
printf("Log2_max_id:%d\n", log2_max_id); // printf("Log2_max_id:%d\n", log2_max_id);
do { do {
for (int k = 1; k <= log2_max_id; k++) { for (int k = 1; k <= log2_max_id; k++) {
printf("N:%d", current->id); // printf("N:%d", current->id);
int finger_id = (current->id + (1 << k-1)) % (1 << log2_max_id); int finger_id = (current->id + (1 << (k-1))) % (1 << log2_max_id);
printf(" Finger_id:%d\n", finger_id); // printf(" Finger_id:%d\n", finger_id);
Node* temp = head; Node* temp = head;
//Inserir na tabela_finger o id do nó que é o menor id maior que o id do finger_id, ou seja, o id mais próximo do finger_id //Inserir na tabela_finger o id do nó que é o menor id maior que o id do finger_id, ou seja, o id mais próximo do finger_id
while(temp->id < finger_id && temp->next != head) { while(temp->id < finger_id && temp->next != head) {
...@@ -221,7 +216,7 @@ void print_finger_table_completa(Node* head, int timestamp) { ...@@ -221,7 +216,7 @@ void print_finger_table_completa(Node* head, int timestamp) {
// Função para realizar lookup de uma chave // Função para realizar lookup de uma chave
Node ** lookup(Node* head, int timestamp, int node_id, int key, int *num_nodes) { Node ** lookup(Node* head, int timestamp, int node_id, int key, int *num_nodes) {
printf("Lookup\n"); // printf("Lookup\n");
Node* current = head; Node* current = head;
Node** nodes_involved = (Node**)malloc(TAM_MAX_FINGER * sizeof(Node*)); Node** nodes_involved = (Node**)malloc(TAM_MAX_FINGER * sizeof(Node*));
*num_nodes = 0; *num_nodes = 0;
...@@ -295,7 +290,7 @@ void insert_key(Node* head, int node_id, int key) { ...@@ -295,7 +290,7 @@ void insert_key(Node* head, int node_id, int key) {
} }
current->keys[current->key_count++] = key; current->keys[current->key_count++] = key;
printf("Chave %d inserida no nó %d\n", key, current->id); // printf("Chave %d inserida no nó %d\n", key, current->id);
} }
void imprime_chaves(Node* head) { void imprime_chaves(Node* head) {
......
1 E 4 -
2 E 10 -
3 I 10 18
4 E 20 -
5 E 14 -
6 S 4 -
7 E 28 -
8 L 10 18
9 E 1 -
10 E 56 -
11 I 1 50
12 S 56 -
13 E 52 -
14 E 42 -
15 L 10 50
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment