2002-12-12 20:47:36 +00:00
|
|
|
/******************************************
|
|
|
|
* File: yap_tries.c *
|
|
|
|
* Author: Ricardo Rocha *
|
|
|
|
* Comments: Tries module for yap *
|
|
|
|
******************************************/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "YapInterface.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#if HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
2003-01-08 16:45:35 +00:00
|
|
|
#include "tries.h"
|
2002-12-12 20:47:36 +00:00
|
|
|
#include "tries.c"
|
|
|
|
|
|
|
|
void init_tries(void);
|
|
|
|
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_trie_stats(void);
|
|
|
|
static int p_print_trie(void);
|
|
|
|
|
|
|
|
|
|
|
|
void init_tries(void) {
|
|
|
|
TRIES = NULL;
|
|
|
|
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;
|
|
|
|
|
2003-01-08 16:45:35 +00:00
|
|
|
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 -> */
|
2002-12-12 20:47:36 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-08 16:45:35 +00:00
|
|
|
/* open_trie(+Trie) */
|
2002-12-12 20:47:36 +00:00
|
|
|
static int p_open_trie(void) {
|
2003-01-08 16:45:35 +00:00
|
|
|
YAP_Term arg_trie = YAP_ARG1;
|
2002-12-12 20:47:36 +00:00
|
|
|
TrNode node;
|
|
|
|
|
|
|
|
/* check arg */
|
2003-01-08 16:45:35 +00:00
|
|
|
if (!YAP_IsVarTerm(arg_trie))
|
2002-12-12 20:47:36 +00:00
|
|
|
return FALSE;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
2002-12-12 20:47:36 +00:00
|
|
|
/* open trie */
|
|
|
|
node = open_trie();
|
2003-01-08 16:45:35 +00:00
|
|
|
if (!YAP_Unify(arg_trie, YAP_MkIntTerm((int) node)))
|
2002-12-12 20:47:36 +00:00
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-08 16:45:35 +00:00
|
|
|
/* close_trie(-Trie) */
|
2002-12-12 20:47:36 +00:00
|
|
|
static int p_close_trie(void) {
|
2003-01-08 16:45:35 +00:00
|
|
|
YAP_Term arg_trie = YAP_ARG1;
|
2002-12-12 20:47:36 +00:00
|
|
|
|
|
|
|
/* check args */
|
2003-01-08 16:45:35 +00:00
|
|
|
if (!YAP_IsIntTerm(arg_trie))
|
2002-12-12 20:47:36 +00:00
|
|
|
return FALSE;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
|
|
|
/* close trie */
|
|
|
|
close_trie((TrNode) YAP_IntOfTerm(arg_trie));
|
2002-12-12 20:47:36 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* close_all_tries() */
|
|
|
|
static int p_close_all_tries(void) {
|
|
|
|
close_all_tries();
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-08 16:45:35 +00:00
|
|
|
/* put_trie_entry(-Mode,-Trie,-Entry,+Ref) */
|
2002-12-12 20:47:36 +00:00
|
|
|
static int p_put_trie_entry(void) {
|
2003-01-08 16:45:35 +00:00
|
|
|
YAP_Term arg_mode = YAP_ARG1;
|
|
|
|
YAP_Term arg_trie = YAP_ARG2;
|
|
|
|
YAP_Term arg_entry = YAP_ARG3;
|
|
|
|
YAP_Term arg_ref = YAP_ARG4;
|
2002-12-12 20:47:36 +00:00
|
|
|
TrNode node;
|
2003-01-08 16:45:35 +00:00
|
|
|
char *mode_str;
|
2002-12-12 20:47:36 +00:00
|
|
|
|
|
|
|
/* check args */
|
2003-01-08 16:45:35 +00:00
|
|
|
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
|
2002-12-12 20:47:36 +00:00
|
|
|
return FALSE;
|
2003-01-08 16:45:35 +00:00
|
|
|
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)))
|
2002-12-12 20:47:36 +00:00
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-08 16:45:35 +00:00
|
|
|
/* get_trie_entry(-Mode,-Ref,+Entry) */
|
2002-12-12 20:47:36 +00:00
|
|
|
static int p_get_trie_entry(void) {
|
2003-01-08 16:45:35 +00:00
|
|
|
YAP_Term arg_mode = YAP_ARG1;
|
|
|
|
YAP_Term arg_ref = YAP_ARG2;
|
|
|
|
YAP_Term arg_entry = YAP_ARG3;
|
2002-12-12 20:47:36 +00:00
|
|
|
YAP_Term entry;
|
2003-01-08 16:45:35 +00:00
|
|
|
char *mode_str;
|
2002-12-12 20:47:36 +00:00
|
|
|
|
|
|
|
/* check args */
|
2003-01-08 16:45:35 +00:00
|
|
|
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;
|
|
|
|
if (!YAP_IsIntTerm(arg_ref))
|
2002-12-12 20:47:36 +00:00
|
|
|
return FALSE;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
|
|
|
/* get trie entry */
|
|
|
|
entry = get_trie_entry((TrNode) YAP_IntOfTerm(arg_ref));
|
|
|
|
if (!YAP_Unify(arg_entry, entry))
|
2002-12-12 20:47:36 +00:00
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* remove_trie_entry(-Ref) */
|
|
|
|
static int p_remove_trie_entry(void) {
|
2003-01-08 16:45:35 +00:00
|
|
|
YAP_Term arg_ref = YAP_ARG1;
|
2002-12-12 20:47:36 +00:00
|
|
|
|
|
|
|
/* check arg */
|
2003-01-08 16:45:35 +00:00
|
|
|
if (!YAP_IsIntTerm(arg_ref))
|
2002-12-12 20:47:36 +00:00
|
|
|
return FALSE;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
2002-12-12 20:47:36 +00:00
|
|
|
/* remove trie entry */
|
2003-01-08 16:45:35 +00:00
|
|
|
remove_trie_entry((TrNode) YAP_IntOfTerm(arg_ref));
|
2002-12-12 20:47:36 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* trie_statistics() */
|
|
|
|
static int p_trie_stats(void) {
|
|
|
|
trie_stats();
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-08 16:45:35 +00:00
|
|
|
/* print_trie(-Trie) */
|
2002-12-12 20:47:36 +00:00
|
|
|
static int p_print_trie(void) {
|
2003-01-08 16:45:35 +00:00
|
|
|
YAP_Term arg_trie = YAP_ARG1;
|
2002-12-12 20:47:36 +00:00
|
|
|
|
|
|
|
/* check arg */
|
2003-01-08 16:45:35 +00:00
|
|
|
if (!YAP_IsIntTerm(arg_trie))
|
2002-12-12 20:47:36 +00:00
|
|
|
return FALSE;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
2002-12-12 20:47:36 +00:00
|
|
|
/* print trie */
|
2003-01-08 16:45:35 +00:00
|
|
|
print_trie((TrNode) YAP_IntOfTerm(arg_trie));
|
2002-12-12 20:47:36 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|