This commit is contained in:
Vitor Santos Costa 2018-11-04 10:55:58 +00:00
parent a1f8631844
commit 76cfa609c2
27 changed files with 4500 additions and 4863 deletions

File diff suppressed because it is too large Load Diff

View File

@ -12,8 +12,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
extern "C"{ extern "C" {
#include "config.h" #include "YapConfig.h"
} }
#if HAVE_GMPXX_H #if HAVE_GMPXX_H
@ -41,17 +41,14 @@ extern "C"{
extern "C" { extern "C" {
#include <stdlib.h> #include <stdlib.h>
// Bad export from Python // Bad export from Python
#include <config.h> #include <YapConfig.h>
#include <stddef.h> #include <stddef.h>
#if YAP_PYTHON #if YAP_PYTHON
#include <Python.h> #include <Python.h>
@ -102,22 +99,19 @@ X_API extern void YAP_UserCPredicate(const char *, YAP_UserCPred,
X_API extern void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred, X_API extern void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred,
YAP_Arity, YAP_Term); YAP_Arity, YAP_Term);
X_API extern void YAP_UserBackCPredicate(const char *name, X_API extern void YAP_UserBackCPredicate(const char *name, YAP_UserCPred init,
YAP_UserCPred init, YAP_UserCPred cont, YAP_Arity arity,
YAP_UserCPred cont, YAP_Arity extra);
YAP_Arity arity, YAP_Arity extra);
X_API extern void YAP_UserBackCutCPredicate(const char *name, X_API extern void YAP_UserBackCutCPredicate(const char *name,
YAP_UserCPred init, YAP_UserCPred init,
YAP_UserCPred cont, YAP_UserCPred cont,
YAP_UserCPred cut, YAP_UserCPred cut, YAP_Arity arity,
YAP_Arity arity, YAP_Arity extra); YAP_Arity extra);
X_API extern YAP_Term YAP_ReadBuffer(const char *s, YAP_Term *tp); X_API extern YAP_Term YAP_ReadBuffer(const char *s, YAP_Term *tp);
extern YAP_Term YAP_MkcharPTerm(char *s); extern YAP_Term YAP_MkcharPTerm(char *s);
} }
class YAPEngine; class YAPEngine;
@ -130,7 +124,6 @@ class YAPModule;
class YAPError; class YAPError;
class YAPPredicate; class YAPPredicate;
#include "yapa.hh" #include "yapa.hh"
#include "yapie.hh" #include "yapie.hh"

View File

@ -1,41 +1,40 @@
#include "config.h" #include "YapConfig.h"
#include "udi.h" #include "udi.h"
#include "utarray.h" #include "utarray.h"
#include "uthash.h" #include "uthash.h"
/* Argument Indexing */ /* Argument Indexing */
struct udi_p_args { struct udi_p_args {
int arg; //indexed arg int arg; // indexed arg
void *idxstr; //user indexing structure void *idxstr; // user indexing structure
UdiControlBlock control; //user indexing structure functions UdiControlBlock control; // user indexing structure functions
}; };
typedef struct udi_p_args *UdiPArg; typedef struct udi_p_args *UdiPArg;
UT_icd arg_icd = {sizeof(struct udi_p_args), NULL, NULL, NULL }; UT_icd arg_icd = {sizeof(struct udi_p_args), NULL, NULL, NULL};
/* clauselist */ /* clauselist */
UT_icd cl_icd = {sizeof(yamop *), NULL, NULL, NULL }; UT_icd cl_icd = {sizeof(yamop *), NULL, NULL, NULL};
/* /*
* All the info we need to enter user indexed code * All the info we need to enter user indexed code
* stored in a uthash * stored in a uthash
*/ */
struct udi_info struct udi_info {
{ PredEntry *p; // predicate (need to identify asserts)
PredEntry *p; //predicate (need to identify asserts) UT_array *clauselist; // clause list used on returns
UT_array *clauselist; //clause list used on returns UT_array *args; // indexed args
UT_array *args; //indexed args UT_hash_handle hh; // uthash handle
UT_hash_handle hh; //uthash handle
}; };
typedef struct udi_info *UdiInfo; typedef struct udi_info *UdiInfo;
/* to ease code for a UdiInfo hash table*/ /* to ease code for a UdiInfo hash table*/
#define HASH_FIND_UdiInfo(head,find,out) \ #define HASH_FIND_UdiInfo(head, find, out) \
HASH_FIND(hh,head,find,sizeof(PredEntry),out) HASH_FIND(hh, head, find, sizeof(PredEntry), out)
#define HASH_ADD_UdiInfo(head,p,add) \ #define HASH_ADD_UdiInfo(head, p, add) \
HASH_ADD_KEYPTR(hh,head,p,sizeof(PredEntry *),add) HASH_ADD_KEYPTR(hh, head, p, sizeof(PredEntry *), add)
/* used during init */ /* used during init */
static YAP_Int p_new_udi( USES_REGS1 ); static YAP_Int p_new_udi(USES_REGS1);
static YAP_Int p_udi_args_init(Term spec, int arity, UdiInfo blk); static YAP_Int p_udi_args_init(Term spec, int arity, UdiInfo blk);
/* /*
@ -44,18 +43,15 @@ static YAP_Int p_udi_args_init(Term spec, int arity, UdiInfo blk);
/* single indexing helpers (no intersection needed just create clauselist) */ /* single indexing helpers (no intersection needed just create clauselist) */
#include "clause_list.h" #include "clause_list.h"
struct si_callback_h struct si_callback_h {
{
clause_list_t cl; clause_list_t cl;
UT_array *clauselist; UT_array *clauselist;
void * pred; void *pred;
}; };
typedef struct si_callback_h * si_callback_h_t; typedef struct si_callback_h *si_callback_h_t;
static inline int si_callback(void *key, void *data, void *arg) static inline int si_callback(void *key, void *data, void *arg) {
{ si_callback_h_t c = (si_callback_h_t)arg;
si_callback_h_t c = (si_callback_h_t) arg; yamop **cl = (yamop **)utarray_eltptr(c->clauselist, ((YAP_Int)data) - 1);
yamop **cl = (yamop **) utarray_eltptr(c->clauselist, ((YAP_Int) data) - 1);
return Yap_ClauseListExtend(c->cl, *cl, c->pred); return Yap_ClauseListExtend(c->cl, *cl, c->pred);
} }

View File

@ -1,23 +1,21 @@
/************************************************************************* /*************************************************************************
* * * *
* Yap Prolog * * Yap Prolog *
* * * *
* Yap Prolog Was Developed At Nccup - Universidade Do Porto * * Yap Prolog Was Developed At Nccup - Universidade Do Porto *
* * * *
* Copyright L.Damas, V.S.Costa And Universidade Do Porto 1985-1997 * * Copyright L.Damas, V.S.Costa And Universidade Do Porto 1985-1997 *
* * * *
************************************************************************** **************************************************************************
* * * *
* File: Yap.C * * File: Yap.C * Last Rev:
* Last Rev: * ** Mods: * Comments: Yap's Main File *
* Mods: * * *
* Comments: Yap's Main File * *************************************************************************/
* *
*************************************************************************/
/* static char SccsId[] = "X 4.3.3"; */ /* static char SccsId[] = "X 4.3.3"; */
#include "YapConfig.h"
#include "YapInterface.h" #include "YapInterface.h"
#include "config.h"
#include "cut_c.h" #include "cut_c.h"
@ -112,9 +110,8 @@ static bool exec_top_level(int BootMode, YAP_init_args *iap) {
livegoal = YAP_FullLookupAtom("live"); livegoal = YAP_FullLookupAtom("live");
} }
return true; return true;
//YAP_Exit(EXIT_SUCCESS); // YAP_Exit(EXIT_SUCCESS);
}
}
// FILE *debugf; // FILE *debugf;

View File

@ -19,7 +19,7 @@ extern "C" {
//=== includes =============================================================== //=== includes ===============================================================
#ifdef YAP_KERNEL #ifdef YAP_KERNEL
#include "config.h" #include "YapConfig.h"
#ifdef __cplusplus #ifdef __cplusplus
} }
@ -40,8 +40,8 @@ extern "C" {
#include "YapInterface.h" #include "YapInterface.h"
#else #else
#if _YAP_NOT_INSTALLED_ #if _YAP_NOT_INSTALLED_
#include <YapConfig.h>
#include <YapInterface.h> #include <YapInterface.h>
#include <config.h>
#else #else
#include <Yap/YapInterface.h> #include <Yap/YapInterface.h>
#endif #endif

View File

@ -31,6 +31,8 @@ The following routines export the YAP internals and architecture.
#define _yap_c_interface_h 1 #define _yap_c_interface_h 1
#include "YapConfig.h"
#define __YAP_PROLOG__ 1 #define __YAP_PROLOG__ 1
#ifndef YAPVERSION #ifndef YAPVERSION

View File

@ -13,8 +13,8 @@
* * * *
*************************************************************************/ *************************************************************************/
#include "YapConfig.h"
#include "YapInterface.h" #include "YapInterface.h"
#include "config.h"
#include <math.h> #include <math.h>
#if defined(__MINGW32__) || _MSC_VER #if defined(__MINGW32__) || _MSC_VER
#include <windows.h> #include <windows.h>
@ -1281,7 +1281,7 @@ static YAP_Bool matrix_sum(void) {
y; // Alas, sum is big, y small, so low-order digits of y are lost. y; // Alas, sum is big, y small, so low-order digits of y are lost.
c = (t - sum) - y; // (t - sum) cancels the high-order part of y; c = (t - sum) - y; // (t - sum) cancels the high-order part of y;
// subtracting y recovers negative (low part of y) // subtracting y recovers negative (low part of y)
sum = t; // Algebraically, c should always be zero. Beware sum = t; // Algebraically, c should always be zero. Beware
// overly-aggressive optimizing compilers! // overly-aggressive optimizing compilers!
} }
tf = YAP_MkFloatTerm(sum); tf = YAP_MkFloatTerm(sum);

View File

@ -1,67 +1,56 @@
/************************************************************************* /*************************************************************************
* * * *
* YAP Prolog * * YAP Prolog *
* * * *
* Yap Prolog was developed at NCCUP - Universidade do Porto * * Yap Prolog was developed at NCCUP - Universidade do Porto *
* * * *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 * * Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
* * * *
************************************************************************** **************************************************************************
* * * *
* File: random.c * * File: random.c * Last rev:
* Last rev: * ** mods: * comments: regular expression interpreter *
* mods: * * *
* comments: regular expression interpreter * *************************************************************************/
* *
*************************************************************************/
#include "config.h"
#include "YapInterface.h" #include "YapInterface.h"
#include <math.h> #include <math.h>
#if defined(__MINGW32__) || _MSC_VER #if defined(__MINGW32__) || _MSC_VER
#include <windows.h> #include <windows.h>
#endif #endif
X_API void init_random( void ); X_API void init_random(void);
static short a1 = 27314, b1 = 9213, c1 = 17773; static short a1 = 27314, b1 = 9213, c1 = 17773;
static YAP_Bool static YAP_Bool p_random(void) {
p_random(void)
{
double fli; double fli;
long int t1, t2, t3; long int t1, t2, t3;
t1 = (a1 * 171) % 30269; t1 = (a1 * 171) % 30269;
t2 = (b1 * 172) % 30307; t2 = (b1 * 172) % 30307;
t3 = (c1 * 170) % 30323; t3 = (c1 * 170) % 30323;
fli = (t1/30269.0) + (t2/30307.0) + (t3/30323.0); fli = (t1 / 30269.0) + (t2 / 30307.0) + (t3 / 30323.0);
a1 = t1; a1 = t1;
b1 = t2; b1 = t2;
c1 = t3; c1 = t3;
return(YAP_Unify(YAP_ARG1, YAP_MkFloatTerm(fli-(int)(fli)))); return (YAP_Unify(YAP_ARG1, YAP_MkFloatTerm(fli - (int)(fli))));
} }
static YAP_Bool static YAP_Bool p_setrand(void) {
p_setrand(void)
{
a1 = YAP_IntOfTerm(YAP_ARG1); a1 = YAP_IntOfTerm(YAP_ARG1);
b1 = YAP_IntOfTerm(YAP_ARG2); b1 = YAP_IntOfTerm(YAP_ARG2);
c1 = YAP_IntOfTerm(YAP_ARG3); c1 = YAP_IntOfTerm(YAP_ARG3);
return(TRUE); return (TRUE);
} }
static YAP_Bool static YAP_Bool p_getrand(void) {
p_getrand(void) return (YAP_Unify(YAP_ARG1, YAP_MkIntTerm(a1)) &&
{ YAP_Unify(YAP_ARG2, YAP_MkIntTerm(b1)) &&
return(YAP_Unify(YAP_ARG1,YAP_MkIntTerm(a1)) && YAP_Unify(YAP_ARG3, YAP_MkIntTerm(c1)));
YAP_Unify(YAP_ARG2,YAP_MkIntTerm(b1)) &&
YAP_Unify(YAP_ARG3,YAP_MkIntTerm(c1)));
} }
X_API void X_API void init_random(void) {
init_random(void)
{
YAP_UserCPredicate("random", p_random, 1); YAP_UserCPredicate("random", p_random, 1);
YAP_UserCPredicate("setrand", p_setrand, 3); YAP_UserCPredicate("setrand", p_setrand, 3);
YAP_UserCPredicate("getrand", p_getrand, 3); YAP_UserCPredicate("getrand", p_getrand, 3);
@ -71,19 +60,17 @@ init_random(void)
int WINAPI win_random(HANDLE, DWORD, LPVOID); int WINAPI win_random(HANDLE, DWORD, LPVOID);
int WINAPI win_random(HANDLE hinst, DWORD reason, LPVOID reserved) int WINAPI win_random(HANDLE hinst, DWORD reason, LPVOID reserved) {
{ switch (reason) {
switch (reason) case DLL_PROCESS_ATTACH:
{ break;
case DLL_PROCESS_ATTACH: case DLL_PROCESS_DETACH:
break; break;
case DLL_PROCESS_DETACH: case DLL_THREAD_ATTACH:
break; break;
case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH:
break; break;
case DLL_THREAD_DETACH: }
break;
}
return 1; return 1;
} }
#endif #endif

View File

@ -50,7 +50,7 @@ static char sccsid[] = "@(#)regcomp.c 8.5 (Berkeley) 3/20/94";
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include "YapInterface.h" #include "YapInterface.h"
#include "config.h"
#if HAVE_SYS_TYPES_H #if HAVE_SYS_TYPES_H
#include <sys/types.h> #include <sys/types.h>
#endif #endif

View File

@ -48,11 +48,10 @@ static char sccsid[] = "@(#)regexec.c 8.3 (Berkeley) 3/20/94";
* macros that code uses. This lets the same code operate on two different * macros that code uses. This lets the same code operate on two different
* representations for state sets. * representations for state sets.
*/ */
#include "config.h" #include "YapInterface.h"
#ifndef HAVE_REGEXEC #ifndef HAVE_REGEXEC
#include "YapInterface.h"
#if HAVE_SYS_TYPES_H #if HAVE_SYS_TYPES_H
#include <sys/types.h> #include <sys/types.h>
#endif #endif
@ -68,8 +67,8 @@ static char sccsid[] = "@(#)regexec.c 8.3 (Berkeley) 3/20/94";
#include <limits.h> #include <limits.h>
#endif #endif
#include "utils.h"
#include "regex2.h" #include "regex2.h"
#include "utils.h"
#include "yapregex.h" #include "yapregex.h"
#if used #if used

View File

@ -22,7 +22,6 @@
* *
*/ */
#include "config.h"
#if HAVE_SYS_TYPES_H #if HAVE_SYS_TYPES_H
#include <sys/types.h> #include <sys/types.h>
#endif #endif

View File

@ -38,66 +38,68 @@
*/ */
#ifndef _REGEX_H_ #ifndef _REGEX_H_
#define _REGEX_H_ #define _REGEX_H_
#include "YapInterface.h"
/* types */ /* types */
typedef int regoff_t; typedef int regoff_t;
typedef struct { typedef struct {
int re_magic; int re_magic;
int re_nsub; /* number of parenthesized subexpressions */ int re_nsub; /* number of parenthesized subexpressions */
const char *re_endp; /* end pointer for REG_PEND */ const char *re_endp; /* end pointer for REG_PEND */
struct re_guts *re_g; /* none of your business :-) */ struct re_guts *re_g; /* none of your business :-) */
} regex_t; } regex_t;
typedef struct { typedef struct {
regoff_t rm_so; /* start of match */ regoff_t rm_so; /* start of match */
regoff_t rm_eo; /* end of match */ regoff_t rm_eo; /* end of match */
} regmatch_t; } regmatch_t;
/* regcomp() flags */ /* regcomp() flags */
#define REG_BASIC 0000 #define REG_BASIC 0000
#define REG_EXTENDED 0001 #define REG_EXTENDED 0001
#define REG_ICASE 0002 #define REG_ICASE 0002
#define REG_NOSUB 0004 #define REG_NOSUB 0004
#define REG_NEWLINE 0010 #define REG_NEWLINE 0010
#define REG_NOSPEC 0020 #define REG_NOSPEC 0020
#define REG_PEND 0040 #define REG_PEND 0040
#define REG_DUMP 0200 #define REG_DUMP 0200
/* regerror() flags */ /* regerror() flags */
#define REG_NOMATCH 1 #define REG_NOMATCH 1
#define REG_BADPAT 2 #define REG_BADPAT 2
#define REG_ECOLLATE 3 #define REG_ECOLLATE 3
#define REG_ECTYPE 4 #define REG_ECTYPE 4
#define REG_EESCAPE 5 #define REG_EESCAPE 5
#define REG_ESUBREG 6 #define REG_ESUBREG 6
#define REG_EBRACK 7 #define REG_EBRACK 7
#define REG_EPAREN 8 #define REG_EPAREN 8
#define REG_EBRACE 9 #define REG_EBRACE 9
#define REG_BADBR 10 #define REG_BADBR 10
#define REG_ERANGE 11 #define REG_ERANGE 11
#define REG_ESPACE 12 #define REG_ESPACE 12
#define REG_BADRPT 13 #define REG_BADRPT 13
#define REG_EMPTY 14 #define REG_EMPTY 14
#define REG_ASSERT 15 #define REG_ASSERT 15
#define REG_INVARG 16 #define REG_INVARG 16
#define REG_ATOI 255 /* convert name to number (!) */ #define REG_ATOI 255 /* convert name to number (!) */
#define REG_ITOA 0400 /* convert number to name (!) */ #define REG_ITOA 0400 /* convert number to name (!) */
/* regexec() flags */ /* regexec() flags */
#define REG_NOTBOL 00001 #define REG_NOTBOL 00001
#define REG_NOTEOL 00002 #define REG_NOTEOL 00002
#define REG_STARTEND 00004 #define REG_STARTEND 00004
#define REG_TRACE 00400 /* tracing of execution */ #define REG_TRACE 00400 /* tracing of execution */
#define REG_LARGE 01000 /* force large representation */ #define REG_LARGE 01000 /* force large representation */
#define REG_BACKR 02000 /* force use of backref code */ #define REG_BACKR 02000 /* force use of backref code */
#include "YapInterface.h" #include "YapInterface.h"
int yap_regcomp(regex_t *, const char *, int); int yap_regcomp(regex_t *, const char *, int);
size_t yap_regerror(int, const regex_t *, char *, size_t); size_t yap_regerror(int, const regex_t *, char *, size_t);
int yap_regexec(const regex_t *, const char *, size_t, regmatch_t [], int); int yap_regexec(const regex_t *, const char *, size_t, regmatch_t[], int);
void yap_regfree(regex_t *); void yap_regfree(regex_t *);
#endif /* !_REGEX_H_ */ #endif /* !_REGEX_H_ */

View File

@ -1,20 +1,19 @@
/************************************************************************* /*************************************************************************
* * * *
* YAP Prolog * * YAP Prolog *
* * * *
* Yap Prolog was developed at NCCUP - Universidade do Porto * * Yap Prolog was developed at NCCUP - Universidade do Porto *
* * * *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 * * Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
* * * *
************************************************************************** **************************************************************************
* * * *
* *
* comments: regular expression interpreter * * comments: regular expression interpreter *
* * * *
*************************************************************************/ *************************************************************************/
#include "YapInterface.h" #include "YapInterface.h"
#include "config.h"
#include <stdlib.h> #include <stdlib.h>
@ -312,8 +311,7 @@ static YAP_Bool rename_file(void) {
return (TRUE); return (TRUE);
} }
static YAP_Bool read_link(void) {
static YAP_Bool read_link(void) {
char *s1 = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1)); char *s1 = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
#if HAVE_READLINK #if HAVE_READLINK
char buf[MAXPATHLEN + 1]; char buf[MAXPATHLEN + 1];
@ -321,13 +319,12 @@ static YAP_Bool rename_file(void) {
if (readlink(s1, buf, MAXPATHLEN) < 0) if (readlink(s1, buf, MAXPATHLEN) < 0)
return false; return false;
/* return an error number */
/* return an error number */
if (!YAP_Unify(YAP_ARG2, YAP_MkAtomTerm(YAP_LookupAtom(buf)))) { if (!YAP_Unify(YAP_ARG2, YAP_MkAtomTerm(YAP_LookupAtom(buf)))) {
return false; return false;
} }
#endif #endif
# if _WIN32 #if _WIN32
return false; return false;
#endif #endif
return true; return true;

File diff suppressed because it is too large Load Diff

View File

@ -5,56 +5,50 @@
version: $ID$ version: $ID$
*********************************************/ *********************************************/
/* -------------------------------------- */ /* -------------------------------------- */
/* Yap Tagging Scheme */ /* Yap Tagging Scheme */
/* -------------------------------------- */ /* -------------------------------------- */
#include "config.h" #include "YapInterface.h"
#if SIZEOF_INT_P==4 #if SIZEOF_INT_P == 4
#define TAG_LOW_BITS_32 /* 'Tags_32LowTag.h' tagging scheme */ #define TAG_LOW_BITS_32 /* 'Tags_32LowTag.h' tagging scheme */
#define SIZE_FLOAT_AS_TERM 2 #define SIZE_FLOAT_AS_TERM 2
#elif SIZEOF_INT_P==8 #elif SIZEOF_INT_P == 8
#define TAG_64BITS /* 'Tags_64bits.h' tagging scheme */ #define TAG_64BITS /* 'Tags_64bits.h' tagging scheme */
#define SIZE_FLOAT_AS_TERM 1 #define SIZE_FLOAT_AS_TERM 1
#else #else
#error Unknown tagging scheme #error Unknown tagging scheme
#endif /* YAP_SCHEME */ #endif /* YAP_SCHEME */
/* --------------------------- */ /* --------------------------- */
/* Defines */ /* Defines */
/* --------------------------- */ /* --------------------------- */
#ifdef TAG_LOW_BITS_32 #ifdef TAG_LOW_BITS_32
#define ApplTag 1 /* 0x01 */ #define ApplTag 1 /* 0x01 */
#else /* TAG_64BITS */ #else /* TAG_64BITS */
#define ApplTag 5 /* 0x05 */ #define ApplTag 5 /* 0x05 */
#endif /* TAG_SCHEME */ #endif /* TAG_SCHEME */
#define PairInitTag 3 /* 0x03 */ #define PairInitTag 3 /* 0x03 */
#define PairEndEmptyTag 19 /* 0x13 */ #define PairEndEmptyTag 19 /* 0x13 */
#define PairEndTermTag 99 /* 0x63 */ #define PairEndTermTag 99 /* 0x63 */
#define CommaInitTag 35 /* 0x23 */ #define CommaInitTag 35 /* 0x23 */
#define CommaEndTag 51 /* 0x33 */ #define CommaEndTag 51 /* 0x33 */
#define FloatInitTag 67 /* 0x43 */ #define FloatInitTag 67 /* 0x43 */
#define FloatEndTag 83 /* 0x53 */ #define FloatEndTag 83 /* 0x53 */
#define TRIE_MODE_STANDARD 0 #define TRIE_MODE_STANDARD 0
#define TRIE_MODE_REVERSE 1 #define TRIE_MODE_REVERSE 1
#define TRIE_MODE_MINIMAL 2 #define TRIE_MODE_MINIMAL 2
#define TRIE_MODE_REVMIN TRIE_MODE_REVERSE || TRIE_MODE_MINIMAL //3 #define TRIE_MODE_REVMIN TRIE_MODE_REVERSE || TRIE_MODE_MINIMAL // 3
#define TRIE_PRINT_NORMAL 0 #define TRIE_PRINT_NORMAL 0
#define TRIE_PRINT_FLOAT 1 #define TRIE_PRINT_FLOAT 1
#define TRIE_PRINT_FLOAT2 2 #define TRIE_PRINT_FLOAT2 2
#define TRIE_PRINT_FLOAT_END 3 #define TRIE_PRINT_FLOAT_END 3
#define BASE_AUXILIARY_TERM_STACK_SIZE 100000 #define BASE_AUXILIARY_TERM_STACK_SIZE 100000
/* --------------------------- */ /* --------------------------- */
/* Structs */ /* Structs */
/* --------------------------- */ /* --------------------------- */
@ -71,17 +65,17 @@ typedef struct trie_engine {
YAP_Int tries_max_used; YAP_Int tries_max_used;
YAP_Int entries_max_used; YAP_Int entries_max_used;
YAP_Int nodes_max_used; YAP_Int nodes_max_used;
} *TrEngine; } * TrEngine;
#define TrEngine_trie(X) ((X)->first_trie) #define TrEngine_trie(X) ((X)->first_trie)
#define TrEngine_memory(X) ((X)->memory_in_use) #define TrEngine_memory(X) ((X)->memory_in_use)
#define TrEngine_tries(X) ((X)->tries_in_use) #define TrEngine_tries(X) ((X)->tries_in_use)
#define TrEngine_entries(X) ((X)->entries_in_use) #define TrEngine_entries(X) ((X)->entries_in_use)
#define TrEngine_nodes(X) ((X)->nodes_in_use) #define TrEngine_nodes(X) ((X)->nodes_in_use)
#define TrEngine_memory_max(X) ((X)->memory_max_used) #define TrEngine_memory_max(X) ((X)->memory_max_used)
#define TrEngine_tries_max(X) ((X)->tries_max_used) #define TrEngine_tries_max(X) ((X)->tries_max_used)
#define TrEngine_entries_max(X) ((X)->entries_max_used) #define TrEngine_entries_max(X) ((X)->entries_max_used)
#define TrEngine_nodes_max(X) ((X)->nodes_max_used) #define TrEngine_nodes_max(X) ((X)->nodes_max_used)
typedef struct trie_node { typedef struct trie_node {
struct trie_node *parent; struct trie_node *parent;
@ -89,230 +83,253 @@ typedef struct trie_node {
struct trie_node *next; struct trie_node *next;
struct trie_node *previous; struct trie_node *previous;
YAP_Term entry; YAP_Term entry;
} *TrNode; } * TrNode;
#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_entry(X) ((X)->entry) #define TrNode_entry(X) ((X)->entry)
typedef struct trie_hash { typedef struct trie_hash {
struct trie_node *parent; /* for compatibility with the trie_node data structure */ struct trie_node
*parent; /* for compatibility with the trie_node data structure */
struct trie_node **buckets; struct trie_node **buckets;
int number_of_buckets; int number_of_buckets;
int number_of_nodes; int number_of_nodes;
} *TrHash; } * TrHash;
#define TrHash_mark(X) ((X)->parent) #define TrHash_mark(X) ((X)->parent)
#define TrHash_buckets(X) ((X)->buckets) #define TrHash_buckets(X) ((X)->buckets)
#define TrHash_bucket(X,N) ((X)->buckets + N) #define TrHash_bucket(X, N) ((X)->buckets + N)
#define TrHash_num_buckets(X) ((X)->number_of_buckets) #define TrHash_num_buckets(X) ((X)->number_of_buckets)
#define TrHash_seed(X) ((X)->number_of_buckets - 1) #define TrHash_seed(X) ((X)->number_of_buckets - 1)
#define TrHash_num_nodes(X) ((X)->number_of_nodes) #define TrHash_num_nodes(X) ((X)->number_of_nodes)
#define TYPE_TR_ENGINE struct trie_engine
#define TYPE_TR_NODE struct trie_node
#define TYPE_TR_HASH struct trie_hash
#define SIZEOF_TR_ENGINE sizeof(TYPE_TR_ENGINE)
#define SIZEOF_TR_NODE sizeof(TYPE_TR_NODE)
#define SIZEOF_TR_HASH sizeof(TYPE_TR_HASH)
#define SIZEOF_TR_BUCKET sizeof(TYPE_TR_NODE *)
#define AS_TR_NODE_NEXT(ADDR) (TrNode)((YAP_UInt)(ADDR) - 2 * sizeof(struct trie_node *))
#define TYPE_TR_ENGINE struct trie_engine
#define TYPE_TR_NODE struct trie_node
#define TYPE_TR_HASH struct trie_hash
#define SIZEOF_TR_ENGINE sizeof(TYPE_TR_ENGINE)
#define SIZEOF_TR_NODE sizeof(TYPE_TR_NODE)
#define SIZEOF_TR_HASH sizeof(TYPE_TR_HASH)
#define SIZEOF_TR_BUCKET sizeof(TYPE_TR_NODE *)
#define AS_TR_NODE_NEXT(ADDR) \
(TrNode)((YAP_UInt)(ADDR)-2 * sizeof(struct trie_node *))
/* --------------------------- */ /* --------------------------- */
/* Macros */ /* Macros */
/* --------------------------- */ /* --------------------------- */
#define TAG_ADDR(ADDR) ((YAP_UInt)(ADDR) | 0x1) #define TAG_ADDR(ADDR) ((YAP_UInt)(ADDR) | 0x1)
#define UNTAG_ADDR(ADDR) ((YAP_UInt)(ADDR) & ~(0x1)) #define UNTAG_ADDR(ADDR) ((YAP_UInt)(ADDR) & ~(0x1))
#define PUT_DATA_IN_LEAF_TRIE_NODE(TR_NODE, DATA) TrNode_child(TR_NODE) = (TrNode)TAG_ADDR(DATA) #define PUT_DATA_IN_LEAF_TRIE_NODE(TR_NODE, DATA) \
#define GET_DATA_FROM_LEAF_TRIE_NODE(TR_NODE) UNTAG_ADDR(TrNode_child(TR_NODE)) TrNode_child(TR_NODE) = (TrNode)TAG_ADDR(DATA)
#define MARK_AS_LEAF_TRIE_NODE(TR_NODE) PUT_DATA_IN_LEAF_TRIE_NODE(TR_NODE, TrNode_child(TR_NODE)) #define GET_DATA_FROM_LEAF_TRIE_NODE(TR_NODE) UNTAG_ADDR(TrNode_child(TR_NODE))
#define IS_LEAF_TRIE_NODE(TR_NODE) ((YAP_UInt)(TrNode_child(TR_NODE)) & 0x1) #define MARK_AS_LEAF_TRIE_NODE(TR_NODE) \
PUT_DATA_IN_LEAF_TRIE_NODE(TR_NODE, TrNode_child(TR_NODE))
#define IS_LEAF_TRIE_NODE(TR_NODE) ((YAP_UInt)(TrNode_child(TR_NODE)) & 0x1)
#define IsTrieVar(TERM, STACK, STACK_BASE) ((YAP_Term *)(TERM) > STACK && (YAP_Term *)(TERM) <= STACK_BASE) #define IsTrieVar(TERM, STACK, STACK_BASE) \
#define MkTrieVar(INDEX) ((INDEX) << 4) ((YAP_Term *)(TERM) > STACK && (YAP_Term *)(TERM) <= STACK_BASE)
#define TrieVarIndex(TERM) ((TERM) >> 4) #define MkTrieVar(INDEX) ((INDEX) << 4)
#define TrieVarIndex(TERM) ((TERM) >> 4)
#define BASE_HASH_BUCKETS 256 #define BASE_HASH_BUCKETS 256
#define MAX_NODES_PER_TRIE_LEVEL 32 #define MAX_NODES_PER_TRIE_LEVEL 32
#define MAX_NODES_PER_BUCKET (MAX_NODES_PER_TRIE_LEVEL / 2) #define MAX_NODES_PER_BUCKET (MAX_NODES_PER_TRIE_LEVEL / 2)
#define HASH_TERM(TERM, SEED) (((TERM) >> 4) & (SEED)) #define HASH_TERM(TERM, SEED) (((TERM) >> 4) & (SEED))
#define IS_HASH_NODE(NODE) (TrHash_mark(NODE) == NULL) #define IS_HASH_NODE(NODE) (TrHash_mark(NODE) == NULL)
#define BASE_SAVE_MARK 10000 /* could lead to errors if the number of different variables in a term is greater than it */ #define BASE_SAVE_MARK \
#define HASH_SAVE_MARK ((YAP_Term) MkTrieVar(BASE_SAVE_MARK)) 10000 /* could lead to errors if the number of different variables in a term \
#define ATOM_SAVE_MARK ((YAP_Term) MkTrieVar(BASE_SAVE_MARK + 1)) is greater than it */
#define FUNCTOR_SAVE_MARK ((YAP_Term) MkTrieVar(BASE_SAVE_MARK + 2)) #define HASH_SAVE_MARK ((YAP_Term)MkTrieVar(BASE_SAVE_MARK))
#define FLOAT_SAVE_MARK ((YAP_Term) MkTrieVar(BASE_SAVE_MARK + 3)) #define ATOM_SAVE_MARK ((YAP_Term)MkTrieVar(BASE_SAVE_MARK + 1))
#define FUNCTOR_SAVE_MARK ((YAP_Term)MkTrieVar(BASE_SAVE_MARK + 2))
#define FLOAT_SAVE_MARK ((YAP_Term)MkTrieVar(BASE_SAVE_MARK + 3))
#define STACK_NOT_EMPTY(STACK, STACK_BASE) STACK != STACK_BASE #define STACK_NOT_EMPTY(STACK, STACK_BASE) STACK != STACK_BASE
#define POP_UP(STACK) *--STACK #define POP_UP(STACK) *--STACK
#define POP_DOWN(STACK) *++STACK #define POP_DOWN(STACK) *++STACK
#define PUSH_UP(STACK, ITEM, STACK_TOP) \ #define PUSH_UP(STACK, ITEM, STACK_TOP) \
{ if (STACK < STACK_TOP) { \ { \
fprintf(stderr, "**************************************\n"); \ if (STACK < STACK_TOP) { \
fprintf(stderr, " Tries core module: term stack full\n"); \ fprintf(stderr, "**************************************\n"); \
fprintf(stderr, "**************************************\n"); \ fprintf(stderr, " Tries core module: term stack full\n"); \
} \ fprintf(stderr, "**************************************\n"); \
*STACK = (YAP_Term)(ITEM); \ } \
STACK--; \ *STACK = (YAP_Term)(ITEM); \
} STACK--; \
#define PUSH_DOWN(STACK, ITEM, STACK_TOP) \ }
{ if (STACK > STACK_TOP) { \ #define PUSH_DOWN(STACK, ITEM, STACK_TOP) \
fprintf(stderr, "**************************************\n"); \ { \
fprintf(stderr, " Tries core module: term stack empty\n"); \ if (STACK > STACK_TOP) { \
fprintf(stderr, "**************************************\n"); \ fprintf(stderr, "**************************************\n"); \
} \ fprintf(stderr, " Tries core module: term stack empty\n"); \
*STACK = (YAP_Term)(ITEM); \ fprintf(stderr, "**************************************\n"); \
STACK++; \ } \
} *STACK = (YAP_Term)(ITEM); \
STACK++; \
}
#define new_struct(STR, STR_TYPE, STR_SIZE) \
STR = (STR_TYPE *)YAP_AllocSpaceFromYap(STR_SIZE)
#define new_trie_engine(TR_ENGINE) \
{ \
new_struct(TR_ENGINE, TYPE_TR_ENGINE, SIZEOF_TR_ENGINE); \
TrEngine_trie(TR_ENGINE) = NULL; \
TrEngine_memory(TR_ENGINE) = 0; \
TrEngine_tries(TR_ENGINE) = 0; \
TrEngine_entries(TR_ENGINE) = 0; \
TrEngine_nodes(TR_ENGINE) = 0; \
TrEngine_memory_max(TR_ENGINE) = 0; \
TrEngine_tries_max(TR_ENGINE) = 0; \
TrEngine_entries_max(TR_ENGINE) = 0; \
TrEngine_nodes_max(TR_ENGINE) = 0; \
}
#define new_trie_node(TR_NODE, ENTRY, PARENT, CHILD, NEXT, PREVIOUS) \
{ \
new_struct(TR_NODE, TYPE_TR_NODE, SIZEOF_TR_NODE); \
TrNode_entry(TR_NODE) = ENTRY; \
TrNode_parent(TR_NODE) = PARENT; \
TrNode_child(TR_NODE) = CHILD; \
TrNode_next(TR_NODE) = NEXT; \
TrNode_previous(TR_NODE) = PREVIOUS; \
INCREMENT_NODES(CURRENT_TRIE_ENGINE); \
INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_NODE); \
}
#define new_trie_hash(TR_HASH, NUM_NODES, NUM_BUCKETS) \
{ \
new_struct(TR_HASH, TYPE_TR_HASH, SIZEOF_TR_HASH); \
TrHash_mark(TR_HASH) = NULL; \
TrHash_num_buckets(TR_HASH) = NUM_BUCKETS; \
new_hash_buckets(TR_HASH, NUM_BUCKETS); \
TrHash_num_nodes(TR_HASH) = NUM_NODES; \
INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_HASH); \
}
#define new_hash_buckets(TR_HASH, NUM_BUCKETS) \
{ \
int i; \
void **ptr; \
new_struct(ptr, void *, NUM_BUCKETS * sizeof(void *)); \
TrHash_buckets(TR_HASH) = (TYPE_TR_NODE **)ptr; \
for (i = NUM_BUCKETS; i != 0; i--) \
*ptr++ = NULL; \
INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, (NUM_BUCKETS)*SIZEOF_TR_BUCKET); \
}
#define expand_auxiliary_term_stack() \
{ \
YAP_Term *aux_stack; \
YAP_Int aux_size = CURRENT_AUXILIARY_TERM_STACK_SIZE * sizeof(YAP_Term); \
new_struct(aux_stack, YAP_Term, aux_size * 2); \
memmove(aux_stack, AUXILIARY_TERM_STACK, aux_size); \
free_struct(AUXILIARY_TERM_STACK); \
AUXILIARY_TERM_STACK = aux_stack; \
CURRENT_AUXILIARY_TERM_STACK_SIZE *= 2; \
}
#define new_struct(STR, STR_TYPE, STR_SIZE) \ #define free_struct(STR) YAP_FreeSpaceFromYap((char *)(STR))
STR = (STR_TYPE *) YAP_AllocSpaceFromYap(STR_SIZE) #define free_trie_node(STR) \
#define new_trie_engine(TR_ENGINE) \ { \
{ new_struct(TR_ENGINE, TYPE_TR_ENGINE, SIZEOF_TR_ENGINE); \ free_struct(STR); \
TrEngine_trie(TR_ENGINE) = NULL; \ DECREMENT_NODES(CURRENT_TRIE_ENGINE); \
TrEngine_memory(TR_ENGINE) = 0; \ DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_NODE); \
TrEngine_tries(TR_ENGINE) = 0; \ }
TrEngine_entries(TR_ENGINE) = 0; \ #define free_trie_hash(STR) \
TrEngine_nodes(TR_ENGINE) = 0; \ { \
TrEngine_memory_max(TR_ENGINE) = 0; \ free_struct(STR); \
TrEngine_tries_max(TR_ENGINE) = 0; \ DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_HASH); \
TrEngine_entries_max(TR_ENGINE) = 0; \ }
TrEngine_nodes_max(TR_ENGINE) = 0; \ #define free_hash_buckets(STR, NUM_BUCKETS) \
} { \
#define new_trie_node(TR_NODE, ENTRY, PARENT, CHILD, NEXT, PREVIOUS) \ free_struct(STR); \
{ new_struct(TR_NODE, TYPE_TR_NODE, SIZEOF_TR_NODE); \ DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, (NUM_BUCKETS)*SIZEOF_TR_BUCKET); \
TrNode_entry(TR_NODE) = ENTRY; \ }
TrNode_parent(TR_NODE) = PARENT; \
TrNode_child(TR_NODE) = CHILD; \
TrNode_next(TR_NODE) = NEXT; \
TrNode_previous(TR_NODE) = PREVIOUS; \
INCREMENT_NODES(CURRENT_TRIE_ENGINE); \
INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_NODE); \
}
#define new_trie_hash(TR_HASH, NUM_NODES, NUM_BUCKETS) \
{ new_struct(TR_HASH, TYPE_TR_HASH, SIZEOF_TR_HASH); \
TrHash_mark(TR_HASH) = NULL; \
TrHash_num_buckets(TR_HASH) = NUM_BUCKETS; \
new_hash_buckets(TR_HASH, NUM_BUCKETS); \
TrHash_num_nodes(TR_HASH) = NUM_NODES; \
INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_HASH); \
}
#define new_hash_buckets(TR_HASH, NUM_BUCKETS) \
{ int i; void **ptr; \
new_struct(ptr, void *, NUM_BUCKETS * sizeof(void *)); \
TrHash_buckets(TR_HASH) = (TYPE_TR_NODE **) ptr; \
for (i = NUM_BUCKETS; i != 0; i--) \
*ptr++ = NULL; \
INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, (NUM_BUCKETS) * SIZEOF_TR_BUCKET); \
}
#define INCREMENT_MEMORY(TR_ENGINE, SIZE) \
{ \
TrEngine_memory(TR_ENGINE) += SIZE; \
if (TrEngine_memory(TR_ENGINE) > TrEngine_memory_max(TR_ENGINE)) \
TrEngine_memory_max(TR_ENGINE) = TrEngine_memory(TR_ENGINE); \
}
#define INCREMENT_TRIES(TR_ENGINE) \
{ \
TrEngine_tries(TR_ENGINE)++; \
if (TrEngine_tries(TR_ENGINE) > TrEngine_tries_max(TR_ENGINE)) \
TrEngine_tries_max(TR_ENGINE) = TrEngine_tries(TR_ENGINE); \
}
#define INCREMENT_ENTRIES(TR_ENGINE) \
{ \
TrEngine_entries(TR_ENGINE)++; \
if (TrEngine_entries(TR_ENGINE) > TrEngine_entries_max(TR_ENGINE)) \
TrEngine_entries_max(TR_ENGINE) = TrEngine_entries(TR_ENGINE); \
}
#define INCREMENT_NODES(TR_ENGINE) \
{ \
TrEngine_nodes(TR_ENGINE)++; \
if (TrEngine_nodes(TR_ENGINE) > TrEngine_nodes_max(TR_ENGINE)) \
TrEngine_nodes_max(TR_ENGINE) = TrEngine_nodes(TR_ENGINE); \
}
#define DECREMENT_MEMORY(TR_ENGINE, SIZE) TrEngine_memory(TR_ENGINE) -= SIZE
#define DECREMENT_TRIES(TR_ENGINE) TrEngine_tries(TR_ENGINE)--
#define DECREMENT_ENTRIES(TR_ENGINE) TrEngine_entries(TR_ENGINE)--
#define DECREMENT_NODES(TR_ENGINE) TrEngine_nodes(TR_ENGINE)--
#define IS_FUNCTOR_NODE(N) \
#define expand_auxiliary_term_stack() \ (((ApplTag & TrNode_entry(N)) == ApplTag) && \
{ YAP_Term *aux_stack; \ (TrNode_entry(N) != PairInitTag) && (TrNode_entry(N) != PairEndEmptyTag) && \
YAP_Int aux_size = CURRENT_AUXILIARY_TERM_STACK_SIZE * sizeof(YAP_Term); \ (TrNode_entry(N) != PairEndTermTag))
new_struct(aux_stack, YAP_Term, aux_size * 2); \
memmove(aux_stack, AUXILIARY_TERM_STACK, aux_size); \
free_struct(AUXILIARY_TERM_STACK); \
AUXILIARY_TERM_STACK = aux_stack; \
CURRENT_AUXILIARY_TERM_STACK_SIZE *= 2; \
}
#define free_struct(STR) \
YAP_FreeSpaceFromYap((char *) (STR))
#define free_trie_node(STR) \
{ free_struct(STR); \
DECREMENT_NODES(CURRENT_TRIE_ENGINE); \
DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_NODE); \
}
#define free_trie_hash(STR) \
{ free_struct(STR); \
DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_HASH); \
}
#define free_hash_buckets(STR, NUM_BUCKETS) \
{ free_struct(STR); \
DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, (NUM_BUCKETS) * SIZEOF_TR_BUCKET); \
}
#define INCREMENT_MEMORY(TR_ENGINE, SIZE) \
{ TrEngine_memory(TR_ENGINE) += SIZE; \
if (TrEngine_memory(TR_ENGINE) > TrEngine_memory_max(TR_ENGINE)) \
TrEngine_memory_max(TR_ENGINE) = TrEngine_memory(TR_ENGINE); \
}
#define INCREMENT_TRIES(TR_ENGINE) \
{ TrEngine_tries(TR_ENGINE)++; \
if (TrEngine_tries(TR_ENGINE) > TrEngine_tries_max(TR_ENGINE)) \
TrEngine_tries_max(TR_ENGINE) = TrEngine_tries(TR_ENGINE); \
}
#define INCREMENT_ENTRIES(TR_ENGINE) \
{ TrEngine_entries(TR_ENGINE)++; \
if (TrEngine_entries(TR_ENGINE) > TrEngine_entries_max(TR_ENGINE)) \
TrEngine_entries_max(TR_ENGINE) = TrEngine_entries(TR_ENGINE); \
}
#define INCREMENT_NODES(TR_ENGINE) \
{ TrEngine_nodes(TR_ENGINE)++; \
if (TrEngine_nodes(TR_ENGINE) > TrEngine_nodes_max(TR_ENGINE)) \
TrEngine_nodes_max(TR_ENGINE) = TrEngine_nodes(TR_ENGINE); \
}
#define DECREMENT_MEMORY(TR_ENGINE, SIZE) \
TrEngine_memory(TR_ENGINE) -= SIZE
#define DECREMENT_TRIES(TR_ENGINE) \
TrEngine_tries(TR_ENGINE)--
#define DECREMENT_ENTRIES(TR_ENGINE) \
TrEngine_entries(TR_ENGINE)--
#define DECREMENT_NODES(TR_ENGINE) \
TrEngine_nodes(TR_ENGINE)--
#define IS_FUNCTOR_NODE(N) (((ApplTag & TrNode_entry(N)) == ApplTag) && \
(TrNode_entry(N) != PairInitTag) && \
(TrNode_entry(N) != PairEndEmptyTag) && \
(TrNode_entry(N) != PairEndTermTag))
/* --------------------------- */ /* --------------------------- */
/* API */ /* API */
/* --------------------------- */ /* --------------------------- */
extern TrEngine core_trie_init_module(void); extern TrEngine core_trie_init_module(void);
extern TrNode core_trie_open(TrEngine engine); extern TrNode core_trie_open(TrEngine engine);
extern void core_trie_close(TrEngine engine, TrNode node, void (*destruct_function)(TrNode)); extern void core_trie_close(TrEngine engine, TrNode node,
extern void core_trie_close_all(TrEngine engine, void (*destruct_function)(TrNode)); void (*destruct_function)(TrNode));
extern void core_trie_set_mode(YAP_Int mode); extern void core_trie_close_all(TrEngine engine,
extern YAP_Int core_trie_get_mode(void); void (*destruct_function)(TrNode));
extern TrNode core_trie_put_entry(TrEngine engine, TrNode node, YAP_Term entry, YAP_Int *depth); extern void core_trie_set_mode(YAP_Int mode);
extern TrNode core_trie_check_entry(TrNode node, YAP_Term entry); extern YAP_Int core_trie_get_mode(void);
extern TrNode core_trie_put_entry(TrEngine engine, TrNode node, YAP_Term entry,
YAP_Int *depth);
extern TrNode core_trie_check_entry(TrNode node, YAP_Term entry);
extern YAP_Term core_trie_get_entry(TrNode node); extern YAP_Term core_trie_get_entry(TrNode node);
extern void core_trie_remove_entry(TrEngine engine, TrNode node, void (*destruct_function)(TrNode)); extern void core_trie_remove_entry(TrEngine engine, TrNode node,
extern void core_trie_remove_subtree(TrEngine engine, TrNode node, void (*destruct_function)(TrNode)); void (*destruct_function)(TrNode));
extern void core_trie_add(TrNode node_dest, TrNode node_source, void (*add_function)(TrNode, TrNode)); extern void core_trie_remove_subtree(TrEngine engine, TrNode node,
extern void core_trie_join(TrEngine engine, TrNode node_dest, TrNode node_source, void (*add_function)(TrNode, TrNode), void (*copy_function)(TrNode, TrNode)); void (*destruct_function)(TrNode));
extern void core_trie_intersect(TrEngine engine, TrNode node_dest, TrNode node_source, void (*add_function)(TrNode, TrNode), void (*destruct_function)(TrNode)); extern void core_trie_add(TrNode node_dest, TrNode node_source,
extern YAP_Int core_trie_count_join(TrNode node1, TrNode node2); void (*add_function)(TrNode, TrNode));
extern YAP_Int core_trie_count_intersect(TrNode node1, TrNode node2); extern void core_trie_join(TrEngine engine, TrNode node_dest,
extern void core_trie_save(TrNode node, FILE *file, void (*save_function)(TrNode, FILE *)); TrNode node_source,
extern TrNode core_trie_load(TrEngine engine, FILE *file, void (*load_function)(TrNode, YAP_Int, FILE *)); void (*add_function)(TrNode, TrNode),
extern void core_trie_stats(TrEngine engine, YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes); void (*copy_function)(TrNode, TrNode));
extern void core_trie_max_stats(TrEngine engine, YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes); extern void core_trie_intersect(TrEngine engine, TrNode node_dest,
extern void core_trie_usage(TrNode node, YAP_Int *entries, YAP_Int *nodes, YAP_Int *virtual_nodes); TrNode node_source,
extern void core_trie_print(TrNode node, void (*print_function)(TrNode)); void (*add_function)(TrNode, TrNode),
void (*destruct_function)(TrNode));
extern YAP_Int core_trie_count_join(TrNode node1, TrNode node2);
extern YAP_Int core_trie_count_intersect(TrNode node1, TrNode node2);
extern void core_trie_save(TrNode node, FILE *file,
void (*save_function)(TrNode, FILE *));
extern TrNode core_trie_load(TrEngine engine, FILE *file,
void (*load_function)(TrNode, YAP_Int, FILE *));
extern void core_trie_stats(TrEngine engine, YAP_Int *memory, YAP_Int *tries,
YAP_Int *entries, YAP_Int *nodes);
extern void core_trie_max_stats(TrEngine engine, YAP_Int *memory,
YAP_Int *tries, YAP_Int *entries,
YAP_Int *nodes);
extern void core_trie_usage(TrNode node, YAP_Int *entries, YAP_Int *nodes,
YAP_Int *virtual_nodes);
extern void core_trie_print(TrNode node, void (*print_function)(TrNode));
extern void core_disable_hash_table(void); extern void core_disable_hash_table(void);
extern void core_enable_hash_table(void); extern void core_enable_hash_table(void);
extern YAP_Term core_trie_to_list(TrNode node); extern YAP_Term core_trie_to_list(TrNode node);
#include "core_dbtries.h" #include "core_dbtries.h"

View File

@ -16,8 +16,7 @@
/* Written by Eric Blake <e...@byu.net>, 2010. */ /* Written by Eric Blake <e...@byu.net>, 2010. */
#include "YapConfig.h"
#include "config.h"
/* Specification. */ /* Specification. */
#include <stdio.h> #include <stdio.h>
@ -32,60 +31,55 @@
#if !HAVE_OPEN_MEMSTREAM && !_WIN32 #if !HAVE_OPEN_MEMSTREAM && !_WIN32
#if !HAVE_FUNOPEN #if !HAVE_FUNOPEN
# error Sorry, not ported to your platform yet #error Sorry, not ported to your platform yet
#else #else
FILE * open_memstream (char **buf, size_t *len); FILE *open_memstream(char **buf, size_t *len);
# define INITIAL_ALLOC 64 #define INITIAL_ALLOC 64
struct data struct data {
{ char **buf; /* User's argument. */
char **buf; /* User's argument. */ size_t *len; /* User's argument. Smaller of pos or eof. */
size_t *len; /* User's argument. Smaller of pos or eof. */ size_t pos; /* Current position. */
size_t pos; /* Current position. */ size_t eof; /* End-of-file position. */
size_t eof; /* End-of-file position. */
size_t allocated; /* Allocated size of *buf, always > eof. */ size_t allocated; /* Allocated size of *buf, always > eof. */
char c; /* Temporary storage for byte overwritten by NUL, if pos < eof. */ char c; /* Temporary storage for byte overwritten by NUL, if pos < eof. */
}; };
typedef struct data data; typedef struct data data;
/* Stupid BSD interface uses int/int instead of ssize_t/size_t. */ /* Stupid BSD interface uses int/int instead of ssize_t/size_t. */
//verify (sizeof (int) <= sizeof (size_t)); // verify (sizeof (int) <= sizeof (size_t));
//verify (sizeof (int) <= sizeof (ssize_t)); // verify (sizeof (int) <= sizeof (ssize_t));
static int static int mem_write(void *c, const char *buf, int n) {
mem_write (void *c, const char *buf, int n)
{
data *cookie = c; data *cookie = c;
char *cbuf = *cookie->buf; char *cbuf = *cookie->buf;
/* Be sure we don't overflow. */ /* Be sure we don't overflow. */
if ((ssize_t) (cookie->pos + n) < 0) if ((ssize_t)(cookie->pos + n) < 0) {
{ errno = EFBIG;
errno = EFBIG; return EOF;
return EOF; }
}
/* Grow the buffer, if necessary. Use geometric growth to avoid /* Grow the buffer, if necessary. Use geometric growth to avoid
quadratic realloc behavior. Overallocate, to accomodate the quadratic realloc behavior. Overallocate, to accomodate the
requirement to always place a trailing NUL not counted by length. requirement to always place a trailing NUL not counted by length.
Thus, we want max(prev_size*1.5, cookie->posn1). */ Thus, we want max(prev_size*1.5, cookie->posn1). */
if (cookie->allocated <= cookie->pos + n) if (cookie->allocated <= cookie->pos + n) {
{ size_t newsize = cookie->allocated * 3 / 2;
size_t newsize = cookie->allocated * 3 / 2; if (newsize < cookie->pos + n + 1)
if (newsize < cookie->pos + n + 1) newsize = cookie->pos + n + 1;
newsize = cookie->pos + n + 1; cbuf = realloc(cbuf, newsize);
cbuf = realloc (cbuf, newsize); if (!cbuf)
if (!cbuf) return EOF;
return EOF; *cookie->buf = cbuf;
*cookie->buf = cbuf; cookie->allocated = newsize;
cookie->allocated = newsize; }
}
/* If we have previously done a seek beyond eof, ensure all /* If we have previously done a seek beyond eof, ensure all
intermediate bytges are NUL. */ intermediate bytges are NUL. */
if (cookie->eof < cookie->pos) if (cookie->eof < cookie->pos)
memset (cbuf + cookie->eof, '\0', cookie->pos - cookie->eof); memset(cbuf + cookie->eof, '\0', cookie->pos - cookie->eof);
memmove (cbuf + cookie->pos, buf, n); memmove(cbuf + cookie->pos, buf, n);
cookie->pos += n; cookie->pos += n;
/* If the user has previously written beyond the current position, /* If the user has previously written beyond the current position,
remember what the trailing NUL is overwriting. Otherwise, remember what the trailing NUL is overwriting. Otherwise,
@ -99,9 +93,7 @@ mem_write (void *c, const char *buf, int n)
return n; return n;
} }
static fpos_t static fpos_t mem_seek(void *c, fpos_t pos, int whence) {
mem_seek (void *c, fpos_t pos, int whence)
{
data *cookie = c; data *cookie = c;
off_t offset = pos; off_t offset = pos;
@ -109,88 +101,71 @@ mem_seek (void *c, fpos_t pos, int whence)
offset = cookie->pos; offset = cookie->pos;
else if (whence == SEEK_END) else if (whence == SEEK_END)
offset = cookie->eof; offset = cookie->eof;
if (offset < 0) if (offset < 0) {
{ errno = EINVAL;
errno = EINVAL; offset = -1;
offset = -1; } else if ((size_t)offset != offset) {
} errno = ENOSPC;
else if ((size_t) offset != offset) offset = -1;
{ } else {
errno = ENOSPC; if (cookie->pos < cookie->eof) {
offset = -1; (*cookie->buf)[cookie->pos] = cookie->c;
} cookie->c = '\0';
else
{
if (cookie->pos < cookie->eof)
{
(*cookie->buf)[cookie->pos] = cookie->c;
cookie->c = '\0';
}
cookie->pos = offset;
if (cookie->pos < cookie->eof)
{
cookie->c = (*cookie->buf)[cookie->pos];
(*cookie->buf)[cookie->pos] = '\0';
*cookie->len = cookie->pos;
}
else
*cookie->len = cookie->eof;
} }
cookie->pos = offset;
if (cookie->pos < cookie->eof) {
cookie->c = (*cookie->buf)[cookie->pos];
(*cookie->buf)[cookie->pos] = '\0';
*cookie->len = cookie->pos;
} else
*cookie->len = cookie->eof;
}
return offset; return offset;
} }
static int static int mem_close(void *c) {
mem_close (void *c)
{
data *cookie = c; data *cookie = c;
char *buf; char *buf;
/* Be nice and try to reduce excess memory. */ /* Be nice and try to reduce excess memory. */
buf = realloc (*cookie->buf, *cookie->len + 1); buf = realloc(*cookie->buf, *cookie->len + 1);
if (buf) if (buf)
*cookie->buf = buf; *cookie->buf = buf;
free (cookie); free(cookie);
return 0; return 0;
} }
FILE * FILE *open_memstream(char **buf, size_t *len) {
open_memstream (char **buf, size_t *len)
{
FILE *f; FILE *f;
data *cookie; data *cookie;
if (!buf || !len) if (!buf || !len) {
{ errno = EINVAL;
errno = EINVAL;
return NULL;
}
if (!(cookie = malloc (sizeof *cookie)))
return NULL; return NULL;
if (!(*buf = malloc (INITIAL_ALLOC))) }
{ if (!(cookie = malloc(sizeof *cookie)))
free (cookie); return NULL;
errno = ENOMEM; if (!(*buf = malloc(INITIAL_ALLOC))) {
return NULL; free(cookie);
} errno = ENOMEM;
return NULL;
}
**buf = '\0'; **buf = '\0';
*len = 0; *len = 0;
f = funopen (cookie, NULL, mem_write, mem_seek, mem_close); f = funopen(cookie, NULL, mem_write, mem_seek, mem_close);
if (!f) if (!f) {
{ int saved_errno = errno;
int saved_errno = errno; free(cookie);
free (cookie); errno = saved_errno;
errno = saved_errno; } else {
} cookie->buf = buf;
else cookie->len = len;
{ cookie->pos = 0;
cookie->buf = buf; cookie->eof = 0;
cookie->len = len; cookie->c = '\0';
cookie->pos = 0; cookie->allocated = INITIAL_ALLOC;
cookie->eof = 0; }
cookie->c = '\0';
cookie->allocated = INITIAL_ALLOC;
}
return f; return f;
} }
#endif /* HAVE_FUNOPEN */ #endif /* HAVE_FUNOPEN */

View File

@ -13,7 +13,7 @@
* *
*/ */
#include "config.h" #include "YapConfig.h"
#if _WIN32 || defined(__MINGW32__) #if _WIN32 || defined(__MINGW32__)
#if !defined(MINGW_HAS_SECURE_API) #if !defined(MINGW_HAS_SECURE_API)
@ -22,10 +22,10 @@
//#undef _POSIX_ //#undef _POSIX_
#endif #endif
#include "Yap.h" #include "Yap.h"
#include "YapEval.h"
#include "YapHeap.h" #include "YapHeap.h"
#include "YapText.h" #include "YapText.h"
#include "Yatom.h" #include "Yatom.h"
#include "YapEval.h"
#include "yapio.h" #include "yapio.h"
// Win32 InputOutput Support // Win32 InputOutput Support
@ -46,7 +46,6 @@
#endif #endif
#endif #endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
@ -121,7 +120,6 @@
/* windows.h does not like absmi.h, this /* windows.h does not like absmi.h, this
should fix it for now */ should fix it for now */
#include <math.h> #include <math.h>
#include <math.h>
#if HAVE_TIME_H #if HAVE_TIME_H
#include <time.h> #include <time.h>
#endif #endif

View File

@ -41,7 +41,6 @@ CUDD will generate better/faster code.
#include <stdio.h> #include <stdio.h>
#include "YapInterface.h" #include "YapInterface.h"
#include "config.h"
#include "cudd_config.h" #include "cudd_config.h"
#if HAVE_STRING_H #if HAVE_STRING_H
@ -811,7 +810,7 @@ static YAP_Bool p_cudd_print_with_names(void) {
DdManager *manager = (DdManager *)YAP_IntOfTerm(YAP_ARG1); DdManager *manager = (DdManager *)YAP_IntOfTerm(YAP_ARG1);
DdNode *n0 = (DdNode *)YAP_IntOfTerm(YAP_ARG2); DdNode *n0 = (DdNode *)YAP_IntOfTerm(YAP_ARG2);
const char *s = YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3)); const char *s = YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3));
char ** namesp; char **namesp;
YAP_Term names = YAP_ARG4; YAP_Term names = YAP_ARG4;
FILE *f; FILE *f;
YAP_Int len; YAP_Int len;
@ -850,7 +849,7 @@ static YAP_Bool p_cudd_print_with_names(void) {
names = YAP_TailOfTerm(names); names = YAP_TailOfTerm(names);
namesp[i++] = f; namesp[i++] = f;
} }
Cudd_DumpDot(manager, 1, &n0, (const char * const*)namesp, NULL, f); Cudd_DumpDot(manager, 1, &n0, (const char *const *)namesp, NULL, f);
if (f != stdout && f != stderr) if (f != stdout && f != stderr)
fclose(f); fclose(f);
while (i > 0) { while (i > 0) {
@ -863,8 +862,8 @@ static YAP_Bool p_cudd_print_with_names(void) {
static YAP_Bool p_cudd_die(void) { static YAP_Bool p_cudd_die(void) {
DdManager *manager = (DdManager *)YAP_IntOfTerm(YAP_ARG1); DdManager *manager = (DdManager *)YAP_IntOfTerm(YAP_ARG1);
//Cudd_FreeTree(manager); // Cudd_FreeTree(manager);
//cuddFreeTable(manager); // cuddFreeTable(manager);
Cudd_CheckZeroRef(manager); Cudd_CheckZeroRef(manager);
Cudd_Quit(manager); Cudd_Quit(manager);
return TRUE; return TRUE;

View File

@ -186,14 +186,13 @@
* * * *
\******************************************************************************/ \******************************************************************************/
#include "YapInterface.h"
#include "cudd_config.h"
#include <math.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <math.h>
#include <time.h> #include <time.h>
#include "config.h"
#include "cudd_config.h"
#if HAVE_CUDD_UTIL_H #if HAVE_CUDD_UTIL_H
#include <cudd/util.h> #include <cudd/util.h>
@ -214,26 +213,26 @@
#include "general.h" #include "general.h"
#define IsHigh(manager, node) HIGH(manager) == node #define IsHigh(manager, node) HIGH(manager) == node
#define IsLow(manager, node) LOW(manager) == node #define IsLow(manager, node) LOW(manager) == node
#define HIGH(manager) Cudd_ReadOne(manager) #define HIGH(manager) Cudd_ReadOne(manager)
#define LOW(manager) Cudd_Not(Cudd_ReadOne(manager)) #define LOW(manager) Cudd_Not(Cudd_ReadOne(manager))
#define NOT(node) Cudd_Not(node) #define NOT(node) Cudd_Not(node)
#define GetIndex(node) Cudd_NodeReadIndex(node) #define GetIndex(node) Cudd_NodeReadIndex(node)
#define GetOrder(manager, node) Cudd_ReadPerm(manager, GetIndex(node)) #define GetOrder(manager, node) Cudd_ReadPerm(manager, GetIndex(node))
#define GetVar(manager, index) Cudd_bddIthVar(manager, index) #define GetVar(manager, index) Cudd_bddIthVar(manager, index)
#define NewVar(manager) Cudd_bddNewVar(manager) #define NewVar(manager) Cudd_bddNewVar(manager)
#define KillBDD(manager) Cudd_Quit(manager) #define KillBDD(manager) Cudd_Quit(manager)
#define GetVarCount(manager) Cudd_ReadSize(manager) #define GetVarCount(manager) Cudd_ReadSize(manager)
#define DEBUGON _debug = 1 #define DEBUGON _debug = 1
#define DEBUGOFF _debug = 0 #define DEBUGOFF _debug = 0
#define RAPIDLOADON _RapidLoad = 1 #define RAPIDLOADON _RapidLoad = 1
#define RAPIDLOADOFF _RapidLoad = 0 #define RAPIDLOADOFF _RapidLoad = 0
#define SETMAXBUFSIZE(size) _maxbufsize = size #define SETMAXBUFSIZE(size) _maxbufsize = size
#define BDDFILE_ERROR -1 #define BDDFILE_ERROR -1
#define BDDFILE_OTHER 0 #define BDDFILE_OTHER 0
#define BDDFILE_SCRIPT 1 #define BDDFILE_SCRIPT 1
#define BDDFILE_NODEDUMP 2 #define BDDFILE_NODEDUMP 2
extern int _RapidLoad; extern int _RapidLoad;
extern int _debug; extern int _debug;
@ -281,28 +280,33 @@ typedef struct _nodeline {
/* Initialization */ /* Initialization */
DdManager* simpleBDDinit(int varcnt); DdManager *simpleBDDinit(int varcnt);
DdManager* simpleBDDinitNoReOrder(int varcnt); DdManager *simpleBDDinitNoReOrder(int varcnt);
/* BDD Generation */ /* BDD Generation */
DdNode* D_BDDAnd(DdManager *manager, DdNode *bdd1, DdNode *bdd2); DdNode *D_BDDAnd(DdManager *manager, DdNode *bdd1, DdNode *bdd2);
DdNode* D_BDDNand(DdManager *manager, DdNode *bdd1, DdNode *bdd2); DdNode *D_BDDNand(DdManager *manager, DdNode *bdd1, DdNode *bdd2);
DdNode* D_BDDOr(DdManager *manager, DdNode *bdd1, DdNode *bdd2); DdNode *D_BDDOr(DdManager *manager, DdNode *bdd1, DdNode *bdd2);
DdNode* D_BDDNor(DdManager *manager, DdNode *bdd1, DdNode *bdd2); DdNode *D_BDDNor(DdManager *manager, DdNode *bdd1, DdNode *bdd2);
DdNode* D_BDDXor(DdManager *manager, DdNode *bdd1, DdNode *bdd2); DdNode *D_BDDXor(DdManager *manager, DdNode *bdd1, DdNode *bdd2);
DdNode* D_BDDXnor(DdManager *manager, DdNode *bdd1, DdNode *bdd2); DdNode *D_BDDXnor(DdManager *manager, DdNode *bdd1, DdNode *bdd2);
DdNode* FileGenerateBDD(DdManager *manager, namedvars varmap, bddfileheader fileheader); DdNode *FileGenerateBDD(DdManager *manager, namedvars varmap,
DdNode** FileGenerateBDDForest(DdManager *manager, namedvars varmap, bddfileheader fileheader); bddfileheader fileheader);
DdNode* OnlineGenerateBDD(DdManager *manager, namedvars *varmap); DdNode **FileGenerateBDDForest(DdManager *manager, namedvars varmap,
DdNode* LineParser(DdManager *manager, namedvars varmap, DdNode **inter, int maxinter, char *function, int iline); bddfileheader fileheader);
DdNode* OnlineLineParser(DdManager *manager, namedvars *varmap, DdNode **inter, int maxinter, char *function, int iline); DdNode *OnlineGenerateBDD(DdManager *manager, namedvars *varmap);
DdNode* BDD_Operator(DdManager *manager, DdNode *bdd1, DdNode *bdd2, char Operator, int inegoper); DdNode *LineParser(DdManager *manager, namedvars varmap, DdNode **inter,
int maxinter, char *function, int iline);
DdNode *OnlineLineParser(DdManager *manager, namedvars *varmap, DdNode **inter,
int maxinter, char *function, int iline);
DdNode *BDD_Operator(DdManager *manager, DdNode *bdd1, DdNode *bdd2,
char Operator, int inegoper);
int getInterBDD(char *function); int getInterBDD(char *function);
char* getFileName(const char *function); char *getFileName(const char *function);
int GetParam(char *inputline, int iParam); int GetParam(char *inputline, int iParam);
char** GetVariableOrder(char *filename, int varcnt); char **GetVariableOrder(char *filename, int varcnt);
int LoadVariableData(namedvars varmap, char *filename); int LoadVariableData(namedvars varmap, char *filename);
/* Named variables */ /* Named variables */
@ -313,42 +317,51 @@ namedvars InitNamedVars(int varcnt, int varstart);
void EnlargeNamedVars(namedvars *varmap, int newvarcnt); void EnlargeNamedVars(namedvars *varmap, int newvarcnt);
int AddNamedVarAt(namedvars varmap, const char *varname, int index); int AddNamedVarAt(namedvars varmap, const char *varname, int index);
int AddNamedVar(namedvars varmap, const char *varname); int AddNamedVar(namedvars varmap, const char *varname);
void SetNamedVarValuesAt(namedvars varmap, int index, double dvalue, int ivalue, void *dynvalue); void SetNamedVarValuesAt(namedvars varmap, int index, double dvalue, int ivalue,
int SetNamedVarValues(namedvars varmap, const char *varname, double dvalue, int ivalue, void *dynvalue); void *dynvalue);
int SetNamedVarValues(namedvars varmap, const char *varname, double dvalue,
int ivalue, void *dynvalue);
int GetNamedVarIndex(const namedvars varmap, const char *varname); int GetNamedVarIndex(const namedvars varmap, const char *varname);
int RepairVarcnt(namedvars *varmap); int RepairVarcnt(namedvars *varmap);
char* GetNodeVarName(DdManager *manager, namedvars varmap, DdNode *node); char *GetNodeVarName(DdManager *manager, namedvars varmap, DdNode *node);
char* GetNodeVarNameDisp(DdManager *manager, namedvars varmap, DdNode *node); char *GetNodeVarNameDisp(DdManager *manager, namedvars varmap, DdNode *node);
int all_loaded(namedvars varmap, int disp); int all_loaded(namedvars varmap, int disp);
/* Traversal */ /* Traversal */
DdNode* HighNodeOf(DdManager *manager, DdNode *node); DdNode *HighNodeOf(DdManager *manager, DdNode *node);
DdNode* LowNodeOf(DdManager *manager, DdNode *node); DdNode *LowNodeOf(DdManager *manager, DdNode *node);
/* Traversal - History */ /* Traversal - History */
hisqueue* InitHistory(int varcnt); hisqueue *InitHistory(int varcnt);
void ReInitHistory(hisqueue *HisQueue, int varcnt); void ReInitHistory(hisqueue *HisQueue, int varcnt);
void AddNode(hisqueue *HisQueue, int varstart, DdNode *node, double dvalue, int ivalue, void *dynvalue); void AddNode(hisqueue *HisQueue, int varstart, DdNode *node, double dvalue,
hisnode* GetNode(hisqueue *HisQueue, int varstart, DdNode *node); int ivalue, void *dynvalue);
hisnode *GetNode(hisqueue *HisQueue, int varstart, DdNode *node);
int GetNodeIndex(hisqueue *HisQueue, int varstart, DdNode *node); int GetNodeIndex(hisqueue *HisQueue, int varstart, DdNode *node);
void onlinetraverse(DdManager *manager, namedvars varmap, hisqueue *HisQueue, DdNode *bdd); void onlinetraverse(DdManager *manager, namedvars varmap, hisqueue *HisQueue,
DdNode *bdd);
/* Save-load */ /* Save-load */
bddfileheader ReadFileHeader(char *filename); bddfileheader ReadFileHeader(char *filename);
int CheckFileVersion(const char *version); int CheckFileVersion(const char *version);
DdNode * LoadNodeDump(DdManager *manager, namedvars varmap, FILE *inputfile); DdNode *LoadNodeDump(DdManager *manager, namedvars varmap, FILE *inputfile);
DdNode * LoadNodeRec(DdManager *manager, namedvars varmap, hisqueue *Nodes, FILE *inputfile, nodeline current); DdNode *LoadNodeRec(DdManager *manager, namedvars varmap, hisqueue *Nodes,
DdNode * GetIfExists(DdManager *manager, namedvars varmap, hisqueue *Nodes, char *varname, int nodenum); FILE *inputfile, nodeline current);
DdNode *GetIfExists(DdManager *manager, namedvars varmap, hisqueue *Nodes,
char *varname, int nodenum);
int SaveNodeDump(DdManager *manager, namedvars varmap, DdNode *bdd, char *filename); int SaveNodeDump(DdManager *manager, namedvars varmap, DdNode *bdd,
void SaveExpand(DdManager *manager, namedvars varmap, hisqueue *Nodes, DdNode *Current, FILE *outputfile); char *filename);
void SaveExpand(DdManager *manager, namedvars varmap, hisqueue *Nodes,
DdNode *Current, FILE *outputfile);
void ExpandNodes(hisqueue *Nodes, int index, int nodenum); void ExpandNodes(hisqueue *Nodes, int index, int nodenum);
/* Export */ /* Export */
int simpleBDDtoDot(DdManager *manager, DdNode *bdd, char *filename); int simpleBDDtoDot(DdManager *manager, DdNode *bdd, char *filename);
int simpleNamedBDDtoDot(DdManager *manager, namedvars varmap, DdNode *bdd, char *filename); int simpleNamedBDDtoDot(DdManager *manager, namedvars varmap, DdNode *bdd,
char *filename);

View File

@ -184,16 +184,15 @@
* * * *
\******************************************************************************/ \******************************************************************************/
#include "YapInterface.h"
#include <unistd.h> #include "cudd_config.h"
#include "pqueue.h"
#include <math.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <math.h>
#include <time.h> #include <time.h>
#include "pqueue.h" #include <unistd.h>
#include "config.h"
#include "cudd_config.h"
#if HAVE_CUDD_UTIL_H #if HAVE_CUDD_UTIL_H
#include <cudd/util.h> #include <cudd/util.h>
#elif HAVE_UTIL_H #elif HAVE_UTIL_H
@ -216,26 +215,26 @@
#include "general.h" #include "general.h"
#define IsHigh(manager, node) HIGH(manager) == node #define IsHigh(manager, node) HIGH(manager) == node
#define IsLow(manager, node) LOW(manager) == node #define IsLow(manager, node) LOW(manager) == node
#define HIGH(manager) Cudd_ReadOne(manager) #define HIGH(manager) Cudd_ReadOne(manager)
#define LOW(manager) Cudd_Not(Cudd_ReadOne(manager)) #define LOW(manager) Cudd_Not(Cudd_ReadOne(manager))
#define NOT(node) Cudd_Not(node) #define NOT(node) Cudd_Not(node)
#define GetIndex(node) Cudd_NodeReadIndex(node) #define GetIndex(node) Cudd_NodeReadIndex(node)
#define GetOrder(manager, node) Cudd_ReadPerm(manager, GetIndex(node)) #define GetOrder(manager, node) Cudd_ReadPerm(manager, GetIndex(node))
#define GetVar(manager, index) Cudd_bddIthVar(manager, index) #define GetVar(manager, index) Cudd_bddIthVar(manager, index)
#define NewVar(manager) Cudd_bddNewVar(manager) #define NewVar(manager) Cudd_bddNewVar(manager)
#define KillBDD(manager) Cudd_Quit(manager) #define KillBDD(manager) Cudd_Quit(manager)
#define GetVarCount(manager) Cudd_ReadSize(manager) #define GetVarCount(manager) Cudd_ReadSize(manager)
#define DEBUGON _debug = 1 #define DEBUGON _debug = 1
#define DEBUGOFF _debug = 0 #define DEBUGOFF _debug = 0
#define RAPIDLOADON _RapidLoad = 1 #define RAPIDLOADON _RapidLoad = 1
#define RAPIDLOADOFF _RapidLoad = 0 #define RAPIDLOADOFF _RapidLoad = 0
#define SETMAXBUFSIZE(size) _maxbufsize = size #define SETMAXBUFSIZE(size) _maxbufsize = size
#define BDDFILE_ERROR -1 #define BDDFILE_ERROR -1
#define BDDFILE_OTHER 0 #define BDDFILE_OTHER 0
#define BDDFILE_SCRIPT 1 #define BDDFILE_SCRIPT 1
#define BDDFILE_NODEDUMP 2 #define BDDFILE_NODEDUMP 2
extern int _RapidLoad; extern int _RapidLoad;
extern int _debug; extern int _debug;
@ -253,7 +252,7 @@ typedef struct _bddfileheader {
typedef struct _namedvars { typedef struct _namedvars {
int varcnt; int varcnt;
int varstart; int varstart;
char ** vars; char **vars;
int *loaded; int *loaded;
double *dvalue; double *dvalue;
int *ivalue; int *ivalue;
@ -265,7 +264,7 @@ typedef struct _namedvars {
typedef struct _hisnode { typedef struct _hisnode {
DdNode *key; DdNode *key;
double dvalue; double dvalue;
double dvalue2;// =0; //needed for expected counts double dvalue2; // =0; //needed for expected counts
int ivalue; int ivalue;
void *dynvalue; void *dynvalue;
} hisnode; } hisnode;
@ -286,27 +285,32 @@ typedef struct _nodeline {
/* Initialization */ /* Initialization */
DdManager* simpleBDDinit(int varcnt); DdManager *simpleBDDinit(int varcnt);
/* BDD Generation */ /* BDD Generation */
DdNode* D_BDDAnd(DdManager *manager, DdNode *bdd1, DdNode *bdd2); DdNode *D_BDDAnd(DdManager *manager, DdNode *bdd1, DdNode *bdd2);
DdNode* D_BDDNand(DdManager *manager, DdNode *bdd1, DdNode *bdd2); DdNode *D_BDDNand(DdManager *manager, DdNode *bdd1, DdNode *bdd2);
DdNode* D_BDDOr(DdManager *manager, DdNode *bdd1, DdNode *bdd2); DdNode *D_BDDOr(DdManager *manager, DdNode *bdd1, DdNode *bdd2);
DdNode* D_BDDNor(DdManager *manager, DdNode *bdd1, DdNode *bdd2); DdNode *D_BDDNor(DdManager *manager, DdNode *bdd1, DdNode *bdd2);
DdNode* D_BDDXor(DdManager *manager, DdNode *bdd1, DdNode *bdd2); DdNode *D_BDDXor(DdManager *manager, DdNode *bdd1, DdNode *bdd2);
DdNode* D_BDDXnor(DdManager *manager, DdNode *bdd1, DdNode *bdd2); DdNode *D_BDDXnor(DdManager *manager, DdNode *bdd1, DdNode *bdd2);
DdNode* FileGenerateBDD(DdManager *manager, namedvars varmap, bddfileheader fileheader); DdNode *FileGenerateBDD(DdManager *manager, namedvars varmap,
DdNode** FileGenerateBDDForest(DdManager *manager, namedvars varmap, bddfileheader fileheader); bddfileheader fileheader);
DdNode* OnlineGenerateBDD(DdManager *manager, namedvars *varmap); DdNode **FileGenerateBDDForest(DdManager *manager, namedvars varmap,
DdNode* LineParser(DdManager *manager, namedvars varmap, DdNode **inter, int maxinter, char *function, int iline); bddfileheader fileheader);
DdNode* OnlineLineParser(DdManager *manager, namedvars *varmap, DdNode **inter, int maxinter, char *function, int iline); DdNode *OnlineGenerateBDD(DdManager *manager, namedvars *varmap);
DdNode* BDD_Operator(DdManager *manager, DdNode *bdd1, DdNode *bdd2, char Operator, int inegoper); DdNode *LineParser(DdManager *manager, namedvars varmap, DdNode **inter,
int maxinter, char *function, int iline);
DdNode *OnlineLineParser(DdManager *manager, namedvars *varmap, DdNode **inter,
int maxinter, char *function, int iline);
DdNode *BDD_Operator(DdManager *manager, DdNode *bdd1, DdNode *bdd2,
char Operator, int inegoper);
int getInterBDD(char *function); int getInterBDD(char *function);
char* getFileName(const char *function); char *getFileName(const char *function);
int GetParam(char *inputline, int iParam); int GetParam(char *inputline, int iParam);
char** GetVariableOrder(char *filename, int varcnt); char **GetVariableOrder(char *filename, int varcnt);
int LoadVariableData(namedvars varmap, char *filename); int LoadVariableData(namedvars varmap, char *filename);
/* Named variables */ /* Named variables */
@ -317,43 +321,53 @@ namedvars InitNamedVars(int varcnt, int varstart);
void EnlargeNamedVars(namedvars *varmap, int newvarcnt); void EnlargeNamedVars(namedvars *varmap, int newvarcnt);
int AddNamedVarAt(namedvars varmap, const char *varname, int index); int AddNamedVarAt(namedvars varmap, const char *varname, int index);
int AddNamedVar(namedvars varmap, const char *varname); int AddNamedVar(namedvars varmap, const char *varname);
void SetNamedVarValuesAt(namedvars varmap, int index, double dvalue, int ivalue, void *dynvalue); void SetNamedVarValuesAt(namedvars varmap, int index, double dvalue, int ivalue,
int SetNamedVarValues(namedvars varmap, const char *varname, double dvalue, int ivalue, void *dynvalue); void *dynvalue);
int SetNamedVarValues(namedvars varmap, const char *varname, double dvalue,
int ivalue, void *dynvalue);
int GetNamedVarIndex(const namedvars varmap, const char *varname); int GetNamedVarIndex(const namedvars varmap, const char *varname);
int RepairVarcnt(namedvars *varmap); int RepairVarcnt(namedvars *varmap);
const char* GetNodeVarName(DdManager *manager, namedvars varmap, DdNode *node); const char *GetNodeVarName(DdManager *manager, namedvars varmap, DdNode *node);
const char* GetNodeVarNameDisp(DdManager *manager, namedvars varmap, DdNode *node); const char *GetNodeVarNameDisp(DdManager *manager, namedvars varmap,
DdNode *node);
int all_loaded(namedvars varmap, int disp); int all_loaded(namedvars varmap, int disp);
int all_loaded_for_deterministic_variables(namedvars varmap, int disp); int all_loaded_for_deterministic_variables(namedvars varmap, int disp);
/* Traversal */ /* Traversal */
DdNode* HighNodeOf(DdManager *manager, DdNode *node); DdNode *HighNodeOf(DdManager *manager, DdNode *node);
DdNode* LowNodeOf(DdManager *manager, DdNode *node); DdNode *LowNodeOf(DdManager *manager, DdNode *node);
/* Traversal - History */ /* Traversal - History */
hisqueue* InitHistory(int varcnt); hisqueue *InitHistory(int varcnt);
void ReInitHistory(hisqueue *HisQueue, int varcnt); void ReInitHistory(hisqueue *HisQueue, int varcnt);
void AddNode(hisqueue *HisQueue, int varstart, DdNode *node, double dvalue, int ivalue, void *dynvalue); void AddNode(hisqueue *HisQueue, int varstart, DdNode *node, double dvalue,
hisnode* GetNode(hisqueue *HisQueue, int varstart, DdNode *node); int ivalue, void *dynvalue);
hisnode *GetNode(hisqueue *HisQueue, int varstart, DdNode *node);
int GetNodeIndex(hisqueue *HisQueue, int varstart, DdNode *node); int GetNodeIndex(hisqueue *HisQueue, int varstart, DdNode *node);
void onlinetraverse(DdManager *manager, namedvars varmap, hisqueue *HisQueue, DdNode *bdd); void onlinetraverse(DdManager *manager, namedvars varmap, hisqueue *HisQueue,
DdNode *bdd);
/* Save-load */ /* Save-load */
bddfileheader ReadFileHeader(char *filename); bddfileheader ReadFileHeader(char *filename);
int CheckFileVersion(const char *version); int CheckFileVersion(const char *version);
DdNode * LoadNodeDump(DdManager *manager, namedvars varmap, FILE *inputfile); DdNode *LoadNodeDump(DdManager *manager, namedvars varmap, FILE *inputfile);
DdNode * LoadNodeRec(DdManager *manager, namedvars varmap, hisqueue *Nodes, FILE *inputfile, nodeline current); DdNode *LoadNodeRec(DdManager *manager, namedvars varmap, hisqueue *Nodes,
DdNode * GetIfExists(DdManager *manager, namedvars varmap, hisqueue *Nodes, char *varname, int nodenum); FILE *inputfile, nodeline current);
DdNode *GetIfExists(DdManager *manager, namedvars varmap, hisqueue *Nodes,
char *varname, int nodenum);
int SaveNodeDump(DdManager *manager, namedvars varmap, DdNode *bdd, char *filename); int SaveNodeDump(DdManager *manager, namedvars varmap, DdNode *bdd,
void SaveExpand(DdManager *manager, namedvars varmap, hisqueue *Nodes, DdNode *Current, FILE *outputfile); char *filename);
void SaveExpand(DdManager *manager, namedvars varmap, hisqueue *Nodes,
DdNode *Current, FILE *outputfile);
void ExpandNodes(hisqueue *Nodes, int index, int nodenum); void ExpandNodes(hisqueue *Nodes, int index, int nodenum);
/* Export */ /* Export */
int simpleBDDtoDot(DdManager *manager, DdNode *bdd, const char *filename); int simpleBDDtoDot(DdManager *manager, DdNode *bdd, const char *filename);
int simpleNamedBDDtoDot(DdManager *manager, namedvars varmap, DdNode *bdd, const char *filename); int simpleNamedBDDtoDot(DdManager *manager, namedvars varmap, DdNode *bdd,
const char *filename);

View File

@ -190,7 +190,7 @@ form
variable=value variable=value
*/ */
#include "config.h" #include "YapInterface.h"
#include "cudd_config.h" #include "cudd_config.h"
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>

View File

@ -7,7 +7,6 @@ This package uses the library cudd, see http://vlsi.colorado.edu/~fabio/CUDD/
for the relative license. for the relative license.
*/ */
#include "config.h"
#include "cudd_config.h" #include "cudd_config.h"
#include <stdio.h> #include <stdio.h>
#if HAVE_CUDDINT_H #if HAVE_CUDDINT_H
@ -18,7 +17,9 @@ for the relative license.
#include "YapInterface.h" #include "YapInterface.h"
typedef struct { int var, value; } factor; typedef struct {
int var, value;
} factor;
typedef struct { typedef struct {
int nFact; int nFact;

View File

@ -9,7 +9,6 @@ for the relative license.
*/ */
#include "config.h"
#include "cudd_config.h" #include "cudd_config.h"
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
@ -366,7 +365,7 @@ static YAP_Bool add_var(void) {
probTerm = arg2; probTerm = arg2;
p0 = 1; p0 = 1;
for (i = 0; i < v->nVal - 1; i++) { for (i = 0; i < v->nVal - 1; i++) {
//node = Cudd_bddIthVar(mgr_ex[ex], boolVars_ex[ex] + i); // node = Cudd_bddIthVar(mgr_ex[ex], boolVars_ex[ex] + i);
p = YAP_FloatOfTerm(YAP_HeadOfTerm(probTerm)); p = YAP_FloatOfTerm(YAP_HeadOfTerm(probTerm));
bVar2mVar_ex[ex][boolVars_ex[ex] + i] = nVars_ex[ex] - 1; bVar2mVar_ex[ex][boolVars_ex[ex] + i] = nVars_ex[ex] - 1;
probs_ex[ex][boolVars_ex[ex] + i] = p / p0; probs_ex[ex][boolVars_ex[ex] + i] = p / p0;

View File

@ -1,23 +1,22 @@
#ifndef __MYDDAS_H__ #ifndef __MYDDAS_H__
#define __MYDDAS_H__ #define __MYDDAS_H__
#include "config.h" #include "YapConfig.h"
#include <stdio.h> #include <stdio.h>
#include "myddas_types.h" #include "myddas_types.h"
/* Passar para o myddas_statictics.h ???????? */ /* Passar para o myddas_statictics.h ???????? */
#ifdef MYDDAS_STATS #ifdef MYDDAS_STATS
#include <time.h>
#include <sys/time.h> #include <sys/time.h>
#include <time.h>
#endif #endif
typedef struct myddas_global *MYDDAS_GLOBAL; typedef struct myddas_global *MYDDAS_GLOBAL;
#include "myddas_util.h" #include "myddas_util.h"
// extern void Yap_InitMYDDAS_SQLITE3Preds(void);
//extern void Yap_InitMYDDAS_SQLITE3Preds(void); // extern void Yap_InitBackMYDDAS_SQLITE3Preds(void);
//extern void Yap_InitBackMYDDAS_SQLITE3Preds(void);
#ifdef MYDDAS_STATS #ifdef MYDDAS_STATS
typedef struct myddas_stats_time_struct *MYDDAS_STATS_TIME; typedef struct myddas_stats_time_struct *MYDDAS_STATS_TIME;
@ -28,18 +27,17 @@ typedef void *MYDDAS_STATS_TIME;
#endif #endif
#ifdef DEBUG #ifdef DEBUG
#define MYDDAS_MEMORY_MALLOC_NR(NUMBER) \ #define MYDDAS_MEMORY_MALLOC_NR(NUMBER) \
NUMBER = Yap_REGS.MYDDAS_GLOBAL_POINTER->malloc_called; NUMBER = Yap_REGS.MYDDAS_GLOBAL_POINTER->malloc_called;
#define MYDDAS_MEMORY_MALLOC_SIZE(NUMBER) \ #define MYDDAS_MEMORY_MALLOC_SIZE(NUMBER) \
NUMBER = Yap_REGS.MYDDAS_GLOBAL_POINTER->memory_allocated; NUMBER = Yap_REGS.MYDDAS_GLOBAL_POINTER->memory_allocated;
#define MYDDAS_MEMORY_FREE_NR(NUMBER) \ #define MYDDAS_MEMORY_FREE_NR(NUMBER) \
NUMBER = Yap_REGS.MYDDAS_GLOBAL_POINTER->free_called; NUMBER = Yap_REGS.MYDDAS_GLOBAL_POINTER->free_called;
#define MYDDAS_MEMORY_FREE_SIZE(NUMBER) \ #define MYDDAS_MEMORY_FREE_SIZE(NUMBER) \
NUMBER = Yap_REGS.MYDDAS_GLOBAL_POINTER->memory_freed; NUMBER = Yap_REGS.MYDDAS_GLOBAL_POINTER->memory_freed;
#endif #endif
#include "myddas_structs.h"
#include "MyddasProto.h" #include "MyddasProto.h"
#include "myddas_structs.h"
#endif /*__MYDDAS_H__*/ #endif /*__MYDDAS_H__*/

File diff suppressed because it is too large Load Diff

View File

@ -68,9 +68,9 @@ static lbfgsfloatval_t evaluate(void *instance, const lbfgsfloatval_t *x,
} }
YAP_Term o; YAP_Term o;
if (YAP_IsIntTerm((o = YAP_GetFromSlot(sl)))) if (YAP_IsIntTerm((o = YAP_GetFromSlot(sl))))
rc = YAP_IntOfTerm(o); rc = YAP_IntOfTerm(o);
else else
rc = YAP_FloatOfTerm(o); rc = YAP_FloatOfTerm(o);
YAP_RecoverSlots(1, sl); YAP_RecoverSlots(1, sl);
return rc; return rc;
} }
@ -350,7 +350,7 @@ static YAP_Bool lbfgs_release(void) {
/* return FALSE; */ /* return FALSE; */
} }
static lbfgs_parameter_t * get_params(YAP_Term t) { static lbfgs_parameter_t *get_params(YAP_Term t) {
YAP_Int ar = YAP_ArityOfFunctor(YAP_FunctorOfTerm(t)); YAP_Int ar = YAP_ArityOfFunctor(YAP_FunctorOfTerm(t));
YAP_Term arg = YAP_ArgOfTerm(ar, t); YAP_Term arg = YAP_ArgOfTerm(ar, t);
return (lbfgs_parameter_t *)YAP_IntOfTerm(arg); return (lbfgs_parameter_t *)YAP_IntOfTerm(arg);
@ -516,7 +516,6 @@ static YAP_Bool lbfgs_set_parameter(void) {
return TRUE; return TRUE;
} }
/** @pred lbfgs_get_parameter(+Name,-Value)</h3> /** @pred lbfgs_get_parameter(+Name,-Value)</h3>
Get the current Value for Name Get the current Value for Name
*/ */

View File

@ -144,7 +144,7 @@ listing(Stream, [MV|MVs]) :- !,
'$listing'(_,_,_,_). '$listing'(_,_,_,_).
'$funcspec'(Name/Arity,Name,Arity) :- !, atom(Name). '$funcspec'(Name/Arity,Name,Arity) :- !, atom(Name).
'$funcspec'(Name,Name,_) :- atom(Name), !. '$funcspec'(Name,Name,0) :- atom(Name), !.
'$funcspec'(Name,_,_) :- '$funcspec'(Name,_,_) :-
'$do_error'(domain_error(predicate_spec,Name),listing(Name)). '$do_error'(domain_error(predicate_spec,Name),listing(Name)).
@ -216,7 +216,7 @@ listing(Stream, [MV|MVs]) :- !,
'$clause'(Pred, M, Body, _), '$clause'(Pred, M, Body, _),
'$current_module'(Mod), '$current_module'(Mod),
( M \= Mod -> H = M:Pred ; H = Pred ), ( M \= Mod -> H = M:Pred ; H = Pred ),
'$portray_clause'(Stream,(H:-Body)), portray_clause(Stream,(H:-Body)),
fail. fail.
/** @pred portray_clause(+ _S_,+ _C_) /** @pred portray_clause(+ _S_,+ _C_)