2007-05-02 16:31:23 +01:00
|
|
|
/****************************************
|
|
|
|
File: tries.c
|
|
|
|
Author: Ricardo Rocha
|
|
|
|
Comments: Tries module for Yap Prolog
|
|
|
|
version: $ID$
|
|
|
|
****************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* -------------------------- */
|
|
|
|
/* Includes */
|
|
|
|
/* -------------------------- */
|
|
|
|
|
|
|
|
#include <YapInterface.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2008-02-22 18:57:27 +00:00
|
|
|
#include "core_tries.h"
|
2007-05-02 16:31:23 +01:00
|
|
|
#include "base_tries.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* -------------------------- */
|
|
|
|
/* 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);
|
2008-02-22 18:57:27 +00:00
|
|
|
static int p_trie_traverse_init(void);
|
|
|
|
static int p_trie_traverse_cont(void);
|
2007-05-02 16:31:23 +01:00
|
|
|
static int p_trie_remove_entry(void);
|
|
|
|
static int p_trie_remove_subtree(void);
|
2008-02-11 17:00:23 +00:00
|
|
|
static int p_trie_join(void);
|
|
|
|
static int p_trie_intersect(void);
|
|
|
|
static int p_trie_count_join(void);
|
|
|
|
static int p_trie_count_intersect(void);
|
2007-05-02 16:31:23 +01:00
|
|
|
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) {
|
2008-02-22 18:57:27 +00:00
|
|
|
trie_init_module();
|
2007-05-02 16:31:23 +01:00
|
|
|
|
|
|
|
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);
|
2008-02-22 18:57:27 +00:00
|
|
|
YAP_UserBackCPredicate("trie_traverse", p_trie_traverse_init, p_trie_traverse_cont, 2, 0);
|
2007-05-02 16:31:23 +01:00
|
|
|
YAP_UserCPredicate("trie_remove_entry", p_trie_remove_entry, 1);
|
|
|
|
YAP_UserCPredicate("trie_remove_subtree", p_trie_remove_subtree, 1);
|
2008-02-11 17:00:23 +00:00
|
|
|
YAP_UserCPredicate("trie_join", p_trie_join, 2);
|
|
|
|
YAP_UserCPredicate("trie_intersect", p_trie_intersect, 2);
|
|
|
|
YAP_UserCPredicate("trie_count_join", p_trie_count_join, 3);
|
|
|
|
YAP_UserCPredicate("trie_count_intersect", p_trie_count_intersect, 3);
|
2007-05-02 16:31:23 +01:00
|
|
|
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) {
|
2008-02-22 18:57:27 +00:00
|
|
|
TrData data;
|
2007-05-02 16:31:23 +01:00
|
|
|
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);
|
2008-02-22 18:57:27 +00:00
|
|
|
data = trie_put_entry((TrEntry) YAP_IntOfTerm(arg_trie), arg_entry);
|
2007-05-02 16:31:23 +01:00
|
|
|
trie_set_mode(current_mode);
|
2008-02-22 18:57:27 +00:00
|
|
|
return YAP_Unify(arg_ref, YAP_MkIntTerm((YAP_Int) data));
|
2007-05-02 16:31:23 +01:00
|
|
|
}
|
|
|
|
#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);
|
2008-02-22 18:57:27 +00:00
|
|
|
entry = trie_get_entry((TrData) YAP_IntOfTerm(arg_ref));
|
2007-05-02 16:31:23 +01:00
|
|
|
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) {
|
2008-02-22 18:57:27 +00:00
|
|
|
TrEntry trie;
|
2007-05-02 16:31:23 +01:00
|
|
|
|
|
|
|
/* check arg */
|
|
|
|
if (!YAP_IsVarTerm(arg_trie))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* open trie */
|
2008-02-22 18:57:27 +00:00
|
|
|
trie = trie_open();
|
|
|
|
return YAP_Unify(arg_trie, YAP_MkIntTerm((YAP_Int) trie));
|
2007-05-02 16:31:23 +01:00
|
|
|
}
|
|
|
|
#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 */
|
2008-02-22 18:57:27 +00:00
|
|
|
trie_close((TrEntry) YAP_IntOfTerm(arg_trie));
|
2007-05-02 16:31:23 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#undef arg_trie
|
|
|
|
|
|
|
|
|
|
|
|
/* trie_close_all() */
|
|
|
|
static int p_trie_close_all(void) {
|
2008-02-22 18:57:27 +00:00
|
|
|
trie_close_all();
|
2007-05-02 16:31:23 +01:00
|
|
|
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) {
|
2008-02-22 18:57:27 +00:00
|
|
|
TrData data;
|
2007-05-02 16:31:23 +01:00
|
|
|
|
|
|
|
/* check args */
|
|
|
|
if (!YAP_IsIntTerm(arg_trie))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* put trie entry */
|
2008-02-22 18:57:27 +00:00
|
|
|
data = trie_put_entry((TrEntry) YAP_IntOfTerm(arg_trie), arg_entry);
|
|
|
|
return YAP_Unify(arg_ref, YAP_MkIntTerm((YAP_Int) data));
|
2007-05-02 16:31:23 +01:00
|
|
|
}
|
|
|
|
#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) {
|
2008-02-22 18:57:27 +00:00
|
|
|
TrData data;
|
2007-05-02 16:31:23 +01:00
|
|
|
|
|
|
|
/* check args */
|
|
|
|
if (!YAP_IsIntTerm(arg_trie))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* check trie entry */
|
2008-02-22 18:57:27 +00:00
|
|
|
if (!(data = trie_check_entry((TrEntry) YAP_IntOfTerm(arg_trie), arg_entry)))
|
2007-05-02 16:31:23 +01:00
|
|
|
return FALSE;
|
2008-02-22 18:57:27 +00:00
|
|
|
return YAP_Unify(arg_ref, YAP_MkIntTerm((YAP_Int) data));
|
2007-05-02 16:31:23 +01:00
|
|
|
}
|
|
|
|
#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 */
|
2008-02-22 18:57:27 +00:00
|
|
|
entry = trie_get_entry((TrData) YAP_IntOfTerm(arg_ref));
|
2007-05-02 16:31:23 +01:00
|
|
|
return YAP_Unify(arg_entry, entry);
|
|
|
|
}
|
|
|
|
#undef arg_ref
|
|
|
|
#undef arg_entry
|
|
|
|
|
|
|
|
|
2008-02-22 18:57:27 +00:00
|
|
|
/* trie_traverse(-Trie,+Ref) */
|
|
|
|
#define arg_trie YAP_ARG1
|
|
|
|
#define arg_ref YAP_ARG2
|
|
|
|
static int p_trie_traverse_init(void) {
|
|
|
|
TrData data;
|
|
|
|
|
|
|
|
/* check arg */
|
|
|
|
if (!YAP_IsIntTerm(arg_trie))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* traverse trie */
|
|
|
|
if (!(data = trie_traverse_init((TrEntry) YAP_IntOfTerm(arg_trie)))) {
|
|
|
|
YAP_cut_fail();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return YAP_Unify(arg_ref, YAP_MkIntTerm((YAP_Int) data));
|
|
|
|
}
|
|
|
|
#undef arg_trie
|
|
|
|
#undef arg_ref
|
|
|
|
|
|
|
|
|
|
|
|
/* trie_traverse(-Trie,+Ref) */
|
|
|
|
#define arg_trie YAP_ARG1
|
|
|
|
#define arg_ref YAP_ARG2
|
|
|
|
static int p_trie_traverse_cont(void) {
|
|
|
|
TrData data;
|
|
|
|
|
|
|
|
/* traverse trie */
|
|
|
|
if (!(data = trie_traverse_cont((TrEntry) YAP_IntOfTerm(arg_trie)))) {
|
|
|
|
YAP_cut_fail();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return YAP_Unify(arg_ref, YAP_MkIntTerm((YAP_Int) data));
|
|
|
|
}
|
|
|
|
#undef arg_trie
|
|
|
|
#undef arg_ref
|
|
|
|
|
|
|
|
|
2007-05-02 16:31:23 +01:00
|
|
|
/* 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 */
|
2008-02-22 18:57:27 +00:00
|
|
|
trie_remove_entry((TrData) YAP_IntOfTerm(arg_ref));
|
2007-05-02 16:31:23 +01:00
|
|
|
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 */
|
2008-02-22 18:57:27 +00:00
|
|
|
trie_remove_subtree((TrData) YAP_IntOfTerm(arg_ref));
|
2007-05-02 16:31:23 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#undef arg_ref
|
|
|
|
|
|
|
|
|
2008-02-11 17:00:23 +00:00
|
|
|
/* trie_join(-TrieDest,-TrieSource) */
|
|
|
|
#define arg_trie_dest YAP_ARG1
|
|
|
|
#define arg_trie_source YAP_ARG2
|
|
|
|
static int p_trie_join(void) {
|
|
|
|
/* check args */
|
|
|
|
if (!YAP_IsIntTerm(arg_trie_dest))
|
|
|
|
return FALSE;
|
|
|
|
if (!YAP_IsIntTerm(arg_trie_source))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* join trie */
|
2008-02-22 18:57:27 +00:00
|
|
|
trie_join((TrEntry) YAP_IntOfTerm(arg_trie_dest), (TrEntry) YAP_IntOfTerm(arg_trie_source));
|
2008-02-11 17:00:23 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#undef arg_trie_dest
|
|
|
|
#undef arg_trie_source
|
|
|
|
|
|
|
|
|
|
|
|
/* trie_intersect(-TrieDest,-TrieSource) */
|
|
|
|
#define arg_trie_dest YAP_ARG1
|
|
|
|
#define arg_trie_source YAP_ARG2
|
|
|
|
static int p_trie_intersect(void) {
|
|
|
|
/* check args */
|
|
|
|
if (!YAP_IsIntTerm(arg_trie_dest))
|
|
|
|
return FALSE;
|
|
|
|
if (!YAP_IsIntTerm(arg_trie_source))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* intersect trie */
|
2008-02-22 18:57:27 +00:00
|
|
|
trie_intersect((TrEntry) YAP_IntOfTerm(arg_trie_dest), (TrEntry) YAP_IntOfTerm(arg_trie_source));
|
2008-02-11 17:00:23 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#undef arg_trie_dest
|
|
|
|
#undef arg_trie_source
|
|
|
|
|
|
|
|
|
|
|
|
/* trie_count_join(-Trie1,-Trie2,+Entries) */
|
|
|
|
#define arg_trie1 YAP_ARG1
|
|
|
|
#define arg_trie2 YAP_ARG2
|
|
|
|
#define arg_entries YAP_ARG3
|
|
|
|
static int p_trie_count_join(void) {
|
|
|
|
YAP_Int entries;
|
|
|
|
|
|
|
|
/* check args */
|
|
|
|
if (!YAP_IsIntTerm(arg_trie1))
|
|
|
|
return FALSE;
|
|
|
|
if (!YAP_IsIntTerm(arg_trie2))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* count join trie */
|
2008-02-22 18:57:27 +00:00
|
|
|
entries = trie_count_join((TrEntry) YAP_IntOfTerm(arg_trie1), (TrEntry) YAP_IntOfTerm(arg_trie2));
|
2008-02-11 17:00:23 +00:00
|
|
|
return YAP_Unify(arg_entries, YAP_MkIntTerm(entries));
|
|
|
|
}
|
|
|
|
#undef arg_trie1
|
|
|
|
#undef arg_trie2
|
|
|
|
#undef arg_entries
|
|
|
|
|
|
|
|
|
|
|
|
/* trie_count_intersect(-Trie1,-Trie2,+Entries) */
|
|
|
|
#define arg_trie1 YAP_ARG1
|
|
|
|
#define arg_trie2 YAP_ARG2
|
|
|
|
#define arg_entries YAP_ARG3
|
|
|
|
static int p_trie_count_intersect(void) {
|
|
|
|
YAP_Int entries;
|
|
|
|
|
|
|
|
/* check args */
|
|
|
|
if (!YAP_IsIntTerm(arg_trie1))
|
|
|
|
return FALSE;
|
|
|
|
if (!YAP_IsIntTerm(arg_trie2))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* count intersect trie */
|
2008-02-22 18:57:27 +00:00
|
|
|
entries = trie_count_intersect((TrEntry) YAP_IntOfTerm(arg_trie1), (TrEntry) YAP_IntOfTerm(arg_trie2));
|
2008-02-11 17:00:23 +00:00
|
|
|
return YAP_Unify(arg_entries, YAP_MkIntTerm(entries));
|
|
|
|
}
|
|
|
|
#undef arg_trie1
|
|
|
|
#undef arg_trie2
|
|
|
|
#undef arg_entries
|
|
|
|
|
|
|
|
|
2007-05-02 16:31:23 +01:00
|
|
|
/* 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 */
|
2008-02-22 18:57:27 +00:00
|
|
|
trie_save((TrEntry) YAP_IntOfTerm(arg_trie), file);
|
2007-05-02 16:31:23 +01:00
|
|
|
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) {
|
2008-02-22 18:57:27 +00:00
|
|
|
TrEntry data;
|
2007-05-02 16:31:23 +01:00
|
|
|
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 */
|
2008-02-22 18:57:27 +00:00
|
|
|
data = trie_load(file);
|
2007-05-02 16:31:23 +01:00
|
|
|
if (fclose(file))
|
|
|
|
return FALSE;
|
2008-05-07 15:21:39 +01:00
|
|
|
if (!data)
|
|
|
|
return FALSE;
|
2008-02-22 18:57:27 +00:00
|
|
|
return YAP_Unify(arg_trie, YAP_MkIntTerm((YAP_Int) data));
|
2007-05-02 16:31:23 +01:00
|
|
|
}
|
|
|
|
#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 */
|
2008-02-22 18:57:27 +00:00
|
|
|
trie_stats(&memory, &tries, &entries, &nodes);
|
2007-05-02 16:31:23 +01:00
|
|
|
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 */
|
2008-02-22 18:57:27 +00:00
|
|
|
trie_max_stats(&memory, &tries, &entries, &nodes);
|
2007-05-02 16:31:23 +01:00
|
|
|
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 */
|
2008-02-22 18:57:27 +00:00
|
|
|
trie_usage((TrEntry) YAP_IntOfTerm(arg_trie), &entries, &nodes, &virtualnodes);
|
2007-05-02 16:31:23 +01:00
|
|
|
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 */
|
2008-02-22 18:57:27 +00:00
|
|
|
trie_print((TrEntry) YAP_IntOfTerm(arg_trie));
|
2007-05-02 16:31:23 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#undef arg_trie
|