My first attempt to synchronize OPTYap with CVS

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@917 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
ricroc 2003-11-05 16:24:51 +00:00
parent 80be9121af
commit f8f7421416
2 changed files with 197 additions and 67 deletions

View File

@ -4,10 +4,21 @@
* Comments: Tries module for yap * * Comments: Tries module for yap *
******************************************/ ******************************************/
/* --------------------------- */ /* --------------------------- */
/* Defines */ /* Defines */
/* --------------------------- */ /* --------------------------- */
/* WARNING: the following macros need Yap tag scheme from file 'Tags_32LowTag' */
#define ApplTag 1
#define PairInitTag 3
#define PairEndTag 19 /* 0x13 */
#define CommaInitTag 35 /* 0x23 */
#define CommaEndTag 51 /* 0x33 */
#define FloatInitTag 67 /* 0x43 */
#define FloatEndTag 83 /* 0x53 */
#define TERM_STACK_SIZE 1000 #define TERM_STACK_SIZE 1000
#define MODE_STANDARD 0 #define MODE_STANDARD 0
#define MODE_REVERSE 1 #define MODE_REVERSE 1
@ -18,41 +29,53 @@
/* Structs */ /* Structs */
/* --------------------------- */ /* --------------------------- */
struct trie_stats { struct global_trie_stats {
long memory_in_use; int memory_in_use;
long memory_max_used; int memory_max_used;
long nodes_in_use; int nodes_in_use;
long nodes_max_used; int nodes_max_used;
long hashes_in_use; int hashes_in_use;
long hashes_max_used; int hashes_max_used;
long buckets_in_use; int buckets_in_use;
long buckets_max_used; int buckets_max_used;
} STATS; };
#define MEMORY_IN_USE (STATS.memory_in_use) #define MEMORY_IN_USE (GLOBAL_STATS.memory_in_use)
#define MEMORY_MAX_USED (STATS.memory_max_used) #define MEMORY_MAX_USED (GLOBAL_STATS.memory_max_used)
#define NODES_IN_USE (STATS.nodes_in_use) #define NODES_IN_USE (GLOBAL_STATS.nodes_in_use)
#define NODES_MAX_USED (STATS.nodes_max_used) #define NODES_MAX_USED (GLOBAL_STATS.nodes_max_used)
#define HASHES_IN_USE (STATS.hashes_in_use) #define HASHES_IN_USE (GLOBAL_STATS.hashes_in_use)
#define HASHES_MAX_USED (STATS.hashes_max_used) #define HASHES_MAX_USED (GLOBAL_STATS.hashes_max_used)
#define BUCKETS_IN_USE (STATS.buckets_in_use) #define BUCKETS_IN_USE (GLOBAL_STATS.buckets_in_use)
#define BUCKETS_MAX_USED (STATS.buckets_max_used) #define BUCKETS_MAX_USED (GLOBAL_STATS.buckets_max_used)
struct local_trie_stats{
int entries;
int nodes;
int virtual_nodes;
};
#define TRIE_ENTRIES (LOCAL_STATS.entries)
#define TRIE_NODES (LOCAL_STATS.nodes)
#define TRIE_VIRTUAL_NODES (LOCAL_STATS.virtual_nodes)
typedef struct trie_node { typedef struct trie_node {
YAP_Term entry; YAP_Term entry;
int hits;
struct trie_node *parent; struct trie_node *parent;
struct trie_node *child; struct trie_node *child;
struct trie_node *next; struct trie_node *next;
#ifdef ALLOW_REMOVE_TRIE
struct trie_node *previous; struct trie_node *previous;
int hits;
#endif /* ALLOW_REMOVE_TRIE */
} *TrNode; } *TrNode;
#define TrNode_entry(X) ((X)->entry) #define TrNode_entry(X) ((X)->entry)
#define TrNode_hits(X) ((X)->hits)
#define TrNode_parent(X) ((X)->parent) #define TrNode_parent(X) ((X)->parent)
#define TrNode_child(X) ((X)->child) #define TrNode_child(X) ((X)->child)
#define TrNode_next(X) ((X)->next) #define TrNode_next(X) ((X)->next)
#define TrNode_previous(X) ((X)->previous) #define TrNode_previous(X) ((X)->previous)
#define TrNode_hits(X) ((X)->hits)
typedef struct trie_hash { typedef struct trie_hash {
YAP_Term entry; /* for compatibility with the trie_node data structure */ YAP_Term entry; /* for compatibility with the trie_node data structure */
@ -104,12 +127,14 @@ typedef struct trie_hash {
#define PUSH_DOWN(STACK, ITEM, STACK_TOP) \ #define PUSH_DOWN(STACK, ITEM, STACK_TOP) \
{ if (STACK > STACK_TOP) \ { if (STACK > STACK_TOP) \
fprintf(stderr, "\nTries module: TERM_STACK full"); \ fprintf(stderr, "\nTries module: TERM_STACK full"); \
*STACK++ = (YAP_Term)(ITEM); \ *STACK = (YAP_Term)(ITEM); \
STACK++; \
} }
#define PUSH_UP(STACK, ITEM, STACK_TOP) \ #define PUSH_UP(STACK, ITEM, STACK_TOP) \
{ if (STACK < STACK_TOP) \ { if (STACK < STACK_TOP) \
fprintf(stderr, "\nTries module: TERM_STACK full"); \ fprintf(stderr, "\nTries module: TERM_STACK full"); \
*STACK-- = (YAP_Term)(ITEM); \ *STACK = (YAP_Term)(ITEM); \
STACK--; \
} }
@ -128,14 +153,23 @@ typedef struct trie_hash {
free_struct(STR); \ free_struct(STR); \
STATS_hash_dec() STATS_hash_dec()
#ifdef ALLOW_REMOVE_TRIE
#define TrNode_allow_remove_trie(TR_NODE, PREVIOUS) \
TrNode_previous(TR_NODE) = PREVIOUS; \
TrNode_hits(TR_NODE) = 0
#else
#define TrNode_allow_remove_trie(TR_NODE, PREVIOUS)
#endif /* ALLOW_REMOVE_TRIE */
#define new_trie_node(TR_NODE, ENTRY, PARENT, CHILD, NEXT, PREVIOUS) \ #define new_trie_node(TR_NODE, ENTRY, PARENT, CHILD, NEXT, PREVIOUS) \
new_struct(TR_NODE, TYPE_TR_NODE, SIZEOF_TR_NODE); \ new_struct(TR_NODE, TYPE_TR_NODE, SIZEOF_TR_NODE); \
TrNode_entry(TR_NODE) = ENTRY; \ TrNode_entry(TR_NODE) = ENTRY; \
TrNode_hits(TR_NODE) = 0; \
TrNode_parent(TR_NODE) = PARENT; \ TrNode_parent(TR_NODE) = PARENT; \
TrNode_child(TR_NODE) = CHILD; \ TrNode_child(TR_NODE) = CHILD; \
TrNode_next(TR_NODE) = NEXT; \ TrNode_next(TR_NODE) = NEXT; \
TrNode_previous(TR_NODE) = PREVIOUS; \ TrNode_allow_remove_trie(TR_NODE, PREVIOUS); \
STATS_node_inc() STATS_node_inc()
#define new_hash_buckets(TR_HASH, NUM_BUCKETS) \ #define new_hash_buckets(TR_HASH, NUM_BUCKETS) \
{ int i; void **ptr; \ { int i; void **ptr; \
@ -195,16 +229,14 @@ typedef struct trie_hash {
/* API */ /* API */
/* --------------------------- */ /* --------------------------- */
extern int MODE; void init_tries_module(void);
extern TrNode TRIES;
extern TrHash HASHES;
extern YAP_Functor FunctorComma;
TrNode open_trie(void); TrNode open_trie(void);
void close_trie(TrNode node); void close_trie(TrNode node);
void close_all_tries(void); void close_all_tries(void);
TrNode put_trie_entry(TrNode node, YAP_Term entry); TrNode put_trie_entry(TrNode node, YAP_Term entry, int mode);
YAP_Term get_trie_entry(TrNode node); YAP_Term get_trie_entry(TrNode node, int mode);
void remove_trie_entry(TrNode node); void remove_trie_entry(TrNode node);
void trie_stats(void); void trie_stats(int *nodes, int *hashes, int *buckets, int *memory);
void trie_max_stats(int *nodes, int *hashes, int *buckets, int *memory);
void trie_usage(TrNode node, int *entries, int *nodes, int *virtual_nodes);
void print_trie(TrNode node); void print_trie(TrNode node);

View File

@ -4,15 +4,21 @@
* Comments: Tries module for yap * * Comments: Tries module for yap *
******************************************/ ******************************************/
#include "config.h"
#include "YapInterface.h"
#include <stdio.h>
#if HAVE_STRING_H
#include <string.h>
#endif
/* -------------------------- */
/* Includes */
/* -------------------------- */
#include <YapInterface.h>
#include <string.h>
#include "tries.h" #include "tries.h"
#include "tries.c"
/* -------------------------- */
/* Procedures */
/* -------------------------- */
void init_tries(void); void init_tries(void);
static int p_open_trie(void); static int p_open_trie(void);
@ -20,38 +26,55 @@ static int p_close_trie(void);
static int p_close_all_tries(void); static int p_close_all_tries(void);
static int p_put_trie_entry(void); static int p_put_trie_entry(void);
static int p_get_trie_entry(void); static int p_get_trie_entry(void);
#ifdef ALLOW_REMOVE_TRIE
static int p_remove_trie_entry(void); static int p_remove_trie_entry(void);
#endif /* ALLOW_REMOVE_TRIE */
static int p_trie_stats(void); static int p_trie_stats(void);
static int p_trie_max_stats(void);
static int p_trie_usage(void);
static int p_print_trie(void); static int p_print_trie(void);
/* -------------------------- */
/* Module Init Procedure */
/* -------------------------- */
void init_tries(void) { void init_tries(void) {
TRIES = NULL; init_tries_module();
HASHES = NULL;
MEMORY_IN_USE = 0;
MEMORY_MAX_USED = 0;
NODES_IN_USE = 0;
NODES_MAX_USED = 0;
HASHES_IN_USE = 0;
HASHES_MAX_USED = 0;
BUCKETS_IN_USE = 0;
BUCKETS_MAX_USED = 0;
FunctorComma = YAP_MkFunctor(YAP_LookupAtom(","), 2); /* open_trie: -> Trie */
YAP_UserCPredicate("open_trie", p_open_trie, 1);
YAP_UserCPredicate("open_trie", p_open_trie, 1); /* -> Trie */ /* close_trie: Trie -> */
YAP_UserCPredicate("close_trie", p_close_trie, 1); /* Trie -> */ YAP_UserCPredicate("close_trie", p_close_trie, 1);
YAP_UserCPredicate("close_all_tries", p_close_all_tries, 0); /* -> */ /* close_all_tries: -> */
YAP_UserCPredicate("put_trie_entry", p_put_trie_entry, 4); /* Mode x Trie x Entry -> Ref */ YAP_UserCPredicate("close_all_tries", p_close_all_tries, 0);
YAP_UserCPredicate("get_trie_entry", p_get_trie_entry, 3); /* Mode x Ref -> Entry */ /* put_trie_entry: Mode x Trie x Entry -> Ref */
YAP_UserCPredicate("remove_trie_entry", p_remove_trie_entry, 1); /* Ref -> */ YAP_UserCPredicate("put_trie_entry", p_put_trie_entry, 4);
YAP_UserCPredicate("trie_statistics", p_trie_stats, 0); /* -> */ /* get_trie_entry: Mode x Ref -> Entry */
YAP_UserCPredicate("print_trie", p_print_trie, 1); /* Trie -> */ YAP_UserCPredicate("get_trie_entry", p_get_trie_entry, 3);
#ifdef ALLOW_REMOVE_TRIE
/* remove_trie_entry: Ref -> */
YAP_UserCPredicate("remove_trie_entry", p_remove_trie_entry, 1);
#endif /* ALLOW_REMOVE_TRIE */
/* trie_stats: -> TrieNodes x HashNodes x HashBuckets x Mem */
YAP_UserCPredicate("trie_stats", p_trie_stats, 4);
/* trie_max_stats: -> TrieNodes x HashNodes x HashBuckets x Mem */
YAP_UserCPredicate("trie_max_stats", p_trie_max_stats, 4);
/* trie_usage: Trie -> Entries x Nodes x VirtualNodes */
YAP_UserCPredicate("trie_usage", p_trie_usage, 4);
/* print_trie: Trie -> */
YAP_UserCPredicate("print_trie", p_print_trie, 1);
return; return;
} }
/* -------------------------- */
/* Local Procedures */
/* -------------------------- */
/* open_trie(+Trie) */ /* open_trie(+Trie) */
static int p_open_trie(void) { static int p_open_trie(void) {
YAP_Term arg_trie = YAP_ARG1; YAP_Term arg_trie = YAP_ARG1;
@ -98,20 +121,21 @@ static int p_put_trie_entry(void) {
YAP_Term arg_ref = YAP_ARG4; YAP_Term arg_ref = YAP_ARG4;
TrNode node; TrNode node;
char *mode_str; char *mode_str;
int mode;
/* check args */ /* check args */
mode_str = YAP_AtomName(YAP_AtomOfTerm(arg_mode)); mode_str = YAP_AtomName(YAP_AtomOfTerm(arg_mode));
if (!strcmp(mode_str, "std")) { if (!strcmp(mode_str, "std")) {
MODE = MODE_STANDARD; mode = MODE_STANDARD;
} else if (!strcmp(mode_str, "rev")) { } else if (!strcmp(mode_str, "rev")) {
MODE = MODE_REVERSE; mode = MODE_REVERSE;
} else } else
return FALSE; return FALSE;
if (!YAP_IsIntTerm(arg_trie)) if (!YAP_IsIntTerm(arg_trie))
return FALSE; return FALSE;
/* put trie entry */ /* put trie entry */
node = put_trie_entry((TrNode) YAP_IntOfTerm(arg_trie), arg_entry); node = put_trie_entry((TrNode) YAP_IntOfTerm(arg_trie), arg_entry, mode);
if (!YAP_Unify(arg_ref, YAP_MkIntTerm((int) node))) if (!YAP_Unify(arg_ref, YAP_MkIntTerm((int) node)))
return FALSE; return FALSE;
return TRUE; return TRUE;
@ -125,26 +149,28 @@ static int p_get_trie_entry(void) {
YAP_Term arg_entry = YAP_ARG3; YAP_Term arg_entry = YAP_ARG3;
YAP_Term entry; YAP_Term entry;
char *mode_str; char *mode_str;
int mode;
/* check args */ /* check args */
mode_str = YAP_AtomName(YAP_AtomOfTerm(arg_mode)); mode_str = YAP_AtomName(YAP_AtomOfTerm(arg_mode));
if (!strcmp(mode_str, "std")) { if (!strcmp(mode_str, "std")) {
MODE = MODE_STANDARD; mode = MODE_STANDARD;
} else if (!strcmp(mode_str, "rev")) { } else if (!strcmp(mode_str, "rev")) {
MODE = MODE_REVERSE; mode = MODE_REVERSE;
} else } else
return FALSE; return FALSE;
if (!YAP_IsIntTerm(arg_ref)) if (!YAP_IsIntTerm(arg_ref))
return FALSE; return FALSE;
/* get trie entry */ /* get trie entry */
entry = get_trie_entry((TrNode) YAP_IntOfTerm(arg_ref)); entry = get_trie_entry((TrNode) YAP_IntOfTerm(arg_ref), mode);
if (!YAP_Unify(arg_entry, entry)) if (!YAP_Unify(arg_entry, entry))
return FALSE; return FALSE;
return TRUE; return TRUE;
} }
#ifdef ALLOW_REMOVE_TRIE
/* remove_trie_entry(-Ref) */ /* remove_trie_entry(-Ref) */
static int p_remove_trie_entry(void) { static int p_remove_trie_entry(void) {
YAP_Term arg_ref = YAP_ARG1; YAP_Term arg_ref = YAP_ARG1;
@ -157,11 +183,83 @@ static int p_remove_trie_entry(void) {
remove_trie_entry((TrNode) YAP_IntOfTerm(arg_ref)); remove_trie_entry((TrNode) YAP_IntOfTerm(arg_ref));
return TRUE; return TRUE;
} }
#endif /* ALLOW_REMOVE_TRIE */
/* trie_statistics() */ /* trie_stats(+TrieNodes,+HashNodes,+HashBuckets,+Mem) */
static int p_trie_stats(void) { static int p_trie_stats(void) {
trie_stats(); YAP_Term arg_trienodes = YAP_ARG1;
YAP_Term arg_hashnodes = YAP_ARG2;
YAP_Term arg_hashbuckets = YAP_ARG3;
YAP_Term arg_mem = YAP_ARG4;
int trienodes;
int hashnodes;
int hashbuckets;
int mem;
trie_stats(&trienodes, &hashnodes, &hashbuckets, &mem);
/* all args should be arguments */
if (!YAP_Unify(arg_trienodes, YAP_MkIntTerm(trienodes)))
return FALSE;
if (!YAP_Unify(arg_hashnodes, YAP_MkIntTerm(hashnodes)))
return FALSE;
if (!YAP_Unify(arg_hashbuckets, YAP_MkIntTerm(hashbuckets)))
return FALSE;
if (!YAP_Unify(arg_mem, YAP_MkIntTerm(mem)))
return FALSE;
return TRUE;
}
/* trie_max_stats(+TrieNodes,+HashNodes,+HashBuckets,+Mem) */
static int p_trie_max_stats(void) {
YAP_Term arg_trienodes = YAP_ARG1;
YAP_Term arg_hashnodes = YAP_ARG2;
YAP_Term arg_hashbuckets = YAP_ARG3;
YAP_Term arg_mem = YAP_ARG4;
int trienodes;
int hashnodes;
int hashbuckets;
int mem;
trie_max_stats(&trienodes, &hashnodes, &hashbuckets, &mem);
/* all args should be arguments */
if (!YAP_Unify(arg_trienodes, YAP_MkIntTerm(trienodes)))
return FALSE;
if (!YAP_Unify(arg_hashnodes, YAP_MkIntTerm(hashnodes)))
return FALSE;
if (!YAP_Unify(arg_hashbuckets, YAP_MkIntTerm(hashbuckets)))
return FALSE;
if (!YAP_Unify(arg_mem, YAP_MkIntTerm(mem)))
return FALSE;
return TRUE;
}
/* trie_usage(-Trie,+Entries,+Nodes,+VirtualNodes) */
static int p_trie_usage(void) {
YAP_Term arg_trie = YAP_ARG1;
YAP_Term arg_entries = YAP_ARG2;
YAP_Term arg_nodes = YAP_ARG3;
YAP_Term arg_virtualnodes = YAP_ARG4;
int entries;
int nodes;
int virtualnodes;
/* check arg */
if (!YAP_IsIntTerm(arg_trie))
return FALSE;
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; return TRUE;
} }