Skip to content

Commit

Permalink
Merge pull request #15 from K0001rch/issue-7
Browse files Browse the repository at this point in the history
feature: #1 заменён полный обход всех элементов хеш-таблицы,
на обход двусвязного списка в направлении назад (так как при вставки очередного элемента в хеш-таблицу, ослеживается только значение укзателя на последний элемент).
  • Loading branch information
Ya-Pasha-364shy authored May 4, 2023
2 parents e22a5b2 + 2466ed1 commit f43551b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
30 changes: 13 additions & 17 deletions helpers/helpers_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,24 +310,20 @@ static void handle_event(queue_entry_t event, thread_argument_t * arg)
return;
}
DIR * thread_directory = NULL;
for (int i = 0; i < arg->hash_table->size; i++)

hash_item_t * hi = arg->hash_table->node;
while (hi != NULL)
{
if (NULL != hash_table_search_item(arg->hash_table, i, NULL))
thread_directory = opendir(hi->value);
if (thread_directory && !backup_helpers_find_file(cur_event_filename, thread_directory))
{
hi = hi->previous;
continue;
}
else if (thread_directory)
{
thread_directory = opendir(arg->hash_table->table[i]->value);
if (NULL != thread_directory)
{
if (false == backup_helpers_find_file(cur_event_filename, thread_directory))
{
continue;
}
else
{
// path to file founded successfully, refresh
arg->path_to_dir = arg->hash_table->table[i]->value;
break;
}
}
arg->path_to_dir = hi->value;
break;
}
}
cur_event_file_or_dir = HELPERS_FILE;
Expand Down Expand Up @@ -609,4 +605,4 @@ char * parser_read_conf()
while (fread(buffer, sizeof(char), HELPERS_FILE_STROKE_MAX_LEN * lines, cp));

return buffer;
}
}
23 changes: 19 additions & 4 deletions helpers/helpers_hash_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@ static hash_item_t * __hash_item_create(char * value)
memset(hash_item->value, 0, sizeof(char) * value_len);
strncpy(hash_item->value, value, value_len);

hash_t key = __hash_key_generator(value);
hash_t key = __hash_key_generator(value);
if (key >= 0 && key <= 1.1)
{
printf("Error: This path to file is too big !\n");
return NULL;
}
else
{
hash_item->key = key;
hash_item->key = key;
hash_item->previous = NULL;
hash_item->next = NULL;

return hash_item;
}
}
Expand All @@ -62,6 +65,7 @@ hash_table_t * hash_table_create(hash_table_size_t size)
{
this->table[i] = NULL;
}
this->node = NULL;

return this;
}
Expand Down Expand Up @@ -118,9 +122,20 @@ unsigned int hash_table_insert_item(hash_table_t * this, char * value)
}
hash_item_t ** head = &this->table[key];

if (!(*head))
if (head && !(*head))
{
*head = item;
if (NULL == this->node)
{
this->node = (void *)*head;
}
else
{
this->node->next = (void *)item;
item->previous = this->node;
this->node = (void *)item;
}

++this->count;
}
else if ((*head)->key == key)
Expand Down Expand Up @@ -175,4 +190,4 @@ int test1()
printf("[1] = %d:%s\n", HI->key, HI->value);
hash_table_free(HT);
}
#endif
#endif
2 changes: 1 addition & 1 deletion helpers/helpers_logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ int logger(char * message)

fclose(fptr);
return LOGGER_NORMAL_EXIT;
}
}
11 changes: 8 additions & 3 deletions include/helpers/helpers_hash_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ typedef enum

typedef struct hash_item_s
{
hash_t key;
char * value;
hash_t key;
char * value;

void * previous;
void * next;
} hash_item_t;

typedef struct hash_table_s
{
hash_item_t ** table;
hash_item_t * node;

hash_table_size_t size;
int count;
} hash_table_t;
Expand All @@ -32,4 +37,4 @@ unsigned int hash_table_insert_item(hash_table_t * this, char * value);
hash_table_t * hash_table_create(hash_table_size_t size);
void hash_table_free(hash_table_t * this);

#endif // HELPERS_HASH_TABLE_H
#endif // HELPERS_HASH_TABLE_H

0 comments on commit f43551b

Please sign in to comment.