Nuno Fonseca's trie writing updates.

This commit is contained in:
Vítor Manuel de Morais Santos Costa 2009-11-03 14:37:12 +00:00
parent 7fddfbc189
commit 995b82cc6e
3 changed files with 56 additions and 4 deletions

View File

@ -31,7 +31,10 @@
itrie_stats/4,
itrie_max_stats/4,
itrie_usage/4,
itrie_print/1
itrie_print/1,
%added by nf
itrie_save2stream/2,
itrie_loadFromstream/2
]).
:- load_foreign_files([itries], [], init_itries).

View File

@ -428,8 +428,11 @@ inline
TrNode core_trie_load(TrEngine engine, FILE *file, void (*load_function)(TrNode, YAP_Int, FILE *)) {
TrNode node;
char version[15];
fpos_t curpos;
fscanf(file, "%14s", version);
if (fgetpos(file, &curpos) ) return NULL;
if (!strcmp(version, "BEGIN_TRIE_v2")) {
fseek(file, -11, SEEK_END);
fscanf(file, "%s", version);
@ -439,7 +442,7 @@ TrNode core_trie_load(TrEngine engine, FILE *file, void (*load_function)(TrNode,
fprintf(stderr, "******************************************\n");
return NULL;
}
fseek(file, 13, SEEK_SET);
if (fsetpos(file, &curpos) ) return NULL;
CURRENT_LOAD_VERSION = 2;
} else if (!strcmp(version, "BEGIN_TRIE")) {
fseek(file, -8, SEEK_END);
@ -450,7 +453,7 @@ TrNode core_trie_load(TrEngine engine, FILE *file, void (*load_function)(TrNode,
fprintf(stderr, "******************************************\n");
return NULL;
}
fseek(file, 10, SEEK_SET);
if (fsetpos(file, &curpos) ) return NULL;
CURRENT_LOAD_VERSION = 1;
} else {
fprintf(stderr, "****************************************\n");

View File

@ -51,7 +51,9 @@ static int p_itrie_stats(void);
static int p_itrie_max_stats(void);
static int p_itrie_usage(void);
static int p_itrie_print(void);
//nf
static int p_itrie_loadFromStream(void);
static int p_itrie_save2stream(void);
/* -------------------------- */
@ -87,6 +89,9 @@ void init_itries(void) {
YAP_UserCPredicate("itrie_max_stats", p_itrie_max_stats, 4);
YAP_UserCPredicate("itrie_usage", p_itrie_usage, 4);
YAP_UserCPredicate("itrie_print", p_itrie_print, 1);
// nf
YAP_UserCPredicate("itrie_save2stream", p_itrie_save2stream, 2);
YAP_UserCPredicate("itrie_loadFromstream", p_itrie_loadFromStream, 2);
return;
}
@ -679,3 +684,44 @@ static int p_itrie_print(void) {
return TRUE;
}
#undef arg_itrie
/* added by nf: itrie_save2stream(+Itrie,+Stream) */
#define arg_itrie YAP_ARG1
#define arg_stream YAP_ARG2
static int p_itrie_save2stream(void) {
FILE *file;
/* check args */
if (!YAP_IsIntTerm(arg_itrie))
return FALSE;
if ((file=(FILE*)YAP_FileDescriptorFromStream(arg_stream))==NULL)
return FALSE;
/* save itrie and close file */
itrie_save((TrEntry) YAP_IntOfTerm(arg_itrie), file);
return TRUE;
}
#undef arg_itrie
#undef arg_stream
/* added by nf: itrie_loadFromStream(-Itrie,+Stream) */
#define arg_itrie YAP_ARG1
#define arg_stream YAP_ARG2
static int p_itrie_loadFromStream(void) {
TrEntry itrie;
FILE *file;
/* check args */
if (!YAP_IsVarTerm(arg_itrie))
return FALSE;
if (!(file=(FILE*)Yap_FileDescriptorFromStream(arg_stream)))
return FALSE;
/* load itrie and close file */
itrie = itrie_load(file);
if (!itrie)
return FALSE;
return YAP_Unify(arg_itrie, YAP_MkIntTerm((YAP_Int) itrie));
}
#undef arg_itrie
#undef arg_stream