Stergios Upload Compile Checker / Explore-Gitea-Actions (push) Successful in 7s
86 lines
1.9 KiB
C
86 lines
1.9 KiB
C
#include "node.h"
|
|
#include "hash.h"
|
|
#include "maths.h"
|
|
#include "queue.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
typedef struct VisitedEntry {
|
|
int128_t a, b, c;
|
|
struct VisitedEntry* next;
|
|
} VisitedEntry;
|
|
|
|
static Node* pool = NULL;
|
|
static VisitedEntry* visited_pool = NULL;
|
|
static VisitedEntry* hash_table[HASH_SIZE] = {0};
|
|
static int32_t pool_ptr = 0;
|
|
static int32_t pool_capacity = 0;
|
|
static int32_t visited_ptr = 0;
|
|
|
|
void node_pool_init(int32_t max_nodes) {
|
|
pool_capacity = max_nodes;
|
|
pool = malloc(pool_capacity * sizeof(Node));
|
|
visited_pool = malloc(pool_capacity * sizeof(VisitedEntry));
|
|
if (!pool || !visited_pool) {
|
|
fprintf(stderr, "Fatal: Could not allocate Node Pool for %i nodes.\n", max_nodes);
|
|
exit(1);
|
|
}
|
|
pool_ptr = 0;
|
|
visited_ptr = 0;
|
|
for (size_t i = 0; i < HASH_SIZE; ++i) hash_table[i] = NULL;
|
|
}
|
|
|
|
void node_pool_reset() {
|
|
pool_ptr = 0;
|
|
}
|
|
|
|
void node_pool_destroy() {
|
|
if (pool) {
|
|
free(pool);
|
|
pool = NULL;
|
|
}
|
|
if (visited_pool) {
|
|
free(visited_pool);
|
|
visited_pool = NULL;
|
|
}
|
|
}
|
|
|
|
int32_t node_pool_usage() {
|
|
return pool_ptr;
|
|
}
|
|
|
|
int32_t node_pool_capacity() {
|
|
return pool_capacity;
|
|
}
|
|
|
|
Node* node_pool_next() {
|
|
if (pool_ptr >= pool_capacity) return NULL; /* Pool safety */
|
|
return &pool[pool_ptr++];
|
|
}
|
|
|
|
bool visit_and_mark(int128_t a, int128_t b, int128_t c) {
|
|
sort_triplet(&a, &b, &c);
|
|
uint32_t h = hash_three128(a,b,c);
|
|
VisitedEntry* cur = hash_table[h];
|
|
while (cur) {
|
|
if (cur->a == a && cur->b == b && cur->c == c) return 0;
|
|
cur = cur->next;
|
|
}
|
|
|
|
if (visited_ptr >= pool_capacity) {
|
|
fprintf(stderr, "VisPool Overflow! Increase MAX_NODES.\n");
|
|
exit(1);
|
|
}
|
|
|
|
VisitedEntry* e = &visited_pool[visited_ptr++];
|
|
e->a = a; e->b = b; e->c = c;
|
|
e->next = hash_table[h];
|
|
hash_table[h] = e;
|
|
return 1;
|
|
}
|
|
|
|
void clear_hash_table() {
|
|
visited_ptr = 0;
|
|
for (size_t i = 0; i < HASH_SIZE; ++i) hash_table[i] = NULL;
|
|
} |