new scanner.
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@736 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
@@ -4,9 +4,19 @@
|
||||
* Comments: Tries module for yap *
|
||||
******************************************/
|
||||
|
||||
/* ----------------------------- */
|
||||
/* Structs and Defines */
|
||||
/* ----------------------------- */
|
||||
/* --------------------------- */
|
||||
/* Defines */
|
||||
/* --------------------------- */
|
||||
|
||||
#define TERM_STACK_SIZE 1000
|
||||
#define MODE_STANDARD 0
|
||||
#define MODE_REVERSE 1
|
||||
|
||||
|
||||
|
||||
/* --------------------------- */
|
||||
/* Structs */
|
||||
/* --------------------------- */
|
||||
|
||||
struct trie_stats {
|
||||
long memory_in_use;
|
||||
@@ -19,6 +29,15 @@ struct trie_stats {
|
||||
long buckets_max_used;
|
||||
} STATS;
|
||||
|
||||
#define MEMORY_IN_USE (STATS.memory_in_use)
|
||||
#define MEMORY_MAX_USED (STATS.memory_max_used)
|
||||
#define NODES_IN_USE (STATS.nodes_in_use)
|
||||
#define NODES_MAX_USED (STATS.nodes_max_used)
|
||||
#define HASHES_IN_USE (STATS.hashes_in_use)
|
||||
#define HASHES_MAX_USED (STATS.hashes_max_used)
|
||||
#define BUCKETS_IN_USE (STATS.buckets_in_use)
|
||||
#define BUCKETS_MAX_USED (STATS.buckets_max_used)
|
||||
|
||||
typedef struct trie_node {
|
||||
YAP_Term entry;
|
||||
int hits;
|
||||
@@ -28,6 +47,13 @@ typedef struct trie_node {
|
||||
struct trie_node *previous;
|
||||
} *TrNode;
|
||||
|
||||
#define TrNode_entry(X) ((X)->entry)
|
||||
#define TrNode_hits(X) ((X)->hits)
|
||||
#define TrNode_parent(X) ((X)->parent)
|
||||
#define TrNode_child(X) ((X)->child)
|
||||
#define TrNode_next(X) ((X)->next)
|
||||
#define TrNode_previous(X) ((X)->previous)
|
||||
|
||||
typedef struct trie_hash {
|
||||
YAP_Term entry; /* for compatibility with the trie_node data structure */
|
||||
int number_of_buckets;
|
||||
@@ -37,22 +63,6 @@ typedef struct trie_hash {
|
||||
struct trie_hash *previous;
|
||||
} *TrHash;
|
||||
|
||||
#define MEMORY_IN_USE (STATS.memory_in_use)
|
||||
#define MEMORY_MAX_USED (STATS.memory_max_used)
|
||||
#define NODES_IN_USE (STATS.nodes_in_use)
|
||||
#define NODES_MAX_USED (STATS.nodes_max_used)
|
||||
#define HASHES_IN_USE (STATS.hashes_in_use)
|
||||
#define HASHES_MAX_USED (STATS.hashes_max_used)
|
||||
#define BUCKETS_IN_USE (STATS.buckets_in_use)
|
||||
#define BUCKETS_MAX_USED (STATS.buckets_max_used)
|
||||
|
||||
#define TrNode_entry(X) ((X)->entry)
|
||||
#define TrNode_hits(X) ((X)->hits)
|
||||
#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 TrHash_mark(X) ((X)->entry)
|
||||
#define TrHash_num_buckets(X) ((X)->number_of_buckets)
|
||||
#define TrHash_seed(X) ((X)->number_of_buckets - 1)
|
||||
@@ -62,16 +72,20 @@ typedef struct trie_hash {
|
||||
#define TrHash_next(X) ((X)->next)
|
||||
#define TrHash_previous(X) ((X)->previous)
|
||||
|
||||
#define TYPE_TR_NODE struct trie_node
|
||||
#define TYPE_TR_HASH struct trie_hash
|
||||
#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 TYPE_TR_NODE struct trie_node
|
||||
#define TYPE_TR_HASH struct trie_hash
|
||||
#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(ADDRESS) (TrNode)((int)(ADDRESS) - sizeof(YAP_Term) - sizeof(int) - 2 * sizeof(struct trie_node *))
|
||||
#define AS_TR_HASH_NEXT(ADDRESS) (TrHash)((int)(ADDRESS) - sizeof(YAP_Term) - 2 * sizeof(int) - sizeof(struct trie_node **))
|
||||
|
||||
#define TERM_STACK_SIZE 1000
|
||||
|
||||
|
||||
/* --------------------------- */
|
||||
/* Macros */
|
||||
/* --------------------------- */
|
||||
|
||||
#define MkTrieVar(INDEX) ((INDEX) << 4)
|
||||
#define TrieVarIndex(TERM) ((TERM) >> 4)
|
||||
@@ -90,12 +104,12 @@ typedef struct trie_hash {
|
||||
#define PUSH_DOWN(STACK, ITEM, STACK_TOP) \
|
||||
{ if (STACK > STACK_TOP) \
|
||||
fprintf(stderr, "\nTries module: TERM_STACK full"); \
|
||||
*STACK++ = (YAP_Term)(ITEM); \
|
||||
*STACK++ = (YAP_Term)(ITEM); \
|
||||
}
|
||||
#define PUSH_UP(STACK, ITEM, STACK_TOP) \
|
||||
{ if (STACK < STACK_TOP) \
|
||||
fprintf(stderr, "\nTries module: TERM_STACK full"); \
|
||||
*STACK-- = (YAP_Term)(ITEM); \
|
||||
*STACK-- = (YAP_Term)(ITEM); \
|
||||
}
|
||||
|
||||
|
||||
@@ -103,7 +117,7 @@ typedef struct trie_hash {
|
||||
#define new_struct(STR, STR_TYPE, STR_SIZE) \
|
||||
STR = (STR_TYPE *) YAP_AllocSpaceFromYap(STR_SIZE)
|
||||
#define free_struct(STR) \
|
||||
YAP_FreeSpaceFromYap((void *) (STR))
|
||||
YAP_FreeSpaceFromYap((char *) (STR))
|
||||
#define free_trie_node(STR) \
|
||||
free_struct(STR); \
|
||||
STATS_node_dec()
|
||||
@@ -177,18 +191,20 @@ typedef struct trie_hash {
|
||||
|
||||
|
||||
|
||||
/* ------------- */
|
||||
/* API */
|
||||
/* ------------- */
|
||||
/* --------------------------- */
|
||||
/* API */
|
||||
/* --------------------------- */
|
||||
|
||||
extern int MODE;
|
||||
extern TrNode TRIES;
|
||||
extern TrHash HASHES;
|
||||
extern YAP_Functor FunctorComma;
|
||||
|
||||
TrNode open_trie(void);
|
||||
void close_trie(TrNode node);
|
||||
void close_all_tries(void);
|
||||
TrNode put_trie_entry(TrNode node, YAP_Term entry);
|
||||
YAP_Term get_trie_entry(TrNode node);
|
||||
void remove_trie_entry(TrNode node);
|
||||
void trie_stats(void);
|
||||
void print_trie(TrNode node);
|
||||
TrNode open_trie(void);
|
||||
void close_trie(TrNode node);
|
||||
void close_all_tries(void);
|
||||
TrNode put_trie_entry(TrNode node, YAP_Term entry);
|
||||
YAP_Term get_trie_entry(TrNode node);
|
||||
void remove_trie_entry(TrNode node);
|
||||
void trie_stats(void);
|
||||
void print_trie(TrNode node);
|
||||
|
||||
@@ -6,13 +6,12 @@
|
||||
|
||||
#include "config.h"
|
||||
#include "YapInterface.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#if HAVE_STRING_H
|
||||
#include <string.h>
|
||||
#endif
|
||||
#include "tries.h"
|
||||
|
||||
#include "tries.h"
|
||||
#include "tries.c"
|
||||
|
||||
void init_tries(void);
|
||||
@@ -38,89 +37,109 @@ void init_tries(void) {
|
||||
BUCKETS_IN_USE = 0;
|
||||
BUCKETS_MAX_USED = 0;
|
||||
|
||||
YAP_UserCPredicate("open_trie", p_open_trie, 1); /* -> Ref */
|
||||
YAP_UserCPredicate("close_trie", p_close_trie, 1); /* Ref -> */
|
||||
YAP_UserCPredicate("close_all_tries", p_close_all_tries, 0); /* -> */
|
||||
YAP_UserCPredicate("put_trie_entry", p_put_trie_entry, 3); /* Ref x Entry -> Ref */
|
||||
YAP_UserCPredicate("get_trie_entry", p_get_trie_entry, 2); /* Ref -> Entry */
|
||||
YAP_UserCPredicate("remove_trie_entry", p_remove_trie_entry, 1); /* Ref -> */
|
||||
YAP_UserCPredicate("trie_statistics", p_trie_stats, 0); /* -> */
|
||||
YAP_UserCPredicate("print_trie", p_print_trie, 1); /* Ref -> */
|
||||
FunctorComma = YAP_MkFunctor(YAP_LookupAtom(","), 2);
|
||||
|
||||
YAP_UserCPredicate("open_trie", p_open_trie, 1); /* -> Trie */
|
||||
YAP_UserCPredicate("close_trie", p_close_trie, 1); /* Trie -> */
|
||||
YAP_UserCPredicate("close_all_tries", p_close_all_tries, 0); /* -> */
|
||||
YAP_UserCPredicate("put_trie_entry", p_put_trie_entry, 4); /* Mode x Trie x Entry -> Ref */
|
||||
YAP_UserCPredicate("get_trie_entry", p_get_trie_entry, 3); /* Mode x Ref -> Entry */
|
||||
YAP_UserCPredicate("remove_trie_entry", p_remove_trie_entry, 1); /* Ref -> */
|
||||
YAP_UserCPredicate("trie_statistics", p_trie_stats, 0); /* -> */
|
||||
YAP_UserCPredicate("print_trie", p_print_trie, 1); /* Trie -> */
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/* open_trie(+Ref) */
|
||||
/* open_trie(+Trie) */
|
||||
static int p_open_trie(void) {
|
||||
YAP_Term arg1 = YAP_ARG1;
|
||||
YAP_Term arg_trie = YAP_ARG1;
|
||||
TrNode node;
|
||||
|
||||
/* check arg */
|
||||
if (!YAP_IsVarTerm(arg1))
|
||||
if (!YAP_IsVarTerm(arg_trie))
|
||||
return FALSE;
|
||||
|
||||
/* open trie */
|
||||
node = open_trie();
|
||||
/* return node reference */
|
||||
if (!YAP_Unify(arg1, YAP_MkIntTerm((int) node)))
|
||||
if (!YAP_Unify(arg_trie, YAP_MkIntTerm((int) node)))
|
||||
return FALSE;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
/* close_trie(-Ref) */
|
||||
/* close_trie(-Trie) */
|
||||
static int p_close_trie(void) {
|
||||
YAP_Term arg1 = YAP_ARG1;
|
||||
YAP_Term arg_trie = YAP_ARG1;
|
||||
|
||||
/* check args */
|
||||
if (!YAP_IsIntTerm(arg1))
|
||||
if (!YAP_IsIntTerm(arg_trie))
|
||||
return FALSE;
|
||||
/* free trie */
|
||||
close_trie((TrNode) YAP_IntOfTerm(arg1));
|
||||
|
||||
/* close trie */
|
||||
close_trie((TrNode) YAP_IntOfTerm(arg_trie));
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
/* close_all_tries() */
|
||||
static int p_close_all_tries(void) {
|
||||
/* close all tries */
|
||||
close_all_tries();
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
/* put_trie_entry(-Ref,-Entry,+Ref) */
|
||||
/* put_trie_entry(-Mode,-Trie,-Entry,+Ref) */
|
||||
static int p_put_trie_entry(void) {
|
||||
YAP_Term arg1 = YAP_ARG1;
|
||||
YAP_Term arg2 = YAP_ARG2;
|
||||
YAP_Term arg3 = YAP_ARG3;
|
||||
YAP_Term arg_mode = YAP_ARG1;
|
||||
YAP_Term arg_trie = YAP_ARG2;
|
||||
YAP_Term arg_entry = YAP_ARG3;
|
||||
YAP_Term arg_ref = YAP_ARG4;
|
||||
TrNode node;
|
||||
char *mode_str;
|
||||
|
||||
/* check args */
|
||||
if (!YAP_IsIntTerm(arg1))
|
||||
mode_str = YAP_AtomName(YAP_AtomOfTerm(arg_mode));
|
||||
if (!strcmp(mode_str, "std")) {
|
||||
MODE = MODE_STANDARD;
|
||||
} else if (!strcmp(mode_str, "rev")) {
|
||||
MODE = MODE_REVERSE;
|
||||
} else
|
||||
return FALSE;
|
||||
/* put entry */
|
||||
node = put_trie_entry((TrNode) YAP_IntOfTerm(arg1), arg2);
|
||||
/* return node reference */
|
||||
if (!YAP_Unify(arg3, YAP_MkIntTerm((int) node)))
|
||||
if (!YAP_IsIntTerm(arg_trie))
|
||||
return FALSE;
|
||||
|
||||
/* put trie entry */
|
||||
node = put_trie_entry((TrNode) YAP_IntOfTerm(arg_trie), arg_entry);
|
||||
if (!YAP_Unify(arg_ref, YAP_MkIntTerm((int) node)))
|
||||
return FALSE;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
/* get_trie_entry(-Ref,+Entry) */
|
||||
/* get_trie_entry(-Mode,-Ref,+Entry) */
|
||||
static int p_get_trie_entry(void) {
|
||||
YAP_Term arg1 = YAP_ARG1;
|
||||
YAP_Term arg2 = YAP_ARG2;
|
||||
YAP_Term arg_mode = YAP_ARG1;
|
||||
YAP_Term arg_ref = YAP_ARG2;
|
||||
YAP_Term arg_entry = YAP_ARG3;
|
||||
YAP_Term entry;
|
||||
char *mode_str;
|
||||
|
||||
/* check args */
|
||||
if (!YAP_IsIntTerm(arg1))
|
||||
mode_str = YAP_AtomName(YAP_AtomOfTerm(arg_mode));
|
||||
if (!strcmp(mode_str, "std")) {
|
||||
MODE = MODE_STANDARD;
|
||||
} else if (!strcmp(mode_str, "rev")) {
|
||||
MODE = MODE_REVERSE;
|
||||
} else
|
||||
return FALSE;
|
||||
/* get entry */
|
||||
entry = get_trie_entry((TrNode) YAP_IntOfTerm(arg1));
|
||||
/* return entry reference */
|
||||
if (!YAP_Unify(arg2, entry))
|
||||
if (!YAP_IsIntTerm(arg_ref))
|
||||
return FALSE;
|
||||
|
||||
/* get trie entry */
|
||||
entry = get_trie_entry((TrNode) YAP_IntOfTerm(arg_ref));
|
||||
if (!YAP_Unify(arg_entry, entry))
|
||||
return FALSE;
|
||||
return TRUE;
|
||||
}
|
||||
@@ -128,33 +147,34 @@ static int p_get_trie_entry(void) {
|
||||
|
||||
/* remove_trie_entry(-Ref) */
|
||||
static int p_remove_trie_entry(void) {
|
||||
YAP_Term arg1 = YAP_ARG1;
|
||||
YAP_Term arg_ref = YAP_ARG1;
|
||||
|
||||
/* check arg */
|
||||
if (!YAP_IsIntTerm(arg1))
|
||||
if (!YAP_IsIntTerm(arg_ref))
|
||||
return FALSE;
|
||||
|
||||
/* remove trie entry */
|
||||
remove_trie_entry((TrNode) YAP_IntOfTerm(arg1));
|
||||
remove_trie_entry((TrNode) YAP_IntOfTerm(arg_ref));
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
/* trie_statistics() */
|
||||
static int p_trie_stats(void) {
|
||||
/* print trie statistics */
|
||||
trie_stats();
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
/* print_trie(-Ref) */
|
||||
/* print_trie(-Trie) */
|
||||
static int p_print_trie(void) {
|
||||
YAP_Term arg1 = YAP_ARG1;
|
||||
YAP_Term arg_trie = YAP_ARG1;
|
||||
|
||||
/* check arg */
|
||||
if (!YAP_IsIntTerm(arg1))
|
||||
if (!YAP_IsIntTerm(arg_trie))
|
||||
return FALSE;
|
||||
|
||||
/* print trie */
|
||||
print_trie((TrNode) YAP_IntOfTerm(arg1));
|
||||
print_trie((TrNode) YAP_IntOfTerm(arg_trie));
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user