2002-12-12 20:47:36 +00:00
|
|
|
/******************************************
|
|
|
|
* File: yap_tries.c *
|
|
|
|
* Author: Ricardo Rocha *
|
|
|
|
* Comments: Tries module for yap *
|
|
|
|
******************************************/
|
|
|
|
|
|
|
|
|
2003-11-05 16:24:51 +00:00
|
|
|
|
|
|
|
/* -------------------------- */
|
|
|
|
/* Includes */
|
|
|
|
/* -------------------------- */
|
|
|
|
|
|
|
|
#include <YapInterface.h>
|
|
|
|
#include <string.h>
|
2003-01-08 16:45:35 +00:00
|
|
|
#include "tries.h"
|
2003-11-05 16:24:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* -------------------------- */
|
|
|
|
/* Procedures */
|
|
|
|
/* -------------------------- */
|
2002-12-12 20:47:36 +00:00
|
|
|
|
|
|
|
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);
|
2003-11-05 16:24:51 +00:00
|
|
|
#ifdef ALLOW_REMOVE_TRIE
|
2002-12-12 20:47:36 +00:00
|
|
|
static int p_remove_trie_entry(void);
|
2003-11-05 16:24:51 +00:00
|
|
|
#endif /* ALLOW_REMOVE_TRIE */
|
2002-12-12 20:47:36 +00:00
|
|
|
static int p_trie_stats(void);
|
2003-11-05 16:24:51 +00:00
|
|
|
static int p_trie_max_stats(void);
|
|
|
|
static int p_trie_usage(void);
|
2002-12-12 20:47:36 +00:00
|
|
|
static int p_print_trie(void);
|
|
|
|
|
|
|
|
|
2003-11-05 16:24:51 +00:00
|
|
|
|
|
|
|
/* -------------------------- */
|
|
|
|
/* Module Init Procedure */
|
|
|
|
/* -------------------------- */
|
|
|
|
|
2002-12-12 20:47:36 +00:00
|
|
|
void init_tries(void) {
|
2003-11-05 16:24:51 +00:00
|
|
|
init_tries_module();
|
|
|
|
|
|
|
|
/* open_trie: -> Trie */
|
|
|
|
YAP_UserCPredicate("open_trie", p_open_trie, 1);
|
|
|
|
/* close_trie: Trie -> */
|
|
|
|
YAP_UserCPredicate("close_trie", p_close_trie, 1);
|
|
|
|
/* close_all_tries: -> */
|
|
|
|
YAP_UserCPredicate("close_all_tries", p_close_all_tries, 0);
|
|
|
|
/* put_trie_entry: Mode x Trie x Entry -> Ref */
|
|
|
|
YAP_UserCPredicate("put_trie_entry", p_put_trie_entry, 4);
|
|
|
|
/* get_trie_entry: Mode x Ref -> Entry */
|
|
|
|
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);
|
2002-12-12 20:47:36 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-05 16:24:51 +00:00
|
|
|
|
|
|
|
/* -------------------------- */
|
|
|
|
/* Local Procedures */
|
|
|
|
/* -------------------------- */
|
|
|
|
|
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;
|
2003-11-05 16:24:51 +00:00
|
|
|
int mode;
|
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")) {
|
2003-11-05 16:24:51 +00:00
|
|
|
mode = MODE_STANDARD;
|
2003-01-08 16:45:35 +00:00
|
|
|
} else if (!strcmp(mode_str, "rev")) {
|
2003-11-05 16:24:51 +00:00
|
|
|
mode = MODE_REVERSE;
|
2003-01-08 16:45:35 +00:00
|
|
|
} 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 */
|
2003-11-05 16:24:51 +00:00
|
|
|
node = put_trie_entry((TrNode) YAP_IntOfTerm(arg_trie), arg_entry, mode);
|
2003-01-08 16:45:35 +00:00
|
|
|
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;
|
2003-11-05 16:24:51 +00:00
|
|
|
int mode;
|
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")) {
|
2003-11-05 16:24:51 +00:00
|
|
|
mode = MODE_STANDARD;
|
2003-01-08 16:45:35 +00:00
|
|
|
} else if (!strcmp(mode_str, "rev")) {
|
2003-11-05 16:24:51 +00:00
|
|
|
mode = MODE_REVERSE;
|
2003-01-08 16:45:35 +00:00
|
|
|
} 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 */
|
2003-11-05 16:24:51 +00:00
|
|
|
entry = get_trie_entry((TrNode) YAP_IntOfTerm(arg_ref), mode);
|
2003-01-08 16:45:35 +00:00
|
|
|
if (!YAP_Unify(arg_entry, entry))
|
2002-12-12 20:47:36 +00:00
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-05 16:24:51 +00:00
|
|
|
#ifdef ALLOW_REMOVE_TRIE
|
2002-12-12 20:47:36 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
2003-11-05 16:24:51 +00:00
|
|
|
#endif /* ALLOW_REMOVE_TRIE */
|
2002-12-12 20:47:36 +00:00
|
|
|
|
|
|
|
|
2003-11-05 16:24:51 +00:00
|
|
|
/* trie_stats(+TrieNodes,+HashNodes,+HashBuckets,+Mem) */
|
2002-12-12 20:47:36 +00:00
|
|
|
static int p_trie_stats(void) {
|
2003-11-05 16:24:51 +00:00
|
|
|
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;
|
|
|
|
|
2002-12-12 20:47:36 +00:00
|
|
|
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;
|
|
|
|
}
|
2004-07-23 22:08:45 +01:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
int WINAPI PROTO(win_tries, (HANDLE, DWORD, LPVOID));
|
|
|
|
|
|
|
|
int WINAPI win_tries(HANDLE hinst, DWORD reason, LPVOID reserved)
|
|
|
|
{
|
|
|
|
switch (reason)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH:
|
|
|
|
break;
|
|
|
|
case DLL_PROCESS_DETACH:
|
|
|
|
break;
|
|
|
|
case DLL_THREAD_ATTACH:
|
|
|
|
break;
|
|
|
|
case DLL_THREAD_DETACH:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|