tries library module

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1877 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
ricroc 2007-05-02 15:31:23 +00:00
parent e9c848ea41
commit d9b7100f37
8 changed files with 3232 additions and 0 deletions

View File

@ -52,6 +52,7 @@ PROGRAMS= $(srcdir)/apply_macros.yap \
$(srcdir)/system.yap \
$(srcdir)/terms.yap \
$(srcdir)/tries.yap \
$(srcdir)/itries.yap \
$(srcdir)/timeout.yap \
$(srcdir)/trees.yap \
$(srcdir)/ugraphs.yap \

71
library/Tries/Makefile.in Normal file
View File

@ -0,0 +1,71 @@
#
# default base directory for YAP installation
# (EROOT for architecture-dependent files)
#
prefix = @prefix@
ROOTDIR = $(prefix)
EROOTDIR = @exec_prefix@
#
# where the binary should be
#
BINDIR = $(EROOTDIR)/bin
#
# where YAP should look for libraries
#
LIBDIR=$(EROOTDIR)/lib/Yap
#
#
CC=@CC@
CFLAGS= @CFLAGS@ $(YAP_EXTRAS) $(DEFS) -I$(srcdir) -I../.. -I$(srcdir)/../../include
#
#
# You shouldn't need to change what follows.
#
INSTALL=@INSTALL@
INSTALL_DATA=@INSTALL_DATA@
INSTALL_PROGRAM=@INSTALL_PROGRAM@
SHELL=/bin/sh
RANLIB=@RANLIB@
srcdir=@srcdir@
SHLIB_CFLAGS=@SHLIB_CFLAGS@
SHLIB_SUFFIX=@SHLIB_SUFFIX@
#4.1VPATH=@srcdir@:@srcdir@/OPTYap
CWD=$(PWD)
#
OBJS=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
# create a libray
all: $(SOBJS)
base_tries.o: $(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
$(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
$(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
$(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@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
install: all
$(INSTALL_PROGRAM) $(SOBJS) $(DESTDIR)$(LIBDIR)
clean:
rm -f *.o *~ $(OBJS) $(SOBJS) *.BAK

373
library/Tries/base_itries.c Normal file
View File

@ -0,0 +1,373 @@
/*********************************
File: base_itries.c
Author: Ricardo Rocha
Comments: Tries module for ILP
version: $ID$
*********************************/
/* -------------------------- */
/* Includes */
/* -------------------------- */
#include <YapInterface.h>
#include <stdio.h>
#include <string.h>
#include "base_tries.h"
#include "base_itries.h"
/* -------------------------- */
/* Local Variables */
/* -------------------------- */
static TrEngine ITRIE_ENGINE;
static TrEntry FIRST_ITRIE, CURRENT_ITRIE;
/* -------------------------- */
/* API */
/* -------------------------- */
inline
void itrie_init_module(void) {
ITRIE_ENGINE = trie_init_module();
FIRST_ITRIE = NULL;
return;
}
inline
void itrie_data_save(TrNode node, FILE *file) {
TrData data;
data = (TrData) GET_DATA_FROM_LEAF_TRIE_NODE(node);
fprintf(file, "%ld %ld %ld ", TrData_pos(data), TrData_neg(data), TrData_timestamp(data));
return;
}
inline
void itrie_data_load(TrNode node, YAP_Int depth, FILE *file) {
TrData data;
YAP_Int pos, neg, timestamp;
fscanf(file, "%ld %ld %ld", &pos, &neg, &timestamp);
new_itrie_data(data, CURRENT_ITRIE, node, pos, neg, timestamp, depth);
PUT_DATA_IN_LEAF_TRIE_NODE(node, data);
return;
}
inline
void itrie_data_print(TrNode node) {
TrData data;
data = (TrData) GET_DATA_FROM_LEAF_TRIE_NODE(node);
printf(" pos: %ld neg: %ld timestamp: %ld\n", TrData_pos(data), TrData_neg(data), TrData_timestamp(data));
return;
}
inline
void itrie_data_destruct(TrNode node) {
TrEntry itrie;
TrData data;
data = (TrData) GET_DATA_FROM_LEAF_TRIE_NODE(node);
itrie = TrData_itrie(data);
if (data == TrEntry_traverse_data(itrie))
TrEntry_traverse_data(itrie) = TrData_next(data);
if (TrData_next(data)) {
TrData_previous(TrData_next(data)) = TrData_previous(data);
TrData_next(TrData_previous(data)) = TrData_next(data);
} else
TrData_next(TrData_previous(data)) = NULL;
free_itrie_data(data);
return;
}
inline
void itrie_data_add(TrNode node_dest, TrNode node_source) {
TrData data_dest, data_source;
data_source = (TrData) GET_DATA_FROM_LEAF_TRIE_NODE(node_source);
if (!(data_dest = (TrData) GET_DATA_FROM_LEAF_TRIE_NODE(node_dest))) {
new_itrie_data(data_dest, CURRENT_ITRIE, node_dest, TrData_pos(data_source), TrData_neg(data_source),
TrData_timestamp(data_source), TrData_depth(data_source));
PUT_DATA_IN_LEAF_TRIE_NODE(node_dest, data_dest);
} else {
TrData_pos(data_dest) += TrData_pos(data_source);
TrData_neg(data_dest) += TrData_neg(data_source);
if (TrData_timestamp(data_dest) < TrData_timestamp(data_source))
TrData_timestamp(data_dest) = TrData_timestamp(data_source);
}
return;
}
inline
void itrie_data_subtract(TrNode node_dest, TrNode node_source) {
TrData data_dest, data_source;
data_dest = (TrData) GET_DATA_FROM_LEAF_TRIE_NODE(node_dest);
data_source = (TrData) GET_DATA_FROM_LEAF_TRIE_NODE(node_source);
TrData_pos(data_dest) -= TrData_pos(data_source);
TrData_neg(data_dest) -= TrData_neg(data_source);
if (TrData_timestamp(data_dest) < TrData_timestamp(data_source))
TrData_timestamp(data_dest) = TrData_timestamp(data_source);
return;
}
inline
TrEntry itrie_open(void) {
TrEntry itrie;
TrNode node;
node = trie_open(ITRIE_ENGINE);
new_itrie_entry(itrie, node);
if (FIRST_ITRIE)
TrEntry_previous(FIRST_ITRIE) = itrie;
FIRST_ITRIE = itrie;
return itrie;
}
inline
void itrie_close(TrEntry itrie) {
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);
} else
TrEntry_next(TrEntry_previous(itrie)) = NULL;
free_itrie_entry(itrie);
return;
}
inline
void itrie_close_all(void) {
TrEntry itrie;
trie_close_all(ITRIE_ENGINE, &itrie_data_destruct);
while (FIRST_ITRIE) {
itrie = TrEntry_next(FIRST_ITRIE);
free_itrie_entry(FIRST_ITRIE);
FIRST_ITRIE = itrie;
}
return;
}
inline
void itrie_set_mode(TrEntry itrie, YAP_Int mode) {
TrEntry_mode(itrie) = mode;
return;
}
inline
YAP_Int itrie_get_mode(TrEntry itrie) {
return TrEntry_mode(itrie);
}
inline
void itrie_set_timestamp(TrEntry itrie, YAP_Int timestamp) {
TrEntry_timestamp(itrie) = timestamp;
return;
}
inline
YAP_Int itrie_get_timestamp(TrEntry itrie) {
return TrEntry_timestamp(itrie);
}
inline
void itrie_put_entry(TrEntry itrie, YAP_Term entry) {
TrData data;
TrNode node;
YAP_Int depth;
node = 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);
}
update_itrie_data(data, TrEntry_timestamp(itrie), TrEntry_mode(itrie));
return;
}
inline
void itrie_update_entry(TrEntry itrie, YAP_Term entry) {
TrData data;
TrNode node;
if ((node = 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));
}
return;
}
inline
TrData itrie_check_entry(TrEntry itrie, YAP_Term entry) {
TrNode node;
if (!(node = trie_check_entry(TrEntry_trie(itrie), entry)))
return NULL;
return (TrData) GET_DATA_FROM_LEAF_TRIE_NODE(node);
}
inline
YAP_Term itrie_get_entry(TrData data) {
return trie_get_entry(TrData_leaf(data));
}
inline
void itrie_get_data(TrData data, YAP_Int *pos, YAP_Int *neg, YAP_Int *timestamp) {
*pos = TrData_pos(data);
*neg = TrData_neg(data);
*timestamp = TrData_timestamp(data);
return;
}
inline
TrData itrie_traverse_init(TrEntry itrie) {
TrData data, *bucket;
YAP_Int traverse_bucket = 0;
do {
bucket = TrEntry_bucket(itrie, traverse_bucket);
traverse_bucket++;
} while (!*bucket && traverse_bucket != TrEntry_num_buckets(itrie));
data = *bucket;
if (data) {
TrEntry_traverse_bucket(itrie) = traverse_bucket;
TrEntry_traverse_data(itrie) = TrData_next(data);
}
return data;
}
inline
TrData itrie_traverse_cont(TrEntry itrie) {
TrData data, *bucket;
YAP_Int traverse_bucket;
data = TrEntry_traverse_data(itrie);
if (!data) {
traverse_bucket = TrEntry_traverse_bucket(itrie);
do {
bucket = TrEntry_bucket(itrie, traverse_bucket);
traverse_bucket++;
} while (!*bucket && traverse_bucket != TrEntry_num_buckets(itrie));
data = *bucket;
if (data) {
TrEntry_traverse_bucket(itrie) = traverse_bucket;
TrEntry_traverse_data(itrie) = TrData_next(data);
}
} else
TrEntry_traverse_data(itrie) = TrData_next(data);
return data;
}
inline
void itrie_remove_entry(TrData data) {
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);
return;
}
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);
return;
}
inline
void itrie_add(TrEntry itrie_dest, TrEntry itrie_source) {
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);
return;
}
inline
void itrie_save(TrEntry itrie, FILE *file) {
trie_save(TrEntry_trie(itrie), file, &itrie_data_save);
return;
}
inline
TrEntry itrie_load(FILE *file) {
TrEntry itrie;
TrNode node;
new_itrie_entry(itrie, NULL);
if (FIRST_ITRIE)
TrEntry_previous(FIRST_ITRIE) = itrie;
FIRST_ITRIE = itrie;
CURRENT_ITRIE = itrie;
node = trie_load(ITRIE_ENGINE, file, &itrie_data_load);
TrEntry_trie(itrie) = node;
return itrie;
}
inline
void itrie_stats(YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes) {
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);
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);
return;
}
inline
void itrie_print(TrEntry itrie) {
trie_print(TrEntry_trie(itrie), &itrie_data_print);
return;
}

201
library/Tries/base_itries.h Normal file
View File

@ -0,0 +1,201 @@
/*********************************
File: base_itries.h
Author: Ricardo Rocha
Comments: Tries module for ILP
version: $ID$
*********************************/
/* --------------------------- */
/* Defines */
/* --------------------------- */
#define ITRIES_MODE_NONE 0
#define ITRIES_MODE_INC_POS 1
#define ITRIES_MODE_DEC_POS 2
#define ITRIES_MODE_INC_NEG 3
#define ITRIES_MODE_DEC_NEG 4
#define BASE_TR_DATA_BUCKETS 20
/* --------------------------- */
/* Structs */
/* --------------------------- */
typedef struct itrie_entry {
struct trie_node *top_trie_node;
struct itrie_data **trie_data_buckets;
struct itrie_data *traverse_trie_data;
struct itrie_entry *next;
struct itrie_entry *previous;
YAP_Int mode;
YAP_Int timestamp;
YAP_Int number_of_buckets;
YAP_Int traverse_bucket;
} *TrEntry;
#define TrEntry_trie(X) ((X)->top_trie_node)
#define TrEntry_buckets(X) ((X)->trie_data_buckets)
#define TrEntry_bucket(X,N) ((X)->trie_data_buckets + N)
#define TrEntry_traverse_data(X) ((X)->traverse_trie_data)
#define TrEntry_next(X) ((X)->next)
#define TrEntry_previous(X) ((X)->previous)
#define TrEntry_mode(X) ((X)->mode)
#define TrEntry_timestamp(X) ((X)->timestamp)
#define TrEntry_num_buckets(X) ((X)->number_of_buckets)
#define TrEntry_traverse_bucket(X) ((X)->traverse_bucket)
typedef struct itrie_data {
struct itrie_entry *itrie;
struct trie_node *leaf_trie_node;
struct itrie_data *next;
struct itrie_data *previous;
YAP_Int pos;
YAP_Int neg;
YAP_Int timestamp;
YAP_Int depth;
} *TrData;
#define TrData_itrie(X) ((X)->itrie)
#define TrData_leaf(X) ((X)->leaf_trie_node)
#define TrData_next(X) ((X)->next)
#define TrData_previous(X) ((X)->previous)
#define TrData_pos(X) ((X)->pos)
#define TrData_neg(X) ((X)->neg)
#define TrData_timestamp(X) ((X)->timestamp)
#define TrData_depth(X) ((X)->depth)
#define TYPE_TR_ENTRY struct itrie_entry
#define TYPE_TR_DATA struct itrie_data
#define SIZEOF_TR_ENTRY sizeof(TYPE_TR_ENTRY)
#define SIZEOF_TR_DATA sizeof(TYPE_TR_DATA)
#define SIZEOF_TR_DATA_BUCKET sizeof(TYPE_TR_DATA *)
#define AS_TR_ENTRY_NEXT(ADDR) (TrEntry)((unsigned long int)(ADDR) - sizeof(struct trie_node *) - sizeof(struct itrie_data **) - sizeof(struct itrie_data *))
#define AS_TR_DATA_NEXT(ADDR) (TrData)((unsigned long int)(ADDR) - sizeof(struct itrie_entry *) - sizeof(struct trie_node *))
/* --------------------------- */
/* Macros */
/* --------------------------- */
#define new_itrie_entry(TR_ENTRY, TR_NODE) \
{ new_struct(TR_ENTRY, TYPE_TR_ENTRY, SIZEOF_TR_ENTRY); \
TrEntry_mode(TR_ENTRY) = ITRIES_MODE_NONE; \
TrEntry_timestamp(TR_ENTRY) = -1; \
TrEntry_num_buckets(TR_ENTRY) = BASE_TR_DATA_BUCKETS; \
new_itrie_buckets(TR_ENTRY, BASE_TR_DATA_BUCKETS); \
TrEntry_trie(TR_ENTRY) = TR_NODE; \
TrEntry_next(TR_ENTRY) = FIRST_ITRIE; \
TrEntry_previous(TR_ENTRY) = AS_TR_ENTRY_NEXT(&FIRST_ITRIE); \
INCREMENT_MEMORY(ITRIE_ENGINE, SIZEOF_TR_ENTRY); \
}
#define new_itrie_buckets(TR_ENTRY, NUM_BUCKETS) \
{ YAP_Int i; void **ptr; \
new_struct(ptr, void *, NUM_BUCKETS * sizeof(void *)); \
TrEntry_buckets(TR_ENTRY) = (TYPE_TR_DATA **) ptr; \
for (i = NUM_BUCKETS; i != 0; i--) \
*ptr++ = NULL; \
INCREMENT_MEMORY(ITRIE_ENGINE, (NUM_BUCKETS) * SIZEOF_TR_DATA_BUCKET); \
}
#define new_itrie_data(TR_DATA, TR_ENTRY, TR_NODE, POS, NEG, TIME, DEPTH) \
{ TrData *bucket; \
new_struct(TR_DATA, TYPE_TR_DATA, SIZEOF_TR_DATA); \
TrData_pos(TR_DATA) = POS; \
TrData_neg(TR_DATA) = NEG; \
TrData_timestamp(TR_DATA) = TIME; \
TrData_depth(TR_DATA) = DEPTH; \
TrData_itrie(TR_DATA) = TR_ENTRY; \
TrData_leaf(TR_DATA) = TR_NODE; \
if (DEPTH >= TrEntry_num_buckets(TR_ENTRY)) { \
YAP_Int i, new_num_buckets = DEPTH + BASE_TR_DATA_BUCKETS; \
bucket = TrEntry_buckets(TR_ENTRY); \
new_itrie_buckets(TR_ENTRY, new_num_buckets); \
memcpy(TrEntry_buckets(TR_ENTRY), bucket, \
TrEntry_num_buckets(TR_ENTRY) * SIZEOF_TR_DATA_BUCKET); \
free_itrie_buckets(bucket, TrEntry_num_buckets(TR_ENTRY)); \
bucket = TrEntry_buckets(TR_ENTRY); \
for (i = 0; i != TrEntry_num_buckets(TR_ENTRY); i++) { \
if (*bucket) \
TrData_previous(*bucket) = AS_TR_DATA_NEXT(bucket); \
bucket++; \
} \
TrEntry_num_buckets(TR_ENTRY) = new_num_buckets; \
} \
bucket = TrEntry_bucket(TR_ENTRY, DEPTH); \
TrData_next(TR_DATA) = *bucket; \
TrData_previous(TR_DATA) = AS_TR_DATA_NEXT(bucket); \
if (*bucket) \
TrData_previous(*bucket) = TR_DATA; \
*bucket = TR_DATA; \
INCREMENT_MEMORY(ITRIE_ENGINE, SIZEOF_TR_DATA); \
}
#define update_itrie_data(TR_DATA, TIMESTAMP, ITRIES_MODE) \
if (TrData_timestamp(TR_DATA) != TIMESTAMP) { \
if (ITRIES_MODE == ITRIES_MODE_INC_POS) \
TrData_pos(TR_DATA)++; \
else if (ITRIES_MODE == ITRIES_MODE_DEC_POS) \
TrData_pos(TR_DATA)--; \
else if (ITRIES_MODE == ITRIES_MODE_INC_NEG) \
TrData_neg(TR_DATA)++; \
else if (ITRIES_MODE == ITRIES_MODE_DEC_NEG) \
TrData_neg(TR_DATA)--; \
TrData_timestamp(TR_DATA) = TIMESTAMP; \
}
#define free_itrie_entry(STR) \
{ free_itrie_buckets(TrEntry_buckets(STR), TrEntry_num_buckets(STR)); \
free_struct(STR); \
DECREMENT_MEMORY(ITRIE_ENGINE, SIZEOF_TR_ENTRY); \
}
#define free_itrie_buckets(STR, NUM_BUCKETS) \
{ free_struct(STR); \
DECREMENT_MEMORY(ITRIE_ENGINE, (NUM_BUCKETS) * SIZEOF_TR_DATA_BUCKET); \
}
#define free_itrie_data(STR) \
{ free_struct(STR); \
DECREMENT_MEMORY(ITRIE_ENGINE, SIZEOF_TR_DATA); \
}
/* --------------------------- */
/* API */
/* --------------------------- */
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_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);
inline TrEntry itrie_open(void);
inline void itrie_close(TrEntry itrie);
inline void itrie_close_all(void);
inline void itrie_set_mode(TrEntry itrie, YAP_Int mode);
inline YAP_Int itrie_get_mode(TrEntry itrie);
inline void itrie_set_timestamp(TrEntry itrie, YAP_Int timestamp);
inline YAP_Int itrie_get_timestamp(TrEntry itrie);
inline void itrie_put_entry(TrEntry itrie, YAP_Term entry);
inline void itrie_update_entry(TrEntry itrie, YAP_Term entry);
inline TrData itrie_check_entry(TrEntry itrie, YAP_Term entry);
inline YAP_Term itrie_get_entry(TrData data);
inline void itrie_get_data(TrData data, YAP_Int *pos, YAP_Int *neg, YAP_Int *timestamp);
inline TrData itrie_traverse_init(TrEntry itrie);
inline TrData itrie_traverse_cont(TrEntry itrie);
inline void itrie_remove_entry(TrData data);
inline void itrie_remove_subtree(TrData data);
inline void itrie_join(TrEntry itrie_dest, TrEntry itrie_source);
inline void itrie_add(TrEntry itrie_dest, TrEntry itrie_source);
inline void itrie_subtract(TrEntry itrie_dest, TrEntry itrie_source);
inline void itrie_save(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);
inline void itrie_usage(TrEntry itrie, YAP_Int *entries, YAP_Int *nodes, YAP_Int *virtual_nodes);
inline void itrie_print(TrEntry itrie);

1226
library/Tries/base_tries.c Normal file

File diff suppressed because it is too large Load Diff

274
library/Tries/base_tries.h Normal file
View File

@ -0,0 +1,274 @@
/*********************************************
File: base_tries.h
Author: Ricardo Rocha
Comments: Tries base 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 AUXILIARY_TERM_STACK_SIZE 1000
#define TRIE_PRINT_NORMAL 0
#define TRIE_PRINT_FLOAT 1
#define TRIE_PRINT_FLOAT2 2
#define TRIE_PRINT_FLOAT_END 3
/* --------------------------- */
/* 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;
#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 HASH_SAVE_MARK ((YAP_Term) MkTrieVar(AUXILIARY_TERM_STACK_SIZE))
#define ATOM_SAVE_MARK ((YAP_Term) MkTrieVar(AUXILIARY_TERM_STACK_SIZE + 1))
#define FUNCTOR_SAVE_MARK ((YAP_Term) MkTrieVar(AUXILIARY_TERM_STACK_SIZE + 2))
#define FLOAT_SAVE_MARK ((YAP_Term) MkTrieVar(AUXILIARY_TERM_STACK_SIZE + 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_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 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 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_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_join(TrEngine engine, TrNode node_dest, TrNode node_source, void (*add_function)(TrNode, TrNode));
inline void trie_add(TrNode node_dest, TrNode node_source, void (*add_function)(TrNode, TrNode));
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));

581
library/Tries/itries.c Normal file
View File

@ -0,0 +1,581 @@
/*********************************
File: itries.c
Author: Ricardo Rocha
Comments: Tries module for ILP
version: $ID$
*********************************/
/* -------------------------- */
/* Includes */
/* -------------------------- */
#include <YapInterface.h>
#include <string.h>
#include <stdio.h>
#include "base_tries.h"
#include "base_itries.h"
/* -------------------------- */
/* Procedures */
/* -------------------------- */
void init_itries(void);
static int p_itrie_open(void);
static int p_itrie_close(void);
static int p_itrie_close_all(void);
static int p_itrie_mode(void);
static int p_itrie_timestamp(void);
static int p_itrie_put_entry(void);
static int p_itrie_update_entry(void);
static int p_itrie_check_entry(void);
static int p_itrie_get_entry(void);
static int p_itrie_get_data(void);
static int p_itrie_traverse_init(void);
static int p_itrie_traverse_cont(void);
static int p_itrie_remove_entry(void);
static int p_itrie_remove_subtree(void);
static int p_itrie_join(void);
static int p_itrie_add(void);
static int p_itrie_subtract(void);
static int p_itrie_save(void);
static int p_itrie_load(void);
static int p_itrie_stats(void);
static int p_itrie_max_stats(void);
static int p_itrie_usage(void);
static int p_itrie_print(void);
/* -------------------------- */
/* Module Init Procedure */
/* -------------------------- */
void init_itries(void) {
itrie_init_module();
YAP_UserCPredicate("itrie_open", p_itrie_open, 1);
YAP_UserCPredicate("itrie_close", p_itrie_close, 1);
YAP_UserCPredicate("itrie_close_all", p_itrie_close_all, 0);
YAP_UserCPredicate("itrie_mode", p_itrie_mode, 2);
YAP_UserCPredicate("itrie_timestamp", p_itrie_timestamp, 2);
YAP_UserCPredicate("itrie_put_entry", p_itrie_put_entry, 2);
YAP_UserCPredicate("itrie_update_entry", p_itrie_update_entry, 2);
YAP_UserCPredicate("itrie_check_entry", p_itrie_check_entry, 3);
YAP_UserCPredicate("itrie_get_entry", p_itrie_get_entry, 2);
YAP_UserCPredicate("itrie_get_data", p_itrie_get_data, 2);
YAP_UserBackCPredicate("itrie_traverse", p_itrie_traverse_init, p_itrie_traverse_cont, 2, 0);
YAP_UserCPredicate("itrie_remove_entry", p_itrie_remove_entry, 1);
YAP_UserCPredicate("itrie_remove_subtree", p_itrie_remove_subtree, 1);
YAP_UserCPredicate("itrie_join", p_itrie_join, 2);
YAP_UserCPredicate("itrie_add", p_itrie_add, 2);
YAP_UserCPredicate("itrie_subtract", p_itrie_subtract, 2);
YAP_UserCPredicate("itrie_save", p_itrie_save, 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);
YAP_UserCPredicate("itrie_usage", p_itrie_usage, 4);
YAP_UserCPredicate("itrie_print", p_itrie_print, 1);
return;
}
/* -------------------------- */
/* Local Procedures */
/* -------------------------- */
/* itrie_open(+Itrie) */
#define arg_itrie YAP_ARG1
static int p_itrie_open(void) {
TrEntry itrie;
/* check arg */
if (!YAP_IsVarTerm(arg_itrie))
return FALSE;
/* open itrie */
itrie = itrie_open();
return YAP_Unify(arg_itrie, YAP_MkIntTerm((YAP_Int) itrie));
}
#undef arg_itrie
/* itrie_close(-Itrie) */
#define arg_itrie YAP_ARG1
static int p_itrie_close(void) {
/* check arg */
if (!YAP_IsIntTerm(arg_itrie))
return FALSE;
/* close itrie */
itrie_close((TrEntry) YAP_IntOfTerm(arg_itrie));
return TRUE;
}
#undef arg_itrie
/* itrie_close_all() */
static int p_itrie_close_all(void) {
itrie_close_all();
return TRUE;
}
/* itrie_mode(-Itrie,?Mode) */
#define arg_itrie YAP_ARG1
#define arg_mode YAP_ARG2
static int p_itrie_mode(void) {
YAP_Term mode_term;
const char *mode_str;
YAP_Int mode;
/* check arg */
if (!YAP_IsIntTerm(arg_itrie))
return FALSE;
/* get mode */
if (YAP_IsVarTerm(arg_mode)) {
mode = itrie_get_mode((TrEntry) YAP_IntOfTerm(arg_itrie));
if (mode == ITRIES_MODE_INC_POS)
mode_term = YAP_MkAtomTerm(YAP_LookupAtom("inc_pos"));
else if (mode == ITRIES_MODE_DEC_POS)
mode_term = YAP_MkAtomTerm(YAP_LookupAtom("dec_pos"));
else if (mode == ITRIES_MODE_INC_NEG)
mode_term = YAP_MkAtomTerm(YAP_LookupAtom("inc_neg"));
else if (mode == ITRIES_MODE_DEC_NEG)
mode_term = YAP_MkAtomTerm(YAP_LookupAtom("dec_neg"));
else if (mode == ITRIES_MODE_NONE)
mode_term = YAP_MkAtomTerm(YAP_LookupAtom("none"));
else
return FALSE;
return YAP_Unify(arg_mode, mode_term);
}
/* set mode */
mode_str = YAP_AtomName(YAP_AtomOfTerm(arg_mode));
if (!strcmp(mode_str, "inc_pos"))
mode = ITRIES_MODE_INC_POS;
else if (!strcmp(mode_str, "dec_pos"))
mode = ITRIES_MODE_DEC_POS;
else if (!strcmp(mode_str, "inc_neg"))
mode = ITRIES_MODE_INC_NEG;
else if (!strcmp(mode_str, "dec_neg"))
mode = ITRIES_MODE_DEC_NEG;
else if (!strcmp(mode_str, "none"))
mode = ITRIES_MODE_NONE;
else
return FALSE;
itrie_set_mode((TrEntry) YAP_IntOfTerm(arg_itrie), mode);
return TRUE;
}
#undef arg_itrie
#undef arg_mode
/* itrie_timestamp(-Itrie,?Time) */
#define arg_itrie YAP_ARG1
#define arg_time YAP_ARG2
static int p_itrie_timestamp(void) {
YAP_Int time;
/* check arg */
if (!YAP_IsIntTerm(arg_itrie))
return FALSE;
/* get mode */
if (YAP_IsVarTerm(arg_time)) {
time = itrie_get_timestamp((TrEntry) YAP_IntOfTerm(arg_itrie));
return YAP_Unify(arg_time, YAP_MkIntTerm(time));
}
/* set mode */
if (YAP_IsIntTerm(arg_time)) {
time = YAP_IntOfTerm(arg_time);
itrie_set_timestamp((TrEntry) YAP_IntOfTerm(arg_itrie), time);
return TRUE;
}
return FALSE;
}
#undef arg_itrie
#undef arg_time
/* itrie_put_entry(-Itrie,-Entry) */
#define arg_itrie YAP_ARG1
#define arg_entry YAP_ARG2
static int p_itrie_put_entry(void) {
/* check arg */
if (!YAP_IsIntTerm(arg_itrie))
return FALSE;
/* put entry */
itrie_put_entry((TrEntry) YAP_IntOfTerm(arg_itrie), arg_entry);
return TRUE;
}
#undef arg_itrie
#undef arg_entry
/* itrie_update_entry(-Itrie,-Entry) */
#define arg_itrie YAP_ARG1
#define arg_entry YAP_ARG2
static int p_itrie_update_entry(void) {
/* check arg */
if (!YAP_IsIntTerm(arg_itrie))
return FALSE;
/* update entry */
itrie_update_entry((TrEntry) YAP_IntOfTerm(arg_itrie), arg_entry);
return TRUE;
}
#undef arg_itrie
#undef arg_entry
/* itrie_check_entry(-Itrie,-Entry,+Ref) */
#define arg_itrie YAP_ARG1
#define arg_entry YAP_ARG2
#define arg_ref YAP_ARG3
static int p_itrie_check_entry(void) {
TrData data;
/* check arg */
if (!YAP_IsIntTerm(arg_itrie))
return FALSE;
/* check entry */
if (!(data = itrie_check_entry((TrEntry) YAP_IntOfTerm(arg_itrie), arg_entry)))
return FALSE;
return YAP_Unify(arg_ref, YAP_MkIntTerm((YAP_Int) data));
}
#undef arg_itrie
#undef arg_entry
#undef arg_ref
/* itrie_get_entry(-Ref,+Entry) */
#define arg_ref YAP_ARG1
#define arg_entry YAP_ARG2
static int p_itrie_get_entry(void) {
YAP_Term entry;
/* check arg */
if (!YAP_IsIntTerm(arg_ref))
return FALSE;
/* get entry */
entry = itrie_get_entry((TrData) YAP_IntOfTerm(arg_ref));
return YAP_Unify(arg_entry, entry);
}
#undef arg_ref
#undef arg_entry
/* itrie_get_data(-Ref,+Data) */
#define arg_ref YAP_ARG1
#define arg_data YAP_ARG2
static int p_itrie_get_data(void) {
YAP_Term list;
YAP_Term item;
YAP_Functor f;
YAP_Int pos, neg, time;
/* check arg */
if (!YAP_IsIntTerm(arg_ref))
return FALSE;
/* get data */
itrie_get_data((TrData) YAP_IntOfTerm(arg_ref), &pos, &neg, &time);
list = YAP_MkAtomTerm(YAP_LookupAtom("[]"));
f = YAP_MkFunctor(YAP_LookupAtom("timestamp"), 1);
item = YAP_MkIntTerm(time);
item = YAP_MkApplTerm(f, 1, &item);
list = YAP_MkPairTerm(item, list);
f = YAP_MkFunctor(YAP_LookupAtom("neg"), 1);
item = YAP_MkIntTerm(neg);
item = YAP_MkApplTerm(f, 1, &item);
list = YAP_MkPairTerm(item, list);
f = YAP_MkFunctor(YAP_LookupAtom("pos"), 1);
item = YAP_MkIntTerm(pos);
item = YAP_MkApplTerm(f, 1, &item);
list = YAP_MkPairTerm(item, list);
return YAP_Unify(arg_data, list);
}
#undef arg_ref
#undef arg_data
/* itrie_traverse(-Itrie,+Ref) */
#define arg_itrie YAP_ARG1
#define arg_ref YAP_ARG2
static int p_itrie_traverse_init(void) {
TrData data;
/* check arg */
if (!YAP_IsIntTerm(arg_itrie))
return FALSE;
/* traverse itrie */
if (!(data = itrie_traverse_init((TrEntry) YAP_IntOfTerm(arg_itrie)))) {
YAP_cut_fail();
return FALSE;
}
return YAP_Unify(arg_ref, YAP_MkIntTerm((YAP_Int) data));
}
#undef arg_itrie
#undef arg_ref
/* itrie_traverse(-Itrie,+Ref) */
#define arg_itrie YAP_ARG1
#define arg_ref YAP_ARG2
static int p_itrie_traverse_cont(void) {
TrData data;
/* traverse itrie */
if (!(data = itrie_traverse_cont((TrEntry) YAP_IntOfTerm(arg_itrie)))) {
YAP_cut_fail();
return FALSE;
}
return YAP_Unify(arg_ref, YAP_MkIntTerm((YAP_Int) data));
}
#undef arg_itrie
#undef arg_ref
/* itrie_remove_entry(-Ref) */
#define arg_ref YAP_ARG1
static int p_itrie_remove_entry(void) {
/* check arg */
if (!YAP_IsIntTerm(arg_ref))
return FALSE;
/* remove entry */
itrie_remove_entry((TrData) YAP_IntOfTerm(arg_ref));
return TRUE;
}
#undef arg_ref
/* itrie_remove_subtree(-Ref) */
#define arg_ref YAP_ARG1
static int p_itrie_remove_subtree(void) {
/* check arg */
if (!YAP_IsIntTerm(arg_ref))
return FALSE;
/* remove subtree */
itrie_remove_subtree((TrData) YAP_IntOfTerm(arg_ref));
return TRUE;
}
#undef arg_ref
/* itrie_join(-ItrieDest,-ItrieSource) */
#define arg_itrie_dest YAP_ARG1
#define arg_itrie_source YAP_ARG2
static int p_itrie_join(void) {
/* check args */
if (!YAP_IsIntTerm(arg_itrie_dest))
return FALSE;
if (!YAP_IsIntTerm(arg_itrie_source))
return FALSE;
/* join itrie */
itrie_join((TrEntry) YAP_IntOfTerm(arg_itrie_dest), (TrEntry) YAP_IntOfTerm(arg_itrie_source));
return TRUE;
}
#undef arg_itrie_dest
#undef arg_itrie_source
/* itrie_add(-ItrieDest,-ItrieSource) */
#define arg_itrie_dest YAP_ARG1
#define arg_itrie_source YAP_ARG2
static int p_itrie_add(void) {
/* check args */
if (!YAP_IsIntTerm(arg_itrie_dest))
return FALSE;
if (!YAP_IsIntTerm(arg_itrie_source))
return FALSE;
/* add itrie */
itrie_add((TrEntry) YAP_IntOfTerm(arg_itrie_dest), (TrEntry) YAP_IntOfTerm(arg_itrie_source));
return TRUE;
}
#undef arg_itrie_dest
#undef arg_itrie_source
/* itrie_subtract(-ItrieDest,-ItrieSource) */
#define arg_itrie_dest YAP_ARG1
#define arg_itrie_source YAP_ARG2
static int p_itrie_subtract(void) {
/* check args */
if (!YAP_IsIntTerm(arg_itrie_dest))
return FALSE;
if (!YAP_IsIntTerm(arg_itrie_source))
return FALSE;
/* subtract itrie */
itrie_subtract((TrEntry) YAP_IntOfTerm(arg_itrie_dest), (TrEntry) YAP_IntOfTerm(arg_itrie_source));
return TRUE;
}
#undef arg_itrie_dest
#undef arg_itrie_source
/* itrie_save(-Itrie,-FileName) */
#define arg_itrie YAP_ARG1
#define arg_file YAP_ARG2
static int p_itrie_save(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 and close file */
itrie_save((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
static int p_itrie_load(void) {
TrEntry itrie;
const char *file_str;
FILE *file;
/* check args */
if (!YAP_IsVarTerm(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, "r")))
return FALSE;
/* load itrie and close file */
itrie = itrie_load(file);
if (fclose(file))
return FALSE;
return YAP_Unify(arg_itrie, YAP_MkIntTerm((YAP_Int) itrie));
}
#undef arg_itrie
#undef arg_file
/* itrie_stats(+Memory,+Tries,+Entries,+Nodes) */
#define arg_memory YAP_ARG1
#define arg_tries YAP_ARG2
#define arg_entries YAP_ARG3
#define arg_nodes YAP_ARG4
static int p_itrie_stats(void) {
YAP_Int memory, tries, entries, nodes;
/* get stats */
itrie_stats(&memory, &tries, &entries, &nodes);
if (!YAP_Unify(arg_memory, YAP_MkIntTerm(memory)))
return FALSE;
if (!YAP_Unify(arg_tries, YAP_MkIntTerm(tries)))
return FALSE;
if (!YAP_Unify(arg_entries, YAP_MkIntTerm(entries)))
return FALSE;
if (!YAP_Unify(arg_nodes, YAP_MkIntTerm(nodes)))
return FALSE;
return TRUE;
}
#undef arg_memory
#undef arg_tries
#undef arg_entries
#undef arg_nodes
/* itrie_max_stats(+Memory,+Tries,+Entries,+Nodes) */
#define arg_memory YAP_ARG1
#define arg_tries YAP_ARG2
#define arg_entries YAP_ARG3
#define arg_nodes YAP_ARG4
static int p_itrie_max_stats(void) {
YAP_Int memory, tries, entries, nodes;
/* get stats */
itrie_max_stats(&memory, &tries, &entries, &nodes);
if (!YAP_Unify(arg_memory, YAP_MkIntTerm(memory)))
return FALSE;
if (!YAP_Unify(arg_tries, YAP_MkIntTerm(tries)))
return FALSE;
if (!YAP_Unify(arg_entries, YAP_MkIntTerm(entries)))
return FALSE;
if (!YAP_Unify(arg_nodes, YAP_MkIntTerm(nodes)))
return FALSE;
return TRUE;
}
#undef arg_memory
#undef arg_tries
#undef arg_entries
#undef arg_nodes
/* itrie_usage(-Itrie,+Entries,+Nodes,+VirtualNodes) */
#define arg_itrie YAP_ARG1
#define arg_entries YAP_ARG2
#define arg_nodes YAP_ARG3
#define arg_virtualnodes YAP_ARG4
static int p_itrie_usage(void) {
YAP_Int entries, nodes, virtualnodes;
/* check arg */
if (!YAP_IsIntTerm(arg_itrie))
return FALSE;
/* get itrie usage */
itrie_usage((TrEntry) YAP_IntOfTerm(arg_itrie), &entries, &nodes, &virtualnodes);
if (!YAP_Unify(arg_entries, YAP_MkIntTerm(entries)))
return FALSE;
if (!YAP_Unify(arg_nodes, YAP_MkIntTerm(nodes)))
return FALSE;
if (!YAP_Unify(arg_virtualnodes, YAP_MkIntTerm(virtualnodes)))
return FALSE;
return TRUE;
}
#undef arg_itrie
#undef arg_entries
#undef arg_nodes
#undef arg_virtualnodes
/* itrie_print(-Itrie) */
#define arg_itrie YAP_ARG1
static int p_itrie_print(void) {
/* check arg */
if (!YAP_IsIntTerm(arg_itrie))
return FALSE;
/* print itrie */
itrie_print((TrEntry) YAP_IntOfTerm(arg_itrie));
return TRUE;
}
#undef arg_itrie

505
library/Tries/tries.c Normal file
View File

@ -0,0 +1,505 @@
/****************************************
File: tries.c
Author: Ricardo Rocha
Comments: Tries module for Yap Prolog
version: $ID$
****************************************/
/* -------------------------- */
/* Includes */
/* -------------------------- */
#include <YapInterface.h>
#include <string.h>
#include <stdio.h>
#include "base_tries.h"
/* -------------------------- */
/* Local Variables */
/* -------------------------- */
static TrEngine TRIE_ENGINE;
/* -------------------------- */
/* Procedures */
/* -------------------------- */
void init_tries(void);
static int p_trie_open(void);
static int p_trie_close(void);
static int p_trie_close_all(void);
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_remove_entry(void);
static int p_trie_remove_subtree(void);
static int p_trie_save(void);
static int p_trie_load(void);
static int p_trie_stats(void);
static int p_trie_max_stats(void);
static int p_trie_usage(void);
static int p_trie_print(void);
/* backwards compatibility */
static int p_open_trie(void);
static int p_close_trie(void);
static int p_close_all_tries(void);
static int p_put_trie_entry(void);
static int p_get_trie_entry(void);
static int p_remove_trie_entry(void);
static int p_print_trie(void);
/* -------------------------- */
/* Module Init Procedure */
/* -------------------------- */
void init_tries(void) {
TRIE_ENGINE = trie_init_module();
YAP_UserCPredicate("trie_open", p_trie_open, 1);
YAP_UserCPredicate("trie_close", p_trie_close, 1);
YAP_UserCPredicate("trie_close_all", p_trie_close_all, 0);
YAP_UserCPredicate("trie_mode", p_trie_mode, 1);
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_UserCPredicate("trie_remove_entry", p_trie_remove_entry, 1);
YAP_UserCPredicate("trie_remove_subtree", p_trie_remove_subtree, 1);
YAP_UserCPredicate("trie_save", p_trie_save, 2);
YAP_UserCPredicate("trie_load", p_trie_load, 2);
YAP_UserCPredicate("trie_usage", p_trie_usage, 4);
YAP_UserCPredicate("trie_stats", p_trie_stats, 4);
YAP_UserCPredicate("trie_max_stats", p_trie_max_stats, 4);
YAP_UserCPredicate("trie_print", p_trie_print, 1);
/* backwards compatibility */
YAP_UserCPredicate("open_trie", p_open_trie, 1);
YAP_UserCPredicate("close_trie", p_close_trie, 1);
YAP_UserCPredicate("close_all_tries", p_close_all_tries, 0);
YAP_UserCPredicate("put_trie_entry", p_put_trie_entry, 4);
YAP_UserCPredicate("get_trie_entry", p_get_trie_entry, 3);
YAP_UserCPredicate("remove_trie_entry", p_remove_trie_entry, 1);
YAP_UserCPredicate("print_trie", p_print_trie, 1);
return;
}
/* --------------------------------- */
/* Backwards Compatibility */
/* --------------------------------- */
/* open_trie(+Trie) */
static int p_open_trie(void) {
return p_trie_open();
}
/* close_trie(-Trie) */
static int p_close_trie(void) {
return p_trie_close();
}
/* close_all_tries() */
static int p_close_all_tries(void) {
return p_trie_close_all();
}
/* put_trie_entry(-Mode,-Trie,-Entry,+Ref) */
#define arg_mode YAP_ARG1
#define arg_trie YAP_ARG2
#define arg_entry YAP_ARG3
#define arg_ref YAP_ARG4
static int p_put_trie_entry(void) {
TrNode node;
const char *mode_str;
YAP_Int mode, current_mode;
/* check args */
mode_str = YAP_AtomName(YAP_AtomOfTerm(arg_mode));
if (!strcmp(mode_str, "std"))
mode = TRIE_MODE_STANDARD;
else if (!strcmp(mode_str, "rev"))
mode = TRIE_MODE_REVERSE;
else
return FALSE;
if (!YAP_IsIntTerm(arg_trie))
return FALSE;
/* 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);
trie_set_mode(current_mode);
return YAP_Unify(arg_ref, YAP_MkIntTerm((YAP_Int) node));
}
#undef arg_mode
#undef arg_trie
#undef arg_entry
#undef arg_ref
/* get_trie_entry(-Mode,-Ref,+Entry) */
#define arg_mode YAP_ARG1
#define arg_ref YAP_ARG2
#define arg_entry YAP_ARG3
static int p_get_trie_entry(void) {
YAP_Term entry;
const char *mode_str;
YAP_Int mode, current_mode;
/* check args */
mode_str = YAP_AtomName(YAP_AtomOfTerm(arg_mode));
if (!strcmp(mode_str, "std"))
mode = TRIE_MODE_STANDARD;
else if (!strcmp(mode_str, "rev"))
mode = TRIE_MODE_REVERSE;
else
return FALSE;
if (!YAP_IsIntTerm(arg_ref))
return FALSE;
/* get trie entry */
current_mode = trie_get_mode();
trie_set_mode(mode);
entry = trie_get_entry((TrNode) YAP_IntOfTerm(arg_ref));
trie_set_mode(current_mode);
return YAP_Unify(arg_entry, entry);
}
#undef arg_mode
#undef arg_ref
#undef arg_entry
/* remove_trie_entry(-Ref) */
static int p_remove_trie_entry(void) {
return p_trie_remove_entry();
}
/* print_trie(-Trie) */
static int p_print_trie(void) {
return p_trie_print();
}
/* -------------------------- */
/* Local Procedures */
/* -------------------------- */
/* trie_open(+Trie) */
#define arg_trie YAP_ARG1
static int p_trie_open(void) {
TrNode node;
/* 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));
}
#undef arg_trie
/* trie_close(-Trie) */
#define arg_trie YAP_ARG1
static int p_trie_close(void) {
/* check arg */
if (!YAP_IsIntTerm(arg_trie))
return FALSE;
/* close trie */
trie_close(TRIE_ENGINE, (TrNode) YAP_IntOfTerm(arg_trie), NULL);
return TRUE;
}
#undef arg_trie
/* trie_close_all() */
static int p_trie_close_all(void) {
trie_close_all(TRIE_ENGINE, NULL);
return TRUE;
}
/* trie_mode(?Mode) */
#define arg_mode YAP_ARG1
static int p_trie_mode(void) {
YAP_Term mode_term;
const char *mode_str;
YAP_Int mode;
/* get mode */
if (YAP_IsVarTerm(arg_mode)) {
mode = trie_get_mode();
if (mode == TRIE_MODE_STANDARD)
mode_term = YAP_MkAtomTerm(YAP_LookupAtom("std"));
else if (mode == TRIE_MODE_REVERSE)
mode_term = YAP_MkAtomTerm(YAP_LookupAtom("rev"));
else
return FALSE;
return YAP_Unify(arg_mode, mode_term);
}
/* set mode */
mode_str = YAP_AtomName(YAP_AtomOfTerm(arg_mode));
if (!strcmp(mode_str, "std"))
mode = TRIE_MODE_STANDARD;
else if (!strcmp(mode_str, "rev"))
mode = TRIE_MODE_REVERSE;
else
return FALSE;
trie_set_mode(mode);
return TRUE;
}
#undef arg_mode
/* trie_put_entry(-Trie,-Entry,+Ref) */
#define arg_trie YAP_ARG1
#define arg_entry YAP_ARG2
#define arg_ref YAP_ARG3
static int p_trie_put_entry(void) {
TrNode node;
/* 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));
}
#undef arg_trie
#undef arg_entry
#undef arg_ref
/* trie_check_entry(-Trie,-Entry,+Ref) */
#define arg_trie YAP_ARG1
#define arg_entry YAP_ARG2
#define arg_ref YAP_ARG3
static int p_trie_check_entry(void) {
TrNode node;
/* check args */
if (!YAP_IsIntTerm(arg_trie))
return FALSE;
/* check trie entry */
if (!(node = trie_check_entry((TrNode) YAP_IntOfTerm(arg_trie), arg_entry)))
return FALSE;
return YAP_Unify(arg_ref, YAP_MkIntTerm((YAP_Int) node));
}
#undef arg_trie
#undef arg_entry
#undef arg_ref
/* trie_get_entry(-Ref,+Entry) */
#define arg_ref YAP_ARG1
#define arg_entry YAP_ARG2
static int p_trie_get_entry(void) {
YAP_Term entry;
/* check args */
if (!YAP_IsIntTerm(arg_ref))
return FALSE;
/* get trie entry */
entry = trie_get_entry((TrNode) YAP_IntOfTerm(arg_ref));
return YAP_Unify(arg_entry, entry);
}
#undef arg_ref
#undef arg_entry
/* trie_remove_entry(-Ref) */
#define arg_ref YAP_ARG1
static int p_trie_remove_entry(void) {
/* check arg */
if (!YAP_IsIntTerm(arg_ref))
return FALSE;
/* remove trie entry */
trie_remove_entry(TRIE_ENGINE, (TrNode) YAP_IntOfTerm(arg_ref), NULL);
return TRUE;
}
#undef arg_ref
/* trie_remove_subtree(-Ref) */
#define arg_ref YAP_ARG1
static int p_trie_remove_subtree(void) {
/* check arg */
if (!YAP_IsIntTerm(arg_ref))
return FALSE;
/* remove trie subtree */
trie_remove_subtree(TRIE_ENGINE, (TrNode) YAP_IntOfTerm(arg_ref), NULL);
return TRUE;
}
#undef arg_ref
/* trie_save(-Trie,-FileName) */
#define arg_trie YAP_ARG1
#define arg_file YAP_ARG2
static int p_trie_save(void) {
const char *file_str;
FILE *file;
/* check args */
if (!YAP_IsIntTerm(arg_trie))
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 trie and close file */
trie_save((TrNode) YAP_IntOfTerm(arg_trie), file, NULL);
if (fclose(file))
return FALSE;
return TRUE;
}
#undef arg_trie
#undef arg_file
/* trie_load(+Trie,-FileName) */
#define arg_trie YAP_ARG1
#define arg_file YAP_ARG2
static int p_trie_load(void) {
TrNode node;
const char *file_str;
FILE *file;
/* check args */
if (!YAP_IsVarTerm(arg_trie))
return FALSE;
if (!YAP_IsAtomTerm(arg_file))
return FALSE;
/* open file */
file_str = YAP_AtomName(YAP_AtomOfTerm(arg_file));
if (!(file = fopen(file_str, "r")))
return FALSE;
/* load trie and close file */
node = trie_load(TRIE_ENGINE, file, NULL);
if (fclose(file))
return FALSE;
return YAP_Unify(arg_trie, YAP_MkIntTerm((YAP_Int) node));
}
#undef arg_trie
#undef arg_file
/* trie_stats(+Memory,+Tries,+Entries,+Nodes) */
#define arg_memory YAP_ARG1
#define arg_tries YAP_ARG2
#define arg_entries YAP_ARG3
#define arg_nodes YAP_ARG4
static int p_trie_stats(void) {
YAP_Int memory, tries, entries, nodes;
/* get stats */
trie_stats(TRIE_ENGINE, &memory, &tries, &entries, &nodes);
if (!YAP_Unify(arg_memory, YAP_MkIntTerm(memory)))
return FALSE;
if (!YAP_Unify(arg_tries, YAP_MkIntTerm(tries)))
return FALSE;
if (!YAP_Unify(arg_entries, YAP_MkIntTerm(entries)))
return FALSE;
if (!YAP_Unify(arg_nodes, YAP_MkIntTerm(nodes)))
return FALSE;
return TRUE;
}
#undef arg_memory
#undef arg_tries
#undef arg_entries
#undef arg_nodes
/* trie_max_stats(+Memory,+Tries,+Entries,+Nodes) */
#define arg_memory YAP_ARG1
#define arg_tries YAP_ARG2
#define arg_entries YAP_ARG3
#define arg_nodes YAP_ARG4
static int p_trie_max_stats(void) {
YAP_Int memory, tries, entries, nodes;
/* get stats */
trie_max_stats(TRIE_ENGINE, &memory, &tries, &entries, &nodes);
if (!YAP_Unify(arg_memory, YAP_MkIntTerm(memory)))
return FALSE;
if (!YAP_Unify(arg_tries, YAP_MkIntTerm(tries)))
return FALSE;
if (!YAP_Unify(arg_entries, YAP_MkIntTerm(entries)))
return FALSE;
if (!YAP_Unify(arg_nodes, YAP_MkIntTerm(nodes)))
return FALSE;
return TRUE;
}
#undef arg_memory
#undef arg_tries
#undef arg_entries
#undef arg_nodes
/* trie_usage(-Trie,+Entries,+Nodes,+VirtualNodes) */
#define arg_trie YAP_ARG1
#define arg_entries YAP_ARG2
#define arg_nodes YAP_ARG3
#define arg_virtualnodes YAP_ARG4
static int p_trie_usage(void) {
YAP_Int entries, nodes, virtualnodes;
/* check arg */
if (!YAP_IsIntTerm(arg_trie))
return FALSE;
/* get trie usage */
trie_usage((TrNode) 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)))
return FALSE;
if (!YAP_Unify(arg_virtualnodes, YAP_MkIntTerm(virtualnodes)))
return FALSE;
return TRUE;
}
#undef arg_trie
#undef arg_entries
#undef arg_nodes
#undef arg_virtualnodes
/* trie_print(-Trie) */
#define arg_trie YAP_ARG1
static int p_trie_print(void) {
/* check arg */
if (!YAP_IsIntTerm(arg_trie))
return FALSE;
/* print trie */
trie_print((TrNode) YAP_IntOfTerm(arg_trie), NULL);
return TRUE;
}
#undef arg_trie