support for trie_traverse/2 and itrie_save_as_trie/2

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@2110 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
ricroc 2008-02-22 18:57:27 +00:00
parent 596ecc5126
commit bd957c06b6
9 changed files with 2181 additions and 1674 deletions

View File

@ -33,7 +33,7 @@ SHLIB_SUFFIX=@SHLIB_SUFFIX@
CWD=$(PWD)
#
OBJS=base_tries.o tries.o base_itries.o itries.o
OBJS=core_tries.o base_tries.o tries.o base_itries.o itries.o
SOBJS=tries@SHLIB_SUFFIX@ itries@SHLIB_SUFFIX@
#in some systems we just create a single object, in others we need to
@ -41,26 +41,29 @@ SOBJS=tries@SHLIB_SUFFIX@ itries@SHLIB_SUFFIX@
all: $(SOBJS)
base_tries.o: $(srcdir)/base_tries.h $(srcdir)/base_tries.c
core_tries.o: $(srcdir)/core_tries.h $(srcdir)/core_tries.c
$(CC) -c $(CFLAGS) $(SHLIB_CFLAGS) $(srcdir)/core_tries.c -o core_tries.o
base_tries.o: $(srcdir)/core_tries.h $(srcdir)/base_tries.h $(srcdir)/base_tries.c
$(CC) -c $(CFLAGS) $(SHLIB_CFLAGS) $(srcdir)/base_tries.c -o base_tries.o
tries.o: $(srcdir)/base_tries.h $(srcdir)/tries.c
tries.o: $(srcdir)/core_tries.h $(srcdir)/base_tries.h $(srcdir)/tries.c
$(CC) -c $(CFLAGS) $(SHLIB_CFLAGS) $(srcdir)/tries.c -o tries.o
base_itries.o: $(srcdir)/base_tries.h $(srcdir)/base_itries.h $(srcdir)/base_itries.c
base_itries.o: $(srcdir)/core_tries.h $(srcdir)/base_itries.h $(srcdir)/base_itries.c
$(CC) -c $(CFLAGS) $(SHLIB_CFLAGS) $(srcdir)/base_itries.c -o base_itries.o
itries.o: $(srcdir)/base_tries.h $(srcdir)/base_itries.h $(srcdir)/itries.c
itries.o: $(srcdir)/core_tries.h $(srcdir)/base_itries.h $(srcdir)/itries.c
$(CC) -c $(CFLAGS) $(SHLIB_CFLAGS) $(srcdir)/itries.c -o itries.o
@DO_SECOND_LD@%@SHLIB_SUFFIX@: %.o
@DO_SECOND_LD@ @SHLIB_LD@ -o $@ $<
@DO_SECOND_LD@tries@SHLIB_SUFFIX@: base_tries.o tries.o
@DO_SECOND_LD@ @SHLIB_LD@ -o tries@SHLIB_SUFFIX@ base_tries.o tries.o
@DO_SECOND_LD@tries@SHLIB_SUFFIX@: core_tries.o base_tries.o tries.o
@DO_SECOND_LD@ @SHLIB_LD@ -o tries@SHLIB_SUFFIX@ core_tries.o base_tries.o tries.o
@DO_SECOND_LD@itries@SHLIB_SUFFIX@: base_tries.o base_itries.o itries.o
@DO_SECOND_LD@ @SHLIB_LD@ -o itries@SHLIB_SUFFIX@ base_tries.o base_itries.o itries.o
@DO_SECOND_LD@itries@SHLIB_SUFFIX@: core_tries.o base_itries.o itries.o
@DO_SECOND_LD@ @SHLIB_LD@ -o itries@SHLIB_SUFFIX@ core_tries.o base_itries.o itries.o
install: all
$(INSTALL_PROGRAM) $(SOBJS) $(DESTDIR)$(LIBDIR)

View File

@ -14,7 +14,7 @@
#include <YapInterface.h>
#include <stdio.h>
#include <string.h>
#include "base_tries.h"
#include "core_tries.h"
#include "base_itries.h"
@ -34,7 +34,7 @@ static TrEntry FIRST_ITRIE, CURRENT_ITRIE;
inline
void itrie_init_module(void) {
ITRIE_ENGINE = trie_init_module();
ITRIE_ENGINE = core_trie_init_module();
FIRST_ITRIE = NULL;
return;
}
@ -73,7 +73,7 @@ void itrie_data_print(TrNode node) {
inline
void itrie_data_construct(TrNode node_dest, TrNode node_source) {
void itrie_data_copy(TrNode node_dest, TrNode node_source) {
TrData data_dest, data_source;
data_source = (TrData) GET_DATA_FROM_LEAF_TRIE_NODE(node_source);
@ -135,7 +135,7 @@ TrEntry itrie_open(void) {
TrEntry itrie;
TrNode node;
node = trie_open(ITRIE_ENGINE);
node = core_trie_open(ITRIE_ENGINE);
new_itrie_entry(itrie, node);
if (FIRST_ITRIE)
TrEntry_previous(FIRST_ITRIE) = itrie;
@ -146,7 +146,7 @@ TrEntry itrie_open(void) {
inline
void itrie_close(TrEntry itrie) {
trie_close(ITRIE_ENGINE, TrEntry_trie(itrie), &itrie_data_destruct);
core_trie_close(ITRIE_ENGINE, TrEntry_trie(itrie), &itrie_data_destruct);
if (TrEntry_next(itrie)) {
TrEntry_previous(TrEntry_next(itrie)) = TrEntry_previous(itrie);
TrEntry_next(TrEntry_previous(itrie)) = TrEntry_next(itrie);
@ -161,7 +161,7 @@ inline
void itrie_close_all(void) {
TrEntry itrie;
trie_close_all(ITRIE_ENGINE, &itrie_data_destruct);
core_trie_close_all(ITRIE_ENGINE, &itrie_data_destruct);
while (FIRST_ITRIE) {
itrie = TrEntry_next(FIRST_ITRIE);
free_itrie_entry(FIRST_ITRIE);
@ -203,7 +203,7 @@ void itrie_put_entry(TrEntry itrie, YAP_Term entry) {
TrNode node;
YAP_Int depth;
node = trie_put_entry(ITRIE_ENGINE, TrEntry_trie(itrie), entry, &depth);
node = core_trie_put_entry(ITRIE_ENGINE, TrEntry_trie(itrie), entry, &depth);
if (!(data = (TrData) GET_DATA_FROM_LEAF_TRIE_NODE(node))) {
new_itrie_data(data, itrie, node, 0, 0, -1, depth);
PUT_DATA_IN_LEAF_TRIE_NODE(node, data);
@ -218,7 +218,7 @@ void itrie_update_entry(TrEntry itrie, YAP_Term entry) {
TrData data;
TrNode node;
if ((node = trie_check_entry(TrEntry_trie(itrie), entry)) != NULL) {
if ((node = core_trie_check_entry(TrEntry_trie(itrie), entry)) != NULL) {
data = (TrData) GET_DATA_FROM_LEAF_TRIE_NODE(node);
update_itrie_data(data, TrEntry_timestamp(itrie), TrEntry_mode(itrie));
}
@ -230,7 +230,7 @@ inline
TrData itrie_check_entry(TrEntry itrie, YAP_Term entry) {
TrNode node;
if (!(node = trie_check_entry(TrEntry_trie(itrie), entry)))
if (!(node = core_trie_check_entry(TrEntry_trie(itrie), entry)))
return NULL;
return (TrData) GET_DATA_FROM_LEAF_TRIE_NODE(node);
}
@ -238,7 +238,7 @@ TrData itrie_check_entry(TrEntry itrie, YAP_Term entry) {
inline
YAP_Term itrie_get_entry(TrData data) {
return trie_get_entry(TrData_leaf(data));
return core_trie_get_entry(TrData_leaf(data));
}
@ -296,28 +296,28 @@ TrData itrie_traverse_cont(TrEntry itrie) {
inline
void itrie_remove_entry(TrData data) {
trie_remove_entry(ITRIE_ENGINE, TrData_leaf(data), &itrie_data_destruct);
core_trie_remove_entry(ITRIE_ENGINE, TrData_leaf(data), &itrie_data_destruct);
return;
}
inline
void itrie_remove_subtree(TrData data) {
trie_remove_subtree(ITRIE_ENGINE, TrData_leaf(data), &itrie_data_destruct);
core_trie_remove_subtree(ITRIE_ENGINE, TrData_leaf(data), &itrie_data_destruct);
return;
}
inline
void itrie_add(TrEntry itrie_dest, TrEntry itrie_source) {
trie_add(TrEntry_trie(itrie_dest), TrEntry_trie(itrie_source), &itrie_data_add);
core_trie_add(TrEntry_trie(itrie_dest), TrEntry_trie(itrie_source), &itrie_data_add);
return;
}
inline
void itrie_subtract(TrEntry itrie_dest, TrEntry itrie_source) {
trie_add(TrEntry_trie(itrie_dest), TrEntry_trie(itrie_source), &itrie_data_subtract);
core_trie_add(TrEntry_trie(itrie_dest), TrEntry_trie(itrie_source), &itrie_data_subtract);
return;
}
@ -325,33 +325,40 @@ void itrie_subtract(TrEntry itrie_dest, TrEntry itrie_source) {
inline
void itrie_join(TrEntry itrie_dest, TrEntry itrie_source) {
CURRENT_ITRIE = itrie_dest;
trie_join(ITRIE_ENGINE, TrEntry_trie(itrie_dest), TrEntry_trie(itrie_source), &itrie_data_add, &itrie_data_construct);
core_trie_join(ITRIE_ENGINE, TrEntry_trie(itrie_dest), TrEntry_trie(itrie_source), &itrie_data_add, &itrie_data_copy);
return;
}
inline
void itrie_intersect(TrEntry itrie_dest, TrEntry itrie_source) {
trie_intersect(ITRIE_ENGINE, TrEntry_trie(itrie_dest), TrEntry_trie(itrie_source), &itrie_data_add, &itrie_data_destruct);
core_trie_intersect(ITRIE_ENGINE, TrEntry_trie(itrie_dest), TrEntry_trie(itrie_source), &itrie_data_add, &itrie_data_destruct);
return;
}
inline
YAP_Int itrie_count_join(TrEntry itrie1, TrEntry itrie2) {
return trie_count_join(TrEntry_trie(itrie1), TrEntry_trie(itrie2));
return core_trie_count_join(TrEntry_trie(itrie1), TrEntry_trie(itrie2));
}
inline
YAP_Int itrie_count_intersect(TrEntry itrie1, TrEntry itrie2) {
return trie_count_intersect(TrEntry_trie(itrie1), TrEntry_trie(itrie2));
return core_trie_count_intersect(TrEntry_trie(itrie1), TrEntry_trie(itrie2));
}
inline
void itrie_save(TrEntry itrie, FILE *file) {
trie_save(TrEntry_trie(itrie), file, &itrie_data_save);
core_trie_save(TrEntry_trie(itrie), file, &itrie_data_save);
return;
}
inline
void itrie_save_as_trie(TrEntry itrie, FILE *file) {
core_trie_save(TrEntry_trie(itrie), file, NULL);
return;
}
@ -366,7 +373,7 @@ TrEntry itrie_load(FILE *file) {
TrEntry_previous(FIRST_ITRIE) = itrie;
FIRST_ITRIE = itrie;
CURRENT_ITRIE = itrie;
node = trie_load(ITRIE_ENGINE, file, &itrie_data_load);
node = core_trie_load(ITRIE_ENGINE, file, &itrie_data_load);
TrEntry_trie(itrie) = node;
return itrie;
}
@ -374,27 +381,27 @@ TrEntry itrie_load(FILE *file) {
inline
void itrie_stats(YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes) {
trie_stats(ITRIE_ENGINE, memory, tries, entries, nodes);
core_trie_stats(ITRIE_ENGINE, memory, tries, entries, nodes);
return;
}
inline
void itrie_max_stats(YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes) {
trie_max_stats(ITRIE_ENGINE, memory, tries, entries, nodes);
core_trie_max_stats(ITRIE_ENGINE, memory, tries, entries, nodes);
return;
}
inline
void itrie_usage(TrEntry itrie, YAP_Int *entries, YAP_Int *nodes, YAP_Int *virtual_nodes) {
trie_usage(TrEntry_trie(itrie), entries, nodes, virtual_nodes);
core_trie_usage(TrEntry_trie(itrie), entries, nodes, virtual_nodes);
return;
}
inline
void itrie_print(TrEntry itrie) {
trie_print(TrEntry_trie(itrie), &itrie_data_print);
core_trie_print(TrEntry_trie(itrie), &itrie_data_print);
return;
}

View File

@ -147,6 +147,7 @@ typedef struct itrie_data {
}
#define free_itrie_entry(STR) \
{ free_itrie_buckets(TrEntry_buckets(STR), TrEntry_num_buckets(STR)); \
free_struct(STR); \
@ -171,7 +172,7 @@ inline void itrie_init_module(void);
inline void itrie_data_save(TrNode node, FILE *file);
inline void itrie_data_load(TrNode node, YAP_Int depth, FILE *file);
inline void itrie_data_print(TrNode node);
inline void itrie_data_construct(TrNode node_dest, TrNode node_source);
inline void itrie_data_copy(TrNode node_dest, TrNode node_source);
inline void itrie_data_destruct(TrNode node);
inline void itrie_data_add(TrNode node_dest, TrNode node_source);
inline void itrie_data_subtract(TrNode node_dest, TrNode node_source);
@ -198,6 +199,7 @@ inline void itrie_intersect(TrEntry itrie_dest, TrEntry itrie_source);
inline YAP_Int itrie_count_join(TrEntry itrie1, TrEntry itrie2);
inline YAP_Int itrie_count_intersect(TrEntry itrie1, TrEntry itrie2);
inline void itrie_save(TrEntry itrie, FILE *file);
inline void itrie_save_as_trie(TrEntry itrie, FILE *file);
inline TrEntry itrie_load(FILE *file);
inline void itrie_stats(YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes);
inline void itrie_max_stats(YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes);

File diff suppressed because it is too large Load Diff

View File

@ -7,112 +7,45 @@
/* -------------------------------------- */
/* Yap Tagging Scheme */
/* -------------------------------------- */
#include "config.h"
#if SIZEOF_INT_P==4
#define TAG_LOW_BITS_32 /* 'Tags_32LowTag.h' tagging scheme */
#elif SIZEOF_INT_P==8
#define TAG_64BITS /* 'Tags_64bits.h' tagging scheme */
#else
#error Unknown tagging scheme
#endif /* YAP_SCHEME */
/* --------------------------- */
/* Defines */
/* --------------------------- */
#ifdef TAG_LOW_BITS_32
#define ApplTag 1 /* 0x01 */
#else /* TAG_64BITS */
#define ApplTag 5 /* 0x05 */
#endif /* TAG_SCHEME */
#define PairInitTag 3 /* 0x03 */
#define PairEndTag 19 /* 0x13 */
#define CommaInitTag 35 /* 0x23 */
#define CommaEndTag 51 /* 0x33 */
#define FloatInitTag 67 /* 0x43 */
#define FloatEndTag 83 /* 0x53 */
#define TRIE_MODE_STANDARD 0
#define TRIE_MODE_REVERSE 1
#define TRIE_PRINT_NORMAL 0
#define TRIE_PRINT_FLOAT 1
#define TRIE_PRINT_FLOAT2 2
#define TRIE_PRINT_FLOAT_END 3
#define BASE_AUXILIARY_TERM_STACK_SIZE 1000
/* --------------------------- */
/* Structs */
/* --------------------------- */
typedef struct trie_engine {
struct trie_node *first_trie;
YAP_Int memory_in_use;
YAP_Int tries_in_use;
YAP_Int entries_in_use;
YAP_Int nodes_in_use;
YAP_Int memory_max_used;
YAP_Int tries_max_used;
YAP_Int entries_max_used;
YAP_Int nodes_max_used;
} *TrEngine;
typedef struct trie_entry {
struct trie_node *top_trie_node;
struct trie_data *first_trie_data;
struct trie_data *last_trie_data;
struct trie_data *traverse_trie_data;
struct trie_entry *next;
struct trie_entry *previous;
} *TrEntry;
#define TrEngine_trie(X) ((X)->first_trie)
#define TrEngine_memory(X) ((X)->memory_in_use)
#define TrEngine_tries(X) ((X)->tries_in_use)
#define TrEngine_entries(X) ((X)->entries_in_use)
#define TrEngine_nodes(X) ((X)->nodes_in_use)
#define TrEngine_memory_max(X) ((X)->memory_max_used)
#define TrEngine_tries_max(X) ((X)->tries_max_used)
#define TrEngine_entries_max(X) ((X)->entries_max_used)
#define TrEngine_nodes_max(X) ((X)->nodes_max_used)
#define TrEntry_trie(X) ((X)->top_trie_node)
#define TrEntry_first_data(X) ((X)->first_trie_data)
#define TrEntry_last_data(X) ((X)->last_trie_data)
#define TrEntry_traverse_data(X) ((X)->traverse_trie_data)
#define TrEntry_next(X) ((X)->next)
#define TrEntry_previous(X) ((X)->previous)
typedef struct trie_node {
struct trie_node *parent;
struct trie_node *child;
struct trie_node *next;
struct trie_node *previous;
YAP_Term entry;
} *TrNode;
typedef struct trie_data {
struct trie_entry *trie;
struct trie_node *leaf_trie_node;
struct trie_data *next;
struct trie_data *previous;
} *TrData;
#define TrNode_parent(X) ((X)->parent)
#define TrNode_child(X) ((X)->child)
#define TrNode_next(X) ((X)->next)
#define TrNode_previous(X) ((X)->previous)
#define TrNode_entry(X) ((X)->entry)
#define TrData_trie(X) ((X)->trie)
#define TrData_leaf(X) ((X)->leaf_trie_node)
#define TrData_next(X) ((X)->next)
#define TrData_previous(X) ((X)->previous)
typedef struct trie_hash {
struct trie_node *parent; /* for compatibility with the trie_node data structure */
struct trie_node **buckets;
int number_of_buckets;
int number_of_nodes;
} *TrHash;
#define TYPE_TR_ENTRY struct trie_entry
#define TYPE_TR_DATA struct trie_data
#define SIZEOF_TR_ENTRY sizeof(TYPE_TR_ENTRY)
#define SIZEOF_TR_DATA sizeof(TYPE_TR_DATA)
#define TrHash_mark(X) ((X)->parent)
#define TrHash_buckets(X) ((X)->buckets)
#define TrHash_bucket(X,N) ((X)->buckets + N)
#define TrHash_num_buckets(X) ((X)->number_of_buckets)
#define TrHash_seed(X) ((X)->number_of_buckets - 1)
#define TrHash_num_nodes(X) ((X)->number_of_nodes)
#define TYPE_TR_ENGINE struct trie_engine
#define TYPE_TR_NODE struct trie_node
#define TYPE_TR_HASH struct trie_hash
#define SIZEOF_TR_ENGINE sizeof(TYPE_TR_ENGINE)
#define SIZEOF_TR_NODE sizeof(TYPE_TR_NODE)
#define SIZEOF_TR_HASH sizeof(TYPE_TR_HASH)
#define SIZEOF_TR_BUCKET sizeof(TYPE_TR_NODE *)
#define AS_TR_NODE_NEXT(ADDR) (TrNode)((unsigned long int)(ADDR) - 2 * sizeof(struct trie_node *))
#define AS_TR_ENTRY_NEXT(ADDR) (TrEntry)((unsigned long int)(ADDR) - sizeof(struct trie_node *) - 3 * sizeof(struct trie_data *))
#define AS_TR_DATA_NEXT(ADDR) (TrData)((unsigned long int)(ADDR) - sizeof(struct trie_entry *) - sizeof(struct trie_node *))
@ -120,144 +53,44 @@ typedef struct trie_hash {
/* Macros */
/* --------------------------- */
#define TAG_ADDR(ADDR) ((unsigned long int)(ADDR) | 0x1)
#define UNTAG_ADDR(ADDR) ((unsigned long int)(ADDR) & ~(0x1))
#define PUT_DATA_IN_LEAF_TRIE_NODE(TR_NODE, DATA) TrNode_child(TR_NODE) = (TrNode)TAG_ADDR(DATA)
#define GET_DATA_FROM_LEAF_TRIE_NODE(TR_NODE) UNTAG_ADDR(TrNode_child(TR_NODE))
#define MARK_AS_LEAF_TRIE_NODE(TR_NODE) PUT_DATA_IN_LEAF_TRIE_NODE(TR_NODE, TrNode_child(TR_NODE))
#define IS_LEAF_TRIE_NODE(TR_NODE) ((unsigned long int)(TrNode_child(TR_NODE)) & 0x1)
#define IsTrieVar(TERM, STACK, STACK_BASE) ((YAP_Term *)(TERM) > STACK && (YAP_Term *)(TERM) <= STACK_BASE)
#define MkTrieVar(INDEX) ((INDEX) << 4)
#define TrieVarIndex(TERM) ((TERM) >> 4)
#define BASE_HASH_BUCKETS 64
#define MAX_NODES_PER_TRIE_LEVEL 8
#define MAX_NODES_PER_BUCKET (MAX_NODES_PER_TRIE_LEVEL / 2)
#define HASH_TERM(TERM, SEED) (((TERM) >> 4) & (SEED))
#define IS_HASH_NODE(NODE) (TrHash_mark(NODE) == NULL)
#define BASE_SAVE_MARK 1000 /* could lead to errors if the number of different variables in a term is greater than it */
#define HASH_SAVE_MARK ((YAP_Term) MkTrieVar(BASE_SAVE_MARK))
#define ATOM_SAVE_MARK ((YAP_Term) MkTrieVar(BASE_SAVE_MARK + 1))
#define FUNCTOR_SAVE_MARK ((YAP_Term) MkTrieVar(BASE_SAVE_MARK + 2))
#define FLOAT_SAVE_MARK ((YAP_Term) MkTrieVar(BASE_SAVE_MARK + 3))
#define STACK_NOT_EMPTY(STACK, STACK_BASE) STACK != STACK_BASE
#define POP_UP(STACK) *--STACK
#define POP_DOWN(STACK) *++STACK
#define PUSH_UP(STACK, ITEM, STACK_TOP) \
{ if (STACK < STACK_TOP) \
fprintf(stderr, "\nTries base module: term stack full"); \
*STACK = (YAP_Term)(ITEM); \
STACK--; \
}
#define PUSH_DOWN(STACK, ITEM, STACK_TOP) \
{ if (STACK > STACK_TOP) \
fprintf(stderr, "\nTries base module: term stack full"); \
*STACK = (YAP_Term)(ITEM); \
STACK++; \
#define new_trie_entry(TR_ENTRY, TR_NODE) \
{ new_struct(TR_ENTRY, TYPE_TR_ENTRY, SIZEOF_TR_ENTRY); \
TrEntry_trie(TR_ENTRY) = TR_NODE; \
TrEntry_first_data(TR_ENTRY) = NULL; \
TrEntry_last_data(TR_ENTRY) = NULL; \
TrEntry_traverse_data(TR_ENTRY) = NULL; \
TrEntry_next(TR_ENTRY) = FIRST_TRIE; \
TrEntry_previous(TR_ENTRY) = AS_TR_ENTRY_NEXT(&FIRST_TRIE); \
INCREMENT_MEMORY(TRIE_ENGINE, SIZEOF_TR_ENTRY); \
}
#define new_trie_data(TR_DATA, TR_ENTRY, TR_NODE) \
{ TrData last_data = TrEntry_last_data(TR_ENTRY); \
new_struct(TR_DATA, TYPE_TR_DATA, SIZEOF_TR_DATA); \
TrData_trie(TR_DATA) = TR_ENTRY; \
TrData_leaf(TR_DATA) = TR_NODE; \
TrData_next(TR_DATA) = NULL; \
if (last_data) { \
TrData_next(last_data) = TR_DATA; \
TrData_previous(TR_DATA) = last_data; \
TrEntry_last_data(TR_ENTRY) = TR_DATA; \
} else { \
TrData_previous(TR_DATA) = AS_TR_DATA_NEXT(&TrEntry_first_data(TR_ENTRY)); \
TrEntry_first_data(TR_ENTRY) = TR_DATA; \
TrEntry_last_data(TR_ENTRY) = TR_DATA; \
} \
INCREMENT_MEMORY(TRIE_ENGINE, SIZEOF_TR_DATA); \
}
#define new_struct(STR, STR_TYPE, STR_SIZE) \
STR = (STR_TYPE *) YAP_AllocSpaceFromYap(STR_SIZE)
#define new_trie_engine(TR_ENGINE) \
{ new_struct(TR_ENGINE, TYPE_TR_ENGINE, SIZEOF_TR_ENGINE); \
TrEngine_trie(TR_ENGINE) = NULL; \
TrEngine_memory(TR_ENGINE) = 0; \
TrEngine_tries(TR_ENGINE) = 0; \
TrEngine_entries(TR_ENGINE) = 0; \
TrEngine_nodes(TR_ENGINE) = 0; \
TrEngine_memory_max(TR_ENGINE) = 0; \
TrEngine_tries_max(TR_ENGINE) = 0; \
TrEngine_entries_max(TR_ENGINE) = 0; \
TrEngine_nodes_max(TR_ENGINE) = 0; \
}
#define new_trie_node(TR_NODE, ENTRY, PARENT, CHILD, NEXT, PREVIOUS) \
{ new_struct(TR_NODE, TYPE_TR_NODE, SIZEOF_TR_NODE); \
TrNode_entry(TR_NODE) = ENTRY; \
TrNode_parent(TR_NODE) = PARENT; \
TrNode_child(TR_NODE) = CHILD; \
TrNode_next(TR_NODE) = NEXT; \
TrNode_previous(TR_NODE) = PREVIOUS; \
INCREMENT_NODES(CURRENT_TRIE_ENGINE); \
INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_NODE); \
}
#define new_trie_hash(TR_HASH, NUM_NODES, NUM_BUCKETS) \
{ new_struct(TR_HASH, TYPE_TR_HASH, SIZEOF_TR_HASH); \
TrHash_mark(TR_HASH) = NULL; \
TrHash_num_buckets(TR_HASH) = NUM_BUCKETS; \
new_hash_buckets(TR_HASH, NUM_BUCKETS); \
TrHash_num_nodes(TR_HASH) = NUM_NODES; \
INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_HASH); \
}
#define new_hash_buckets(TR_HASH, NUM_BUCKETS) \
{ int i; void **ptr; \
new_struct(ptr, void *, NUM_BUCKETS * sizeof(void *)); \
TrHash_buckets(TR_HASH) = (TYPE_TR_NODE **) ptr; \
for (i = NUM_BUCKETS; i != 0; i--) \
*ptr++ = NULL; \
INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, (NUM_BUCKETS) * SIZEOF_TR_BUCKET); \
}
#define expand_auxiliary_term_stack() \
{ YAP_Term *aux_stack; \
YAP_Int aux_size = CURRENT_AUXILIARY_TERM_STACK_SIZE * sizeof(YAP_Term); \
new_struct(aux_stack, YAP_Term, aux_size * 2); \
memcpy(aux_stack, AUXILIARY_TERM_STACK, aux_size); \
free_struct(AUXILIARY_TERM_STACK); \
AUXILIARY_TERM_STACK = aux_stack; \
CURRENT_AUXILIARY_TERM_STACK_SIZE *= 2; \
#define free_trie_entry(STR) \
{ free_struct(STR); \
DECREMENT_MEMORY(TRIE_ENGINE, SIZEOF_TR_ENTRY); \
}
#define free_struct(STR) \
YAP_FreeSpaceFromYap((char *) (STR))
#define free_trie_node(STR) \
{ free_struct(STR); \
DECREMENT_NODES(CURRENT_TRIE_ENGINE); \
DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_NODE); \
}
#define free_trie_hash(STR) \
{ free_struct(STR); \
DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_HASH); \
}
#define free_hash_buckets(STR, NUM_BUCKETS) \
{ free_struct(STR); \
DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, (NUM_BUCKETS) * SIZEOF_TR_BUCKET); \
}
#define INCREMENT_MEMORY(TR_ENGINE, SIZE) \
{ TrEngine_memory(TR_ENGINE) += SIZE; \
if (TrEngine_memory(TR_ENGINE) > TrEngine_memory_max(TR_ENGINE)) \
TrEngine_memory_max(TR_ENGINE) = TrEngine_memory(TR_ENGINE); \
}
#define INCREMENT_TRIES(TR_ENGINE) \
{ TrEngine_tries(TR_ENGINE)++; \
if (TrEngine_tries(TR_ENGINE) > TrEngine_tries_max(TR_ENGINE)) \
TrEngine_tries_max(TR_ENGINE) = TrEngine_tries(TR_ENGINE); \
}
#define INCREMENT_ENTRIES(TR_ENGINE) \
{ TrEngine_entries(TR_ENGINE)++; \
if (TrEngine_entries(TR_ENGINE) > TrEngine_entries_max(TR_ENGINE)) \
TrEngine_entries_max(TR_ENGINE) = TrEngine_entries(TR_ENGINE); \
}
#define INCREMENT_NODES(TR_ENGINE) \
{ TrEngine_nodes(TR_ENGINE)++; \
if (TrEngine_nodes(TR_ENGINE) > TrEngine_nodes_max(TR_ENGINE)) \
TrEngine_nodes_max(TR_ENGINE) = TrEngine_nodes(TR_ENGINE); \
}
#define DECREMENT_MEMORY(TR_ENGINE, SIZE) \
TrEngine_memory(TR_ENGINE) -= SIZE
#define DECREMENT_TRIES(TR_ENGINE) \
TrEngine_tries(TR_ENGINE)--
#define DECREMENT_ENTRIES(TR_ENGINE) \
TrEngine_entries(TR_ENGINE)--
#define DECREMENT_NODES(TR_ENGINE) \
TrEngine_nodes(TR_ENGINE)--
#define free_trie_data(STR) \
{ free_struct(STR); \
DECREMENT_MEMORY(TRIE_ENGINE, SIZEOF_TR_DATA); \
}
@ -265,25 +98,29 @@ typedef struct trie_hash {
/* API */
/* --------------------------- */
inline TrEngine trie_init_module(void);
inline TrNode trie_open(TrEngine engine);
inline void trie_close(TrEngine engine, TrNode node, void (*destruct_function)(TrNode));
inline void trie_close_all(TrEngine engine, void (*destruct_function)(TrNode));
inline void trie_init_module(void);
inline void trie_data_load(TrNode node, YAP_Int depth, FILE *file);
inline void trie_data_copy(TrNode node_dest, TrNode node_source);
inline void trie_data_destruct(TrNode node);
inline TrEntry trie_open(void);
inline void trie_close(TrEntry trie);
inline void trie_close_all(void);
inline void trie_set_mode(YAP_Int mode);
inline YAP_Int trie_get_mode(void);
inline TrNode trie_put_entry(TrEngine engine, TrNode node, YAP_Term entry, YAP_Int *depth);
inline TrNode trie_check_entry(TrNode node, YAP_Term entry);
inline YAP_Term trie_get_entry(TrNode node);
inline void trie_remove_entry(TrEngine engine, TrNode node, void (*destruct_function)(TrNode));
inline void trie_remove_subtree(TrEngine engine, TrNode node, void (*destruct_function)(TrNode));
inline void trie_add(TrNode node_dest, TrNode node_source, void (*add_function)(TrNode, TrNode));
inline void trie_join(TrEngine engine, TrNode node_dest, TrNode node_source, void (*add_function)(TrNode, TrNode), void (*construct_function)(TrNode, TrNode));
inline void trie_intersect(TrEngine engine, TrNode node_dest, TrNode node_source, void (*add_function)(TrNode, TrNode), void (*destruct_function)(TrNode));
inline YAP_Int trie_count_join(TrNode node1, TrNode node2);
inline YAP_Int trie_count_intersect(TrNode node1, TrNode node2);
inline void trie_save(TrNode node, FILE *file, void (*save_function)(TrNode, FILE *));
inline TrNode trie_load(TrEngine engine, FILE *file, void (*load_function)(TrNode, YAP_Int, FILE *));
inline void trie_stats(TrEngine engine, YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes);
inline void trie_max_stats(TrEngine engine, YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes);
inline void trie_usage(TrNode node, YAP_Int *entries, YAP_Int *nodes, YAP_Int *virtual_nodes);
inline void trie_print(TrNode node, void (*print_function)(TrNode));
inline TrData trie_put_entry(TrEntry trie, YAP_Term entry);
inline TrData trie_check_entry(TrEntry trie, YAP_Term entry);
inline YAP_Term trie_get_entry(TrData data);
inline TrData trie_traverse_init(TrEntry trie);
inline TrData trie_traverse_cont(TrEntry trie);
inline void trie_remove_entry(TrData data);
inline void trie_remove_subtree(TrData data);
inline void trie_join(TrEntry trie_dest, TrEntry trie_source);
inline void trie_intersect(TrEntry trie_dest, TrEntry trie_source);
inline YAP_Int trie_count_join(TrEntry trie1, TrEntry trie2);
inline YAP_Int trie_count_intersect(TrEntry trie1, TrEntry trie2);
inline void trie_save(TrEntry trie, FILE *file);
inline TrEntry trie_load(FILE *file);
inline void trie_stats(YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes);
inline void trie_max_stats(YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes);
inline void trie_usage(TrEntry trie, YAP_Int *entries, YAP_Int *nodes, YAP_Int *virtual_nodes);
inline void trie_print(TrEntry trie);

1468
library/Tries/core_tries.c Normal file

File diff suppressed because it is too large Load Diff

301
library/Tries/core_tries.h Normal file
View File

@ -0,0 +1,301 @@
/*********************************************
File: core_tries.h
Author: Ricardo Rocha
Comments: Tries core module for Yap Prolog
version: $ID$
*********************************************/
/* -------------------------------------- */
/* Yap Tagging Scheme */
/* -------------------------------------- */
#include "config.h"
#if SIZEOF_INT_P==4
#define TAG_LOW_BITS_32 /* 'Tags_32LowTag.h' tagging scheme */
#elif SIZEOF_INT_P==8
#define TAG_64BITS /* 'Tags_64bits.h' tagging scheme */
#else
#error Unknown tagging scheme
#endif /* YAP_SCHEME */
/* --------------------------- */
/* Defines */
/* --------------------------- */
#ifdef TAG_LOW_BITS_32
#define ApplTag 1 /* 0x01 */
#else /* TAG_64BITS */
#define ApplTag 5 /* 0x05 */
#endif /* TAG_SCHEME */
#define PairInitTag 3 /* 0x03 */
#define PairEndTag 19 /* 0x13 */
#define CommaInitTag 35 /* 0x23 */
#define CommaEndTag 51 /* 0x33 */
#define FloatInitTag 67 /* 0x43 */
#define FloatEndTag 83 /* 0x53 */
#define TRIE_MODE_STANDARD 0
#define TRIE_MODE_REVERSE 1
#define TRIE_PRINT_NORMAL 0
#define TRIE_PRINT_FLOAT 1
#define TRIE_PRINT_FLOAT2 2
#define TRIE_PRINT_FLOAT_END 3
#define BASE_AUXILIARY_TERM_STACK_SIZE 1000
/* --------------------------- */
/* Structs */
/* --------------------------- */
typedef struct trie_engine {
struct trie_node *first_trie;
/* in use */
YAP_Int memory_in_use;
YAP_Int tries_in_use;
YAP_Int entries_in_use;
YAP_Int nodes_in_use;
/* max used */
YAP_Int memory_max_used;
YAP_Int tries_max_used;
YAP_Int entries_max_used;
YAP_Int nodes_max_used;
} *TrEngine;
#define TrEngine_trie(X) ((X)->first_trie)
#define TrEngine_memory(X) ((X)->memory_in_use)
#define TrEngine_tries(X) ((X)->tries_in_use)
#define TrEngine_entries(X) ((X)->entries_in_use)
#define TrEngine_nodes(X) ((X)->nodes_in_use)
#define TrEngine_memory_max(X) ((X)->memory_max_used)
#define TrEngine_tries_max(X) ((X)->tries_max_used)
#define TrEngine_entries_max(X) ((X)->entries_max_used)
#define TrEngine_nodes_max(X) ((X)->nodes_max_used)
typedef struct trie_node {
struct trie_node *parent;
struct trie_node *child;
struct trie_node *next;
struct trie_node *previous;
YAP_Term entry;
} *TrNode;
#define TrNode_parent(X) ((X)->parent)
#define TrNode_child(X) ((X)->child)
#define TrNode_next(X) ((X)->next)
#define TrNode_previous(X) ((X)->previous)
#define TrNode_entry(X) ((X)->entry)
typedef struct trie_hash {
struct trie_node *parent; /* for compatibility with the trie_node data structure */
struct trie_node **buckets;
int number_of_buckets;
int number_of_nodes;
} *TrHash;
#define TrHash_mark(X) ((X)->parent)
#define TrHash_buckets(X) ((X)->buckets)
#define TrHash_bucket(X,N) ((X)->buckets + N)
#define TrHash_num_buckets(X) ((X)->number_of_buckets)
#define TrHash_seed(X) ((X)->number_of_buckets - 1)
#define TrHash_num_nodes(X) ((X)->number_of_nodes)
#define TYPE_TR_ENGINE struct trie_engine
#define TYPE_TR_NODE struct trie_node
#define TYPE_TR_HASH struct trie_hash
#define SIZEOF_TR_ENGINE sizeof(TYPE_TR_ENGINE)
#define SIZEOF_TR_NODE sizeof(TYPE_TR_NODE)
#define SIZEOF_TR_HASH sizeof(TYPE_TR_HASH)
#define SIZEOF_TR_BUCKET sizeof(TYPE_TR_NODE *)
#define AS_TR_NODE_NEXT(ADDR) (TrNode)((unsigned long int)(ADDR) - 2 * sizeof(struct trie_node *))
/* --------------------------- */
/* Macros */
/* --------------------------- */
#define TAG_ADDR(ADDR) ((unsigned long int)(ADDR) | 0x1)
#define UNTAG_ADDR(ADDR) ((unsigned long int)(ADDR) & ~(0x1))
#define PUT_DATA_IN_LEAF_TRIE_NODE(TR_NODE, DATA) TrNode_child(TR_NODE) = (TrNode)TAG_ADDR(DATA)
#define GET_DATA_FROM_LEAF_TRIE_NODE(TR_NODE) UNTAG_ADDR(TrNode_child(TR_NODE))
#define MARK_AS_LEAF_TRIE_NODE(TR_NODE) PUT_DATA_IN_LEAF_TRIE_NODE(TR_NODE, TrNode_child(TR_NODE))
#define IS_LEAF_TRIE_NODE(TR_NODE) ((unsigned long int)(TrNode_child(TR_NODE)) & 0x1)
#define IsTrieVar(TERM, STACK, STACK_BASE) ((YAP_Term *)(TERM) > STACK && (YAP_Term *)(TERM) <= STACK_BASE)
#define MkTrieVar(INDEX) ((INDEX) << 4)
#define TrieVarIndex(TERM) ((TERM) >> 4)
#define BASE_HASH_BUCKETS 64
#define MAX_NODES_PER_TRIE_LEVEL 8
#define MAX_NODES_PER_BUCKET (MAX_NODES_PER_TRIE_LEVEL / 2)
#define HASH_TERM(TERM, SEED) (((TERM) >> 4) & (SEED))
#define IS_HASH_NODE(NODE) (TrHash_mark(NODE) == NULL)
#define BASE_SAVE_MARK 1000 /* could lead to errors if the number of different variables in a term is greater than it */
#define HASH_SAVE_MARK ((YAP_Term) MkTrieVar(BASE_SAVE_MARK))
#define ATOM_SAVE_MARK ((YAP_Term) MkTrieVar(BASE_SAVE_MARK + 1))
#define FUNCTOR_SAVE_MARK ((YAP_Term) MkTrieVar(BASE_SAVE_MARK + 2))
#define FLOAT_SAVE_MARK ((YAP_Term) MkTrieVar(BASE_SAVE_MARK + 3))
#define STACK_NOT_EMPTY(STACK, STACK_BASE) STACK != STACK_BASE
#define POP_UP(STACK) *--STACK
#define POP_DOWN(STACK) *++STACK
#define PUSH_UP(STACK, ITEM, STACK_TOP) \
{ if (STACK < STACK_TOP) { \
fprintf(stderr, "**************************************\n"); \
fprintf(stderr, " Tries core module: term stack full\n"); \
fprintf(stderr, "**************************************\n"); \
} \
*STACK = (YAP_Term)(ITEM); \
STACK--; \
}
#define PUSH_DOWN(STACK, ITEM, STACK_TOP) \
{ if (STACK > STACK_TOP) { \
fprintf(stderr, "**************************************\n"); \
fprintf(stderr, " Tries core module: term stack full\n"); \
fprintf(stderr, "**************************************\n"); \
} \
*STACK = (YAP_Term)(ITEM); \
STACK++; \
}
#define new_struct(STR, STR_TYPE, STR_SIZE) \
STR = (STR_TYPE *) YAP_AllocSpaceFromYap(STR_SIZE)
#define new_trie_engine(TR_ENGINE) \
{ new_struct(TR_ENGINE, TYPE_TR_ENGINE, SIZEOF_TR_ENGINE); \
TrEngine_trie(TR_ENGINE) = NULL; \
TrEngine_memory(TR_ENGINE) = 0; \
TrEngine_tries(TR_ENGINE) = 0; \
TrEngine_entries(TR_ENGINE) = 0; \
TrEngine_nodes(TR_ENGINE) = 0; \
TrEngine_memory_max(TR_ENGINE) = 0; \
TrEngine_tries_max(TR_ENGINE) = 0; \
TrEngine_entries_max(TR_ENGINE) = 0; \
TrEngine_nodes_max(TR_ENGINE) = 0; \
}
#define new_trie_node(TR_NODE, ENTRY, PARENT, CHILD, NEXT, PREVIOUS) \
{ new_struct(TR_NODE, TYPE_TR_NODE, SIZEOF_TR_NODE); \
TrNode_entry(TR_NODE) = ENTRY; \
TrNode_parent(TR_NODE) = PARENT; \
TrNode_child(TR_NODE) = CHILD; \
TrNode_next(TR_NODE) = NEXT; \
TrNode_previous(TR_NODE) = PREVIOUS; \
INCREMENT_NODES(CURRENT_TRIE_ENGINE); \
INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_NODE); \
}
#define new_trie_hash(TR_HASH, NUM_NODES, NUM_BUCKETS) \
{ new_struct(TR_HASH, TYPE_TR_HASH, SIZEOF_TR_HASH); \
TrHash_mark(TR_HASH) = NULL; \
TrHash_num_buckets(TR_HASH) = NUM_BUCKETS; \
new_hash_buckets(TR_HASH, NUM_BUCKETS); \
TrHash_num_nodes(TR_HASH) = NUM_NODES; \
INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_HASH); \
}
#define new_hash_buckets(TR_HASH, NUM_BUCKETS) \
{ int i; void **ptr; \
new_struct(ptr, void *, NUM_BUCKETS * sizeof(void *)); \
TrHash_buckets(TR_HASH) = (TYPE_TR_NODE **) ptr; \
for (i = NUM_BUCKETS; i != 0; i--) \
*ptr++ = NULL; \
INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, (NUM_BUCKETS) * SIZEOF_TR_BUCKET); \
}
#define expand_auxiliary_term_stack() \
{ YAP_Term *aux_stack; \
YAP_Int aux_size = CURRENT_AUXILIARY_TERM_STACK_SIZE * sizeof(YAP_Term); \
new_struct(aux_stack, YAP_Term, aux_size * 2); \
memcpy(aux_stack, AUXILIARY_TERM_STACK, aux_size); \
free_struct(AUXILIARY_TERM_STACK); \
AUXILIARY_TERM_STACK = aux_stack; \
CURRENT_AUXILIARY_TERM_STACK_SIZE *= 2; \
}
#define free_struct(STR) \
YAP_FreeSpaceFromYap((char *) (STR))
#define free_trie_node(STR) \
{ free_struct(STR); \
DECREMENT_NODES(CURRENT_TRIE_ENGINE); \
DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_NODE); \
}
#define free_trie_hash(STR) \
{ free_struct(STR); \
DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_HASH); \
}
#define free_hash_buckets(STR, NUM_BUCKETS) \
{ free_struct(STR); \
DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, (NUM_BUCKETS) * SIZEOF_TR_BUCKET); \
}
#define INCREMENT_MEMORY(TR_ENGINE, SIZE) \
{ TrEngine_memory(TR_ENGINE) += SIZE; \
if (TrEngine_memory(TR_ENGINE) > TrEngine_memory_max(TR_ENGINE)) \
TrEngine_memory_max(TR_ENGINE) = TrEngine_memory(TR_ENGINE); \
}
#define INCREMENT_TRIES(TR_ENGINE) \
{ TrEngine_tries(TR_ENGINE)++; \
if (TrEngine_tries(TR_ENGINE) > TrEngine_tries_max(TR_ENGINE)) \
TrEngine_tries_max(TR_ENGINE) = TrEngine_tries(TR_ENGINE); \
}
#define INCREMENT_ENTRIES(TR_ENGINE) \
{ TrEngine_entries(TR_ENGINE)++; \
if (TrEngine_entries(TR_ENGINE) > TrEngine_entries_max(TR_ENGINE)) \
TrEngine_entries_max(TR_ENGINE) = TrEngine_entries(TR_ENGINE); \
}
#define INCREMENT_NODES(TR_ENGINE) \
{ TrEngine_nodes(TR_ENGINE)++; \
if (TrEngine_nodes(TR_ENGINE) > TrEngine_nodes_max(TR_ENGINE)) \
TrEngine_nodes_max(TR_ENGINE) = TrEngine_nodes(TR_ENGINE); \
}
#define DECREMENT_MEMORY(TR_ENGINE, SIZE) \
TrEngine_memory(TR_ENGINE) -= SIZE
#define DECREMENT_TRIES(TR_ENGINE) \
TrEngine_tries(TR_ENGINE)--
#define DECREMENT_ENTRIES(TR_ENGINE) \
TrEngine_entries(TR_ENGINE)--
#define DECREMENT_NODES(TR_ENGINE) \
TrEngine_nodes(TR_ENGINE)--
/* --------------------------- */
/* API */
/* --------------------------- */
inline TrEngine core_trie_init_module(void);
inline TrNode core_trie_open(TrEngine engine);
inline void core_trie_close(TrEngine engine, TrNode node, void (*destruct_function)(TrNode));
inline void core_trie_close_all(TrEngine engine, void (*destruct_function)(TrNode));
inline void core_trie_set_mode(YAP_Int mode);
inline YAP_Int core_trie_get_mode(void);
inline TrNode core_trie_put_entry(TrEngine engine, TrNode node, YAP_Term entry, YAP_Int *depth);
inline TrNode core_trie_check_entry(TrNode node, YAP_Term entry);
inline YAP_Term core_trie_get_entry(TrNode node);
inline void core_trie_remove_entry(TrEngine engine, TrNode node, void (*destruct_function)(TrNode));
inline void core_trie_remove_subtree(TrEngine engine, TrNode node, void (*destruct_function)(TrNode));
inline void core_trie_add(TrNode node_dest, TrNode node_source, void (*add_function)(TrNode, TrNode));
inline void core_trie_join(TrEngine engine, TrNode node_dest, TrNode node_source, void (*add_function)(TrNode, TrNode), void (*copy_function)(TrNode, TrNode));
inline void core_trie_intersect(TrEngine engine, TrNode node_dest, TrNode node_source, void (*add_function)(TrNode, TrNode), void (*destruct_function)(TrNode));
inline YAP_Int core_trie_count_join(TrNode node1, TrNode node2);
inline YAP_Int core_trie_count_intersect(TrNode node1, TrNode node2);
inline void core_trie_save(TrNode node, FILE *file, void (*save_function)(TrNode, FILE *));
inline TrNode core_trie_load(TrEngine engine, FILE *file, void (*load_function)(TrNode, YAP_Int, FILE *));
inline void core_trie_stats(TrEngine engine, YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes);
inline void core_trie_max_stats(TrEngine engine, YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes);
inline void core_trie_usage(TrNode node, YAP_Int *entries, YAP_Int *nodes, YAP_Int *virtual_nodes);
inline void core_trie_print(TrNode node, void (*print_function)(TrNode));

View File

@ -14,7 +14,7 @@
#include <YapInterface.h>
#include <string.h>
#include <stdio.h>
#include "base_tries.h"
#include "core_tries.h"
#include "base_itries.h"
@ -45,6 +45,7 @@ static int p_itrie_intersect(void);
static int p_itrie_count_join(void);
static int p_itrie_count_intersect(void);
static int p_itrie_save(void);
static int p_itrie_save_as_trie(void);
static int p_itrie_load(void);
static int p_itrie_stats(void);
static int p_itrie_max_stats(void);
@ -80,6 +81,7 @@ void init_itries(void) {
YAP_UserCPredicate("itrie_count_join", p_itrie_count_join, 3);
YAP_UserCPredicate("itrie_count_intersect", p_itrie_count_intersect, 3);
YAP_UserCPredicate("itrie_save", p_itrie_save, 2);
YAP_UserCPredicate("itrie_save_as_trie", p_itrie_save_as_trie, 2);
YAP_UserCPredicate("itrie_load", p_itrie_load, 2);
YAP_UserCPredicate("itrie_stats", p_itrie_stats, 4);
YAP_UserCPredicate("itrie_max_stats", p_itrie_max_stats, 4);
@ -526,6 +528,34 @@ static int p_itrie_save(void) {
#undef arg_file
/* itrie_save_as_trie(-Itrie,-FileName) */
#define arg_itrie YAP_ARG1
#define arg_file YAP_ARG2
static int p_itrie_save_as_trie(void) {
const char *file_str;
FILE *file;
/* check args */
if (!YAP_IsIntTerm(arg_itrie))
return FALSE;
if (!YAP_IsAtomTerm(arg_file))
return FALSE;
/* open file */
file_str = YAP_AtomName(YAP_AtomOfTerm(arg_file));
if (!(file = fopen(file_str, "w")))
return FALSE;
/* save itrie as trie and close file */
itrie_save_as_trie((TrEntry) YAP_IntOfTerm(arg_itrie), file);
if (fclose(file))
return FALSE;
return TRUE;
}
#undef arg_itrie
#undef arg_file
/* itrie_load(+Itrie,-FileName) */
#define arg_itrie YAP_ARG1
#define arg_file YAP_ARG2

View File

@ -14,18 +14,11 @@
#include <YapInterface.h>
#include <string.h>
#include <stdio.h>
#include "core_tries.h"
#include "base_tries.h"
/* -------------------------- */
/* Local Variables */
/* -------------------------- */
static TrEngine TRIE_ENGINE;
/* -------------------------- */
/* Procedures */
/* -------------------------- */
@ -38,6 +31,8 @@ static int p_trie_mode(void);
static int p_trie_put_entry(void);
static int p_trie_check_entry(void);
static int p_trie_get_entry(void);
static int p_trie_traverse_init(void);
static int p_trie_traverse_cont(void);
static int p_trie_remove_entry(void);
static int p_trie_remove_subtree(void);
static int p_trie_join(void);
@ -66,7 +61,7 @@ static int p_print_trie(void);
/* -------------------------- */
void init_tries(void) {
TRIE_ENGINE = trie_init_module();
trie_init_module();
YAP_UserCPredicate("trie_open", p_trie_open, 1);
YAP_UserCPredicate("trie_close", p_trie_close, 1);
@ -75,6 +70,7 @@ void init_tries(void) {
YAP_UserCPredicate("trie_put_entry", p_trie_put_entry, 3);
YAP_UserCPredicate("trie_check_entry", p_trie_check_entry, 3);
YAP_UserCPredicate("trie_get_entry", p_trie_get_entry, 2);
YAP_UserBackCPredicate("trie_traverse", p_trie_traverse_init, p_trie_traverse_cont, 2, 0);
YAP_UserCPredicate("trie_remove_entry", p_trie_remove_entry, 1);
YAP_UserCPredicate("trie_remove_subtree", p_trie_remove_subtree, 1);
YAP_UserCPredicate("trie_join", p_trie_join, 2);
@ -128,7 +124,7 @@ static int p_close_all_tries(void) {
#define arg_entry YAP_ARG3
#define arg_ref YAP_ARG4
static int p_put_trie_entry(void) {
TrNode node;
TrData data;
const char *mode_str;
YAP_Int mode, current_mode;
@ -146,9 +142,9 @@ static int p_put_trie_entry(void) {
/* put trie entry */
current_mode = trie_get_mode();
trie_set_mode(mode);
node = trie_put_entry(TRIE_ENGINE, (TrNode) YAP_IntOfTerm(arg_trie), arg_entry, NULL);
data = trie_put_entry((TrEntry) YAP_IntOfTerm(arg_trie), arg_entry);
trie_set_mode(current_mode);
return YAP_Unify(arg_ref, YAP_MkIntTerm((YAP_Int) node));
return YAP_Unify(arg_ref, YAP_MkIntTerm((YAP_Int) data));
}
#undef arg_mode
#undef arg_trie
@ -179,7 +175,7 @@ static int p_get_trie_entry(void) {
/* get trie entry */
current_mode = trie_get_mode();
trie_set_mode(mode);
entry = trie_get_entry((TrNode) YAP_IntOfTerm(arg_ref));
entry = trie_get_entry((TrData) YAP_IntOfTerm(arg_ref));
trie_set_mode(current_mode);
return YAP_Unify(arg_entry, entry);
}
@ -208,15 +204,15 @@ static int p_print_trie(void) {
/* trie_open(+Trie) */
#define arg_trie YAP_ARG1
static int p_trie_open(void) {
TrNode node;
TrEntry trie;
/* check arg */
if (!YAP_IsVarTerm(arg_trie))
return FALSE;
/* open trie */
node = trie_open(TRIE_ENGINE);
return YAP_Unify(arg_trie, YAP_MkIntTerm((YAP_Int) node));
trie = trie_open();
return YAP_Unify(arg_trie, YAP_MkIntTerm((YAP_Int) trie));
}
#undef arg_trie
@ -229,7 +225,7 @@ static int p_trie_close(void) {
return FALSE;
/* close trie */
trie_close(TRIE_ENGINE, (TrNode) YAP_IntOfTerm(arg_trie), NULL);
trie_close((TrEntry) YAP_IntOfTerm(arg_trie));
return TRUE;
}
#undef arg_trie
@ -237,7 +233,7 @@ static int p_trie_close(void) {
/* trie_close_all() */
static int p_trie_close_all(void) {
trie_close_all(TRIE_ENGINE, NULL);
trie_close_all();
return TRUE;
}
@ -280,15 +276,15 @@ static int p_trie_mode(void) {
#define arg_entry YAP_ARG2
#define arg_ref YAP_ARG3
static int p_trie_put_entry(void) {
TrNode node;
TrData data;
/* check args */
if (!YAP_IsIntTerm(arg_trie))
return FALSE;
/* put trie entry */
node = trie_put_entry(TRIE_ENGINE, (TrNode) YAP_IntOfTerm(arg_trie), arg_entry, NULL);
return YAP_Unify(arg_ref, YAP_MkIntTerm((YAP_Int) node));
data = trie_put_entry((TrEntry) YAP_IntOfTerm(arg_trie), arg_entry);
return YAP_Unify(arg_ref, YAP_MkIntTerm((YAP_Int) data));
}
#undef arg_trie
#undef arg_entry
@ -300,16 +296,16 @@ static int p_trie_put_entry(void) {
#define arg_entry YAP_ARG2
#define arg_ref YAP_ARG3
static int p_trie_check_entry(void) {
TrNode node;
TrData data;
/* check args */
if (!YAP_IsIntTerm(arg_trie))
return FALSE;
/* check trie entry */
if (!(node = trie_check_entry((TrNode) YAP_IntOfTerm(arg_trie), arg_entry)))
if (!(data = trie_check_entry((TrEntry) YAP_IntOfTerm(arg_trie), arg_entry)))
return FALSE;
return YAP_Unify(arg_ref, YAP_MkIntTerm((YAP_Int) node));
return YAP_Unify(arg_ref, YAP_MkIntTerm((YAP_Int) data));
}
#undef arg_trie
#undef arg_entry
@ -327,13 +323,51 @@ static int p_trie_get_entry(void) {
return FALSE;
/* get trie entry */
entry = trie_get_entry((TrNode) YAP_IntOfTerm(arg_ref));
entry = trie_get_entry((TrData) YAP_IntOfTerm(arg_ref));
return YAP_Unify(arg_entry, entry);
}
#undef arg_ref
#undef arg_entry
/* trie_traverse(-Trie,+Ref) */
#define arg_trie YAP_ARG1
#define arg_ref YAP_ARG2
static int p_trie_traverse_init(void) {
TrData data;
/* check arg */
if (!YAP_IsIntTerm(arg_trie))
return FALSE;
/* traverse trie */
if (!(data = trie_traverse_init((TrEntry) YAP_IntOfTerm(arg_trie)))) {
YAP_cut_fail();
return FALSE;
}
return YAP_Unify(arg_ref, YAP_MkIntTerm((YAP_Int) data));
}
#undef arg_trie
#undef arg_ref
/* trie_traverse(-Trie,+Ref) */
#define arg_trie YAP_ARG1
#define arg_ref YAP_ARG2
static int p_trie_traverse_cont(void) {
TrData data;
/* traverse trie */
if (!(data = trie_traverse_cont((TrEntry) YAP_IntOfTerm(arg_trie)))) {
YAP_cut_fail();
return FALSE;
}
return YAP_Unify(arg_ref, YAP_MkIntTerm((YAP_Int) data));
}
#undef arg_trie
#undef arg_ref
/* trie_remove_entry(-Ref) */
#define arg_ref YAP_ARG1
static int p_trie_remove_entry(void) {
@ -342,7 +376,7 @@ static int p_trie_remove_entry(void) {
return FALSE;
/* remove trie entry */
trie_remove_entry(TRIE_ENGINE, (TrNode) YAP_IntOfTerm(arg_ref), NULL);
trie_remove_entry((TrData) YAP_IntOfTerm(arg_ref));
return TRUE;
}
#undef arg_ref
@ -356,7 +390,7 @@ static int p_trie_remove_subtree(void) {
return FALSE;
/* remove trie subtree */
trie_remove_subtree(TRIE_ENGINE, (TrNode) YAP_IntOfTerm(arg_ref), NULL);
trie_remove_subtree((TrData) YAP_IntOfTerm(arg_ref));
return TRUE;
}
#undef arg_ref
@ -373,7 +407,7 @@ static int p_trie_join(void) {
return FALSE;
/* join trie */
trie_join(TRIE_ENGINE, (TrNode) YAP_IntOfTerm(arg_trie_dest), (TrNode) YAP_IntOfTerm(arg_trie_source), NULL, NULL);
trie_join((TrEntry) YAP_IntOfTerm(arg_trie_dest), (TrEntry) YAP_IntOfTerm(arg_trie_source));
return TRUE;
}
#undef arg_trie_dest
@ -391,7 +425,7 @@ static int p_trie_intersect(void) {
return FALSE;
/* intersect trie */
trie_intersect(TRIE_ENGINE, (TrNode) YAP_IntOfTerm(arg_trie_dest), (TrNode) YAP_IntOfTerm(arg_trie_source), NULL, NULL);
trie_intersect((TrEntry) YAP_IntOfTerm(arg_trie_dest), (TrEntry) YAP_IntOfTerm(arg_trie_source));
return TRUE;
}
#undef arg_trie_dest
@ -412,7 +446,7 @@ static int p_trie_count_join(void) {
return FALSE;
/* count join trie */
entries = trie_count_join((TrNode) YAP_IntOfTerm(arg_trie1), (TrNode) YAP_IntOfTerm(arg_trie2));
entries = trie_count_join((TrEntry) YAP_IntOfTerm(arg_trie1), (TrEntry) YAP_IntOfTerm(arg_trie2));
return YAP_Unify(arg_entries, YAP_MkIntTerm(entries));
}
#undef arg_trie1
@ -434,7 +468,7 @@ static int p_trie_count_intersect(void) {
return FALSE;
/* count intersect trie */
entries = trie_count_intersect((TrNode) YAP_IntOfTerm(arg_trie1), (TrNode) YAP_IntOfTerm(arg_trie2));
entries = trie_count_intersect((TrEntry) YAP_IntOfTerm(arg_trie1), (TrEntry) YAP_IntOfTerm(arg_trie2));
return YAP_Unify(arg_entries, YAP_MkIntTerm(entries));
}
#undef arg_trie1
@ -461,7 +495,7 @@ static int p_trie_save(void) {
return FALSE;
/* save trie and close file */
trie_save((TrNode) YAP_IntOfTerm(arg_trie), file, NULL);
trie_save((TrEntry) YAP_IntOfTerm(arg_trie), file);
if (fclose(file))
return FALSE;
return TRUE;
@ -474,7 +508,7 @@ static int p_trie_save(void) {
#define arg_trie YAP_ARG1
#define arg_file YAP_ARG2
static int p_trie_load(void) {
TrNode node;
TrEntry data;
const char *file_str;
FILE *file;
@ -490,10 +524,10 @@ static int p_trie_load(void) {
return FALSE;
/* load trie and close file */
node = trie_load(TRIE_ENGINE, file, NULL);
data = trie_load(file);
if (fclose(file))
return FALSE;
return YAP_Unify(arg_trie, YAP_MkIntTerm((YAP_Int) node));
return YAP_Unify(arg_trie, YAP_MkIntTerm((YAP_Int) data));
}
#undef arg_trie
#undef arg_file
@ -508,7 +542,7 @@ static int p_trie_stats(void) {
YAP_Int memory, tries, entries, nodes;
/* get stats */
trie_stats(TRIE_ENGINE, &memory, &tries, &entries, &nodes);
trie_stats(&memory, &tries, &entries, &nodes);
if (!YAP_Unify(arg_memory, YAP_MkIntTerm(memory)))
return FALSE;
if (!YAP_Unify(arg_tries, YAP_MkIntTerm(tries)))
@ -534,7 +568,7 @@ static int p_trie_max_stats(void) {
YAP_Int memory, tries, entries, nodes;
/* get stats */
trie_max_stats(TRIE_ENGINE, &memory, &tries, &entries, &nodes);
trie_max_stats(&memory, &tries, &entries, &nodes);
if (!YAP_Unify(arg_memory, YAP_MkIntTerm(memory)))
return FALSE;
if (!YAP_Unify(arg_tries, YAP_MkIntTerm(tries)))
@ -564,7 +598,7 @@ static int p_trie_usage(void) {
return FALSE;
/* get trie usage */
trie_usage((TrNode) YAP_IntOfTerm(arg_trie), &entries, &nodes, &virtualnodes);
trie_usage((TrEntry) YAP_IntOfTerm(arg_trie), &entries, &nodes, &virtualnodes);
if (!YAP_Unify(arg_entries, YAP_MkIntTerm(entries)))
return FALSE;
if (!YAP_Unify(arg_nodes, YAP_MkIntTerm(nodes)))
@ -587,7 +621,7 @@ static int p_trie_print(void) {
return FALSE;
/* print trie */
trie_print((TrNode) YAP_IntOfTerm(arg_trie), NULL);
trie_print((TrEntry) YAP_IntOfTerm(arg_trie));
return TRUE;
}
#undef arg_trie