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>

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,10 +60,8 @@ 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: case DLL_PROCESS_ATTACH:
break; break;
case DLL_PROCESS_DETACH: case DLL_PROCESS_DETACH:

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

@ -40,6 +40,8 @@
#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;
@ -97,7 +99,7 @@ typedef struct {
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,25 +5,21 @@
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 */
/* --------------------------- */ /* --------------------------- */
@ -44,7 +40,7 @@
#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
@ -53,8 +49,6 @@
#define BASE_AUXILIARY_TERM_STACK_SIZE 100000 #define BASE_AUXILIARY_TERM_STACK_SIZE 100000
/* --------------------------- */ /* --------------------------- */
/* Structs */ /* Structs */
/* --------------------------- */ /* --------------------------- */
@ -71,7 +65,7 @@ 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)
@ -89,7 +83,7 @@ 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)
@ -98,15 +92,16 @@ typedef struct trie_node {
#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)
@ -119,9 +114,8 @@ typedef struct trie_hash {
#define SIZEOF_TR_HASH sizeof(TYPE_TR_HASH) #define SIZEOF_TR_HASH sizeof(TYPE_TR_HASH)
#define SIZEOF_TR_BUCKET sizeof(TYPE_TR_NODE *) #define SIZEOF_TR_BUCKET sizeof(TYPE_TR_NODE *)
#define AS_TR_NODE_NEXT(ADDR) (TrNode)((YAP_UInt)(ADDR) - 2 * sizeof(struct trie_node *)) #define AS_TR_NODE_NEXT(ADDR) \
(TrNode)((YAP_UInt)(ADDR)-2 * sizeof(struct trie_node *))
/* --------------------------- */ /* --------------------------- */
/* Macros */ /* Macros */
@ -129,12 +123,15 @@ typedef struct trie_hash {
#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) \
TrNode_child(TR_NODE) = (TrNode)TAG_ADDR(DATA)
#define GET_DATA_FROM_LEAF_TRIE_NODE(TR_NODE) UNTAG_ADDR(TrNode_child(TR_NODE)) #define GET_DATA_FROM_LEAF_TRIE_NODE(TR_NODE) UNTAG_ADDR(TrNode_child(TR_NODE))
#define MARK_AS_LEAF_TRIE_NODE(TR_NODE) PUT_DATA_IN_LEAF_TRIE_NODE(TR_NODE, TrNode_child(TR_NODE)) #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 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) \
((YAP_Term *)(TERM) > STACK && (YAP_Term *)(TERM) <= STACK_BASE)
#define MkTrieVar(INDEX) ((INDEX) << 4) #define MkTrieVar(INDEX) ((INDEX) << 4)
#define TrieVarIndex(TERM) ((TERM) >> 4) #define TrieVarIndex(TERM) ((TERM) >> 4)
@ -144,17 +141,20 @@ typedef struct trie_hash {
#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) { \ { \
if (STACK < STACK_TOP) { \
fprintf(stderr, "**************************************\n"); \ fprintf(stderr, "**************************************\n"); \
fprintf(stderr, " Tries core module: term stack full\n"); \ fprintf(stderr, " Tries core module: term stack full\n"); \
fprintf(stderr, "**************************************\n"); \ fprintf(stderr, "**************************************\n"); \
@ -163,7 +163,8 @@ typedef struct trie_hash {
STACK--; \ STACK--; \
} }
#define PUSH_DOWN(STACK, ITEM, STACK_TOP) \ #define PUSH_DOWN(STACK, ITEM, STACK_TOP) \
{ if (STACK > STACK_TOP) { \ { \
if (STACK > STACK_TOP) { \
fprintf(stderr, "**************************************\n"); \ fprintf(stderr, "**************************************\n"); \
fprintf(stderr, " Tries core module: term stack empty\n"); \ fprintf(stderr, " Tries core module: term stack empty\n"); \
fprintf(stderr, "**************************************\n"); \ fprintf(stderr, "**************************************\n"); \
@ -172,12 +173,11 @@ typedef struct trie_hash {
STACK++; \ STACK++; \
} }
#define new_struct(STR, STR_TYPE, STR_SIZE) \ #define new_struct(STR, STR_TYPE, STR_SIZE) \
STR = (STR_TYPE *) YAP_AllocSpaceFromYap(STR_SIZE) STR = (STR_TYPE *)YAP_AllocSpaceFromYap(STR_SIZE)
#define new_trie_engine(TR_ENGINE) \ #define new_trie_engine(TR_ENGINE) \
{ new_struct(TR_ENGINE, TYPE_TR_ENGINE, SIZEOF_TR_ENGINE); \ { \
new_struct(TR_ENGINE, TYPE_TR_ENGINE, SIZEOF_TR_ENGINE); \
TrEngine_trie(TR_ENGINE) = NULL; \ TrEngine_trie(TR_ENGINE) = NULL; \
TrEngine_memory(TR_ENGINE) = 0; \ TrEngine_memory(TR_ENGINE) = 0; \
TrEngine_tries(TR_ENGINE) = 0; \ TrEngine_tries(TR_ENGINE) = 0; \
@ -189,7 +189,8 @@ typedef struct trie_hash {
TrEngine_nodes_max(TR_ENGINE) = 0; \ TrEngine_nodes_max(TR_ENGINE) = 0; \
} }
#define new_trie_node(TR_NODE, ENTRY, PARENT, CHILD, NEXT, PREVIOUS) \ #define new_trie_node(TR_NODE, ENTRY, PARENT, CHILD, NEXT, PREVIOUS) \
{ new_struct(TR_NODE, TYPE_TR_NODE, SIZEOF_TR_NODE); \ { \
new_struct(TR_NODE, TYPE_TR_NODE, SIZEOF_TR_NODE); \
TrNode_entry(TR_NODE) = ENTRY; \ TrNode_entry(TR_NODE) = ENTRY; \
TrNode_parent(TR_NODE) = PARENT; \ TrNode_parent(TR_NODE) = PARENT; \
TrNode_child(TR_NODE) = CHILD; \ TrNode_child(TR_NODE) = CHILD; \
@ -199,7 +200,8 @@ typedef struct trie_hash {
INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_NODE); \ INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_NODE); \
} }
#define new_trie_hash(TR_HASH, NUM_NODES, NUM_BUCKETS) \ #define new_trie_hash(TR_HASH, NUM_NODES, NUM_BUCKETS) \
{ new_struct(TR_HASH, TYPE_TR_HASH, SIZEOF_TR_HASH); \ { \
new_struct(TR_HASH, TYPE_TR_HASH, SIZEOF_TR_HASH); \
TrHash_mark(TR_HASH) = NULL; \ TrHash_mark(TR_HASH) = NULL; \
TrHash_num_buckets(TR_HASH) = NUM_BUCKETS; \ TrHash_num_buckets(TR_HASH) = NUM_BUCKETS; \
new_hash_buckets(TR_HASH, NUM_BUCKETS); \ new_hash_buckets(TR_HASH, NUM_BUCKETS); \
@ -207,18 +209,19 @@ typedef struct trie_hash {
INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_HASH); \ INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_HASH); \
} }
#define new_hash_buckets(TR_HASH, NUM_BUCKETS) \ #define new_hash_buckets(TR_HASH, NUM_BUCKETS) \
{ int i; void **ptr; \ { \
int i; \
void **ptr; \
new_struct(ptr, void *, NUM_BUCKETS * sizeof(void *)); \ new_struct(ptr, void *, NUM_BUCKETS * sizeof(void *)); \
TrHash_buckets(TR_HASH) = (TYPE_TR_NODE **) ptr; \ TrHash_buckets(TR_HASH) = (TYPE_TR_NODE **)ptr; \
for (i = NUM_BUCKETS; i != 0; i--) \ for (i = NUM_BUCKETS; i != 0; i--) \
*ptr++ = NULL; \ *ptr++ = NULL; \
INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, (NUM_BUCKETS) * SIZEOF_TR_BUCKET); \ INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, (NUM_BUCKETS)*SIZEOF_TR_BUCKET); \
} }
#define expand_auxiliary_term_stack() \ #define expand_auxiliary_term_stack() \
{ YAP_Term *aux_stack; \ { \
YAP_Term *aux_stack; \
YAP_Int aux_size = CURRENT_AUXILIARY_TERM_STACK_SIZE * sizeof(YAP_Term); \ YAP_Int aux_size = CURRENT_AUXILIARY_TERM_STACK_SIZE * sizeof(YAP_Term); \
new_struct(aux_stack, YAP_Term, aux_size * 2); \ new_struct(aux_stack, YAP_Term, aux_size * 2); \
memmove(aux_stack, AUXILIARY_TERM_STACK, aux_size); \ memmove(aux_stack, AUXILIARY_TERM_STACK, aux_size); \
@ -227,87 +230,101 @@ typedef struct trie_hash {
CURRENT_AUXILIARY_TERM_STACK_SIZE *= 2; \ CURRENT_AUXILIARY_TERM_STACK_SIZE *= 2; \
} }
#define free_struct(STR) YAP_FreeSpaceFromYap((char *)(STR))
#define free_struct(STR) \
YAP_FreeSpaceFromYap((char *) (STR))
#define free_trie_node(STR) \ #define free_trie_node(STR) \
{ free_struct(STR); \ { \
free_struct(STR); \
DECREMENT_NODES(CURRENT_TRIE_ENGINE); \ DECREMENT_NODES(CURRENT_TRIE_ENGINE); \
DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_NODE); \ DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_NODE); \
} }
#define free_trie_hash(STR) \ #define free_trie_hash(STR) \
{ free_struct(STR); \ { \
free_struct(STR); \
DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_HASH); \ DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_HASH); \
} }
#define free_hash_buckets(STR, NUM_BUCKETS) \ #define free_hash_buckets(STR, NUM_BUCKETS) \
{ free_struct(STR); \ { \
DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, (NUM_BUCKETS) * SIZEOF_TR_BUCKET); \ free_struct(STR); \
DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, (NUM_BUCKETS)*SIZEOF_TR_BUCKET); \
} }
#define INCREMENT_MEMORY(TR_ENGINE, SIZE) \ #define INCREMENT_MEMORY(TR_ENGINE, SIZE) \
{ TrEngine_memory(TR_ENGINE) += SIZE; \ { \
TrEngine_memory(TR_ENGINE) += SIZE; \
if (TrEngine_memory(TR_ENGINE) > TrEngine_memory_max(TR_ENGINE)) \ if (TrEngine_memory(TR_ENGINE) > TrEngine_memory_max(TR_ENGINE)) \
TrEngine_memory_max(TR_ENGINE) = TrEngine_memory(TR_ENGINE); \ TrEngine_memory_max(TR_ENGINE) = TrEngine_memory(TR_ENGINE); \
} }
#define INCREMENT_TRIES(TR_ENGINE) \ #define INCREMENT_TRIES(TR_ENGINE) \
{ TrEngine_tries(TR_ENGINE)++; \ { \
TrEngine_tries(TR_ENGINE)++; \
if (TrEngine_tries(TR_ENGINE) > TrEngine_tries_max(TR_ENGINE)) \ if (TrEngine_tries(TR_ENGINE) > TrEngine_tries_max(TR_ENGINE)) \
TrEngine_tries_max(TR_ENGINE) = TrEngine_tries(TR_ENGINE); \ TrEngine_tries_max(TR_ENGINE) = TrEngine_tries(TR_ENGINE); \
} }
#define INCREMENT_ENTRIES(TR_ENGINE) \ #define INCREMENT_ENTRIES(TR_ENGINE) \
{ TrEngine_entries(TR_ENGINE)++; \ { \
TrEngine_entries(TR_ENGINE)++; \
if (TrEngine_entries(TR_ENGINE) > TrEngine_entries_max(TR_ENGINE)) \ if (TrEngine_entries(TR_ENGINE) > TrEngine_entries_max(TR_ENGINE)) \
TrEngine_entries_max(TR_ENGINE) = TrEngine_entries(TR_ENGINE); \ TrEngine_entries_max(TR_ENGINE) = TrEngine_entries(TR_ENGINE); \
} }
#define INCREMENT_NODES(TR_ENGINE) \ #define INCREMENT_NODES(TR_ENGINE) \
{ TrEngine_nodes(TR_ENGINE)++; \ { \
TrEngine_nodes(TR_ENGINE)++; \
if (TrEngine_nodes(TR_ENGINE) > TrEngine_nodes_max(TR_ENGINE)) \ if (TrEngine_nodes(TR_ENGINE) > TrEngine_nodes_max(TR_ENGINE)) \
TrEngine_nodes_max(TR_ENGINE) = TrEngine_nodes(TR_ENGINE); \ TrEngine_nodes_max(TR_ENGINE) = TrEngine_nodes(TR_ENGINE); \
} }
#define DECREMENT_MEMORY(TR_ENGINE, SIZE) \ #define DECREMENT_MEMORY(TR_ENGINE, SIZE) TrEngine_memory(TR_ENGINE) -= SIZE
TrEngine_memory(TR_ENGINE) -= SIZE #define DECREMENT_TRIES(TR_ENGINE) TrEngine_tries(TR_ENGINE)--
#define DECREMENT_TRIES(TR_ENGINE) \ #define DECREMENT_ENTRIES(TR_ENGINE) TrEngine_entries(TR_ENGINE)--
TrEngine_tries(TR_ENGINE)-- #define DECREMENT_NODES(TR_ENGINE) TrEngine_nodes(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 IS_FUNCTOR_NODE(N) (((ApplTag & TrNode_entry(N)) == ApplTag) && \ (((ApplTag & TrNode_entry(N)) == ApplTag) && \
(TrNode_entry(N) != PairInitTag) && \ (TrNode_entry(N) != PairInitTag) && (TrNode_entry(N) != PairEndEmptyTag) && \
(TrNode_entry(N) != PairEndEmptyTag) && \
(TrNode_entry(N) != PairEndTermTag)) (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_close_all(TrEngine engine,
void (*destruct_function)(TrNode));
extern void core_trie_set_mode(YAP_Int mode); extern void core_trie_set_mode(YAP_Int mode);
extern YAP_Int core_trie_get_mode(void); 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_put_entry(TrEngine engine, TrNode node, YAP_Term entry,
YAP_Int *depth);
extern TrNode core_trie_check_entry(TrNode node, YAP_Term entry); 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,
void (*add_function)(TrNode, TrNode));
extern void core_trie_join(TrEngine engine, TrNode node_dest,
TrNode node_source,
void (*add_function)(TrNode, TrNode),
void (*copy_function)(TrNode, TrNode));
extern void core_trie_intersect(TrEngine engine, TrNode node_dest,
TrNode node_source,
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_join(TrNode node1, TrNode node2);
extern YAP_Int core_trie_count_intersect(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 void core_trie_save(TrNode node, FILE *file,
extern TrNode core_trie_load(TrEngine engine, FILE *file, void (*load_function)(TrNode, YAP_Int, FILE *)); void (*save_function)(TrNode, FILE *));
extern void core_trie_stats(TrEngine engine, YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes); extern TrNode core_trie_load(TrEngine engine, FILE *file,
extern void core_trie_max_stats(TrEngine engine, YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes); void (*load_function)(TrNode, YAP_Int, FILE *));
extern void core_trie_usage(TrNode node, YAP_Int *entries, YAP_Int *nodes, YAP_Int *virtual_nodes); 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_trie_print(TrNode node, void (*print_function)(TrNode));
extern void core_disable_hash_table(void); extern void core_disable_hash_table(void);

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,15 +31,14 @@
#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. */
@ -51,18 +49,15 @@ struct data
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;
} }
@ -70,12 +65,11 @@ mem_write (void *c, const char *buf, int n)
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;
@ -84,8 +78,8 @@ mem_write (void *c, const char *buf, int n)
/* 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,81 +101,64 @@ 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) {
else if ((size_t) offset != offset)
{
errno = ENOSPC; errno = ENOSPC;
offset = -1; offset = -1;
} } else {
else if (cookie->pos < cookie->eof) {
{
if (cookie->pos < cookie->eof)
{
(*cookie->buf)[cookie->pos] = cookie->c; (*cookie->buf)[cookie->pos] = cookie->c;
cookie->c = '\0'; cookie->c = '\0';
} }
cookie->pos = offset; cookie->pos = offset;
if (cookie->pos < cookie->eof) if (cookie->pos < cookie->eof) {
{
cookie->c = (*cookie->buf)[cookie->pos]; cookie->c = (*cookie->buf)[cookie->pos];
(*cookie->buf)[cookie->pos] = '\0'; (*cookie->buf)[cookie->pos] = '\0';
*cookie->len = cookie->pos; *cookie->len = cookie->pos;
} } else
else
*cookie->len = cookie->eof; *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; return NULL;
} }
if (!(cookie = malloc (sizeof *cookie))) if (!(cookie = malloc(sizeof *cookie)))
return NULL; return NULL;
if (!(*buf = malloc (INITIAL_ALLOC))) if (!(*buf = malloc(INITIAL_ALLOC))) {
{ free(cookie);
free (cookie);
errno = ENOMEM; errno = ENOMEM;
return NULL; 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 {
else
{
cookie->buf = buf; cookie->buf = buf;
cookie->len = len; cookie->len = len;
cookie->pos = 0; cookie->pos = 0;

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>
@ -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
@ -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;
@ -38,8 +37,7 @@ typedef void *MYDDAS_STATS_TIME;
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__*/

View File

@ -61,20 +61,18 @@ distributing the effieicnt and explanatory implementation in an open source
licence. licence.
*/ */
#ifdef HAVE_CONFIG_H #include "YapConfig.h"
#include <config.h>
#endif/*HAVE_CONFIG_H*/
#include <math.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <math.h>
#include <lbfgs.h> #include <lbfgs.h>
#ifdef _MSC_VER #ifdef _MSC_VER
#define inline __inline #define inline __inline
#endif/*_MSC_VER*/ #endif /*_MSC_VER*/
#if defined(USE_SSE) && defined(__SSE2__) && LBFGS_FLOAT == 64 #if defined(USE_SSE) && defined(__SSE2__) && LBFGS_FLOAT == 64
/* Use SSE2 optimization for 64bit double precision. */ /* Use SSE2 optimization for 64bit double precision. */
@ -111,147 +109,86 @@ struct tag_iteration_data {
typedef struct tag_iteration_data iteration_data_t; typedef struct tag_iteration_data iteration_data_t;
static const lbfgs_parameter_t _defparam = { static const lbfgs_parameter_t _defparam = {
6, 1e-5, 0, 1e-5, 6, 1e-5, 0, 1e-5, 0, LBFGS_LINESEARCH_DEFAULT,
0, LBFGS_LINESEARCH_DEFAULT, 40, 40, 1e-20, 1e20, 1e-4, 0.9, 0.9,
1e-20, 1e20, 1e-4, 0.9, 0.9, 1.0e-16, 1.0e-16, 0.0, 0, -1,
0.0, 0, -1,
}; };
/* Forward function declarations. */ /* Forward function declarations. */
typedef int (*line_search_proc)( typedef int (*line_search_proc)(int n, lbfgsfloatval_t *x, lbfgsfloatval_t *f,
int n, lbfgsfloatval_t *g, lbfgsfloatval_t *s,
lbfgsfloatval_t *x, lbfgsfloatval_t *stp, const lbfgsfloatval_t *xp,
lbfgsfloatval_t *f, const lbfgsfloatval_t *gp, lbfgsfloatval_t *wa,
lbfgsfloatval_t *g,
lbfgsfloatval_t *s,
lbfgsfloatval_t *stp,
const lbfgsfloatval_t* xp,
const lbfgsfloatval_t* gp,
lbfgsfloatval_t *wa,
callback_data_t *cd, callback_data_t *cd,
const lbfgs_parameter_t *param const lbfgs_parameter_t *param);
);
static int line_search_backtracking( static int line_search_backtracking(int n, lbfgsfloatval_t *x,
int n, lbfgsfloatval_t *f, lbfgsfloatval_t *g,
lbfgsfloatval_t *x, lbfgsfloatval_t *s, lbfgsfloatval_t *stp,
lbfgsfloatval_t *f, const lbfgsfloatval_t *xp,
lbfgsfloatval_t *g, const lbfgsfloatval_t *gp,
lbfgsfloatval_t *s, lbfgsfloatval_t *wa, callback_data_t *cd,
lbfgsfloatval_t *stp, const lbfgs_parameter_t *param);
const lbfgsfloatval_t* xp,
const lbfgsfloatval_t* gp,
lbfgsfloatval_t *wa,
callback_data_t *cd,
const lbfgs_parameter_t *param
);
static int line_search_backtracking_owlqn( static int line_search_backtracking_owlqn(
int n, int n, lbfgsfloatval_t *x, lbfgsfloatval_t *f, lbfgsfloatval_t *g,
lbfgsfloatval_t *x, lbfgsfloatval_t *s, lbfgsfloatval_t *stp, const lbfgsfloatval_t *xp,
lbfgsfloatval_t *f, const lbfgsfloatval_t *gp, lbfgsfloatval_t *wp, callback_data_t *cd,
lbfgsfloatval_t *g, const lbfgs_parameter_t *param);
lbfgsfloatval_t *s,
lbfgsfloatval_t *stp,
const lbfgsfloatval_t* xp,
const lbfgsfloatval_t* gp,
lbfgsfloatval_t *wp,
callback_data_t *cd,
const lbfgs_parameter_t *param
);
static int line_search_morethuente( static int line_search_morethuente(int n, lbfgsfloatval_t *x,
int n, lbfgsfloatval_t *f, lbfgsfloatval_t *g,
lbfgsfloatval_t *x, lbfgsfloatval_t *s, lbfgsfloatval_t *stp,
lbfgsfloatval_t *f, const lbfgsfloatval_t *xp,
lbfgsfloatval_t *g, const lbfgsfloatval_t *gp,
lbfgsfloatval_t *s, lbfgsfloatval_t *wa, callback_data_t *cd,
lbfgsfloatval_t *stp, const lbfgs_parameter_t *param);
const lbfgsfloatval_t* xp,
const lbfgsfloatval_t* gp,
lbfgsfloatval_t *wa,
callback_data_t *cd,
const lbfgs_parameter_t *param
);
static int update_trial_interval( static int update_trial_interval(lbfgsfloatval_t *x, lbfgsfloatval_t *fx,
lbfgsfloatval_t *x, lbfgsfloatval_t *dx, lbfgsfloatval_t *y,
lbfgsfloatval_t *fx, lbfgsfloatval_t *fy, lbfgsfloatval_t *dy,
lbfgsfloatval_t *dx, lbfgsfloatval_t *t, lbfgsfloatval_t *ft,
lbfgsfloatval_t *y,
lbfgsfloatval_t *fy,
lbfgsfloatval_t *dy,
lbfgsfloatval_t *t,
lbfgsfloatval_t *ft,
lbfgsfloatval_t *dt, lbfgsfloatval_t *dt,
const lbfgsfloatval_t tmin, const lbfgsfloatval_t tmin,
const lbfgsfloatval_t tmax, const lbfgsfloatval_t tmax, int *brackt);
int *brackt
);
static lbfgsfloatval_t owlqn_x1norm( static lbfgsfloatval_t owlqn_x1norm(const lbfgsfloatval_t *x, const int start,
const lbfgsfloatval_t* x, const int n);
const int start,
const int n
);
static void owlqn_pseudo_gradient( static void owlqn_pseudo_gradient(lbfgsfloatval_t *pg, const lbfgsfloatval_t *x,
lbfgsfloatval_t* pg, const lbfgsfloatval_t *g, const int n,
const lbfgsfloatval_t* x, const lbfgsfloatval_t c, const int start,
const lbfgsfloatval_t* g, const int end);
const int n,
const lbfgsfloatval_t c,
const int start,
const int end
);
static void owlqn_project(
lbfgsfloatval_t* d,
const lbfgsfloatval_t* sign,
const int start,
const int end
);
static void owlqn_project(lbfgsfloatval_t *d, const lbfgsfloatval_t *sign,
const int start, const int end);
#if defined(USE_SSE) && (defined(__SSE__) || defined(__SSE2__)) #if defined(USE_SSE) && (defined(__SSE__) || defined(__SSE2__))
static int round_out_variables(int n) static int round_out_variables(int n) {
{
n += 7; n += 7;
n /= 8; n /= 8;
n *= 8; n *= 8;
return n; return n;
} }
#endif/*defined(USE_SSE)*/ #endif /*defined(USE_SSE)*/
lbfgsfloatval_t* lbfgs_malloc(int n) lbfgsfloatval_t *lbfgs_malloc(int n) {
{
#if defined(USE_SSE) && (defined(__SSE__) || defined(__SSE2__)) #if defined(USE_SSE) && (defined(__SSE__) || defined(__SSE2__))
n = round_out_variables(n); n = round_out_variables(n);
#endif/*defined(USE_SSE)*/ #endif /*defined(USE_SSE)*/
return (lbfgsfloatval_t*)vecalloc(sizeof(lbfgsfloatval_t) * n); return (lbfgsfloatval_t *)vecalloc(sizeof(lbfgsfloatval_t) * n);
} }
void lbfgs_free(lbfgsfloatval_t *x) void lbfgs_free(lbfgsfloatval_t *x) { vecfree(x); }
{
vecfree(x);
}
void lbfgs_parameter_init(lbfgs_parameter_t *param) void lbfgs_parameter_init(lbfgs_parameter_t *param) {
{
memmove(param, &_defparam, sizeof(*param)); memmove(param, &_defparam, sizeof(*param));
} }
int lbfgs( int lbfgs(int n, lbfgsfloatval_t *x, lbfgsfloatval_t *ptr_fx,
int n, lbfgs_evaluate_t proc_evaluate, lbfgs_progress_t proc_progress,
lbfgsfloatval_t *x, void *instance, lbfgs_parameter_t *_param) {
lbfgsfloatval_t *ptr_fx,
lbfgs_evaluate_t proc_evaluate,
lbfgs_progress_t proc_progress,
void *instance,
lbfgs_parameter_t *_param
)
{
int ret; int ret;
int i, j, k, ls, end, bound; int i, j, k, ls, end, bound;
lbfgsfloatval_t step; lbfgsfloatval_t step;
@ -280,7 +217,7 @@ int lbfgs(
#if defined(USE_SSE) && (defined(__SSE__) || defined(__SSE2__)) #if defined(USE_SSE) && (defined(__SSE__) || defined(__SSE2__))
/* Round out the number of variables. */ /* Round out the number of variables. */
n = round_out_variables(n); n = round_out_variables(n);
#endif/*defined(USE_SSE)*/ #endif /*defined(USE_SSE)*/
/* Check the input parameters for errors. */ /* Check the input parameters for errors. */
if (n <= 0) { if (n <= 0) {
@ -290,10 +227,10 @@ int lbfgs(
if (n % 8 != 0) { if (n % 8 != 0) {
return LBFGSERR_INVALID_N_SSE; return LBFGSERR_INVALID_N_SSE;
} }
if ((uintptr_t)(const void*)x % 16 != 0) { if ((uintptr_t)(const void *)x % 16 != 0) {
return LBFGSERR_INVALID_X_SSE; return LBFGSERR_INVALID_X_SSE;
} }
#endif/*defined(USE_SSE)*/ #endif /*defined(USE_SSE)*/
if (param.epsilon < 0.) { if (param.epsilon < 0.) {
return LBFGSERR_INVALID_EPSILON; return LBFGSERR_INVALID_EPSILON;
} }
@ -364,11 +301,11 @@ int lbfgs(
} }
/* Allocate working space. */ /* Allocate working space. */
xp = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t)); xp = (lbfgsfloatval_t *)vecalloc(n * sizeof(lbfgsfloatval_t));
g = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t)); g = (lbfgsfloatval_t *)vecalloc(n * sizeof(lbfgsfloatval_t));
gp = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t)); gp = (lbfgsfloatval_t *)vecalloc(n * sizeof(lbfgsfloatval_t));
d = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t)); d = (lbfgsfloatval_t *)vecalloc(n * sizeof(lbfgsfloatval_t));
w = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t)); w = (lbfgsfloatval_t *)vecalloc(n * sizeof(lbfgsfloatval_t));
if (xp == NULL || g == NULL || gp == NULL || d == NULL || w == NULL) { if (xp == NULL || g == NULL || gp == NULL || d == NULL || w == NULL) {
ret = LBFGSERR_OUTOFMEMORY; ret = LBFGSERR_OUTOFMEMORY;
goto lbfgs_exit; goto lbfgs_exit;
@ -376,7 +313,7 @@ int lbfgs(
if (param.orthantwise_c != 0.) { if (param.orthantwise_c != 0.) {
/* Allocate working space for OW-LQN. */ /* Allocate working space for OW-LQN. */
pg = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t)); pg = (lbfgsfloatval_t *)vecalloc(n * sizeof(lbfgsfloatval_t));
if (pg == NULL) { if (pg == NULL) {
ret = LBFGSERR_OUTOFMEMORY; ret = LBFGSERR_OUTOFMEMORY;
goto lbfgs_exit; goto lbfgs_exit;
@ -384,19 +321,19 @@ int lbfgs(
} }
/* Allocate limited memory storage. */ /* Allocate limited memory storage. */
lm = (iteration_data_t*)vecalloc(m * sizeof(iteration_data_t)); lm = (iteration_data_t *)vecalloc(m * sizeof(iteration_data_t));
if (lm == NULL) { if (lm == NULL) {
ret = LBFGSERR_OUTOFMEMORY; ret = LBFGSERR_OUTOFMEMORY;
goto lbfgs_exit; goto lbfgs_exit;
} }
/* Initialize the limited memory. */ /* Initialize the limited memory. */
for (i = 0;i < m;++i) { for (i = 0; i < m; ++i) {
it = &lm[i]; it = &lm[i];
it->alpha = 0; it->alpha = 0;
it->ys = 0; it->ys = 0;
it->s = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t)); it->s = (lbfgsfloatval_t *)vecalloc(n * sizeof(lbfgsfloatval_t));
it->y = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t)); it->y = (lbfgsfloatval_t *)vecalloc(n * sizeof(lbfgsfloatval_t));
if (it->s == NULL || it->y == NULL) { if (it->s == NULL || it->y == NULL) {
ret = LBFGSERR_OUTOFMEMORY; ret = LBFGSERR_OUTOFMEMORY;
goto lbfgs_exit; goto lbfgs_exit;
@ -405,7 +342,7 @@ int lbfgs(
/* Allocate an array for storing previous values of the objective function. */ /* Allocate an array for storing previous values of the objective function. */
if (0 < param.past) { if (0 < param.past) {
pf = (lbfgsfloatval_t*)vecalloc(param.past * sizeof(lbfgsfloatval_t)); pf = (lbfgsfloatval_t *)vecalloc(param.past * sizeof(lbfgsfloatval_t));
} }
/* Evaluate the function value and its gradient. */ /* Evaluate the function value and its gradient. */
@ -414,10 +351,8 @@ int lbfgs(
/* Compute the L1 norm of the variable and add it to the object value. */ /* Compute the L1 norm of the variable and add it to the object value. */
xnorm = owlqn_x1norm(x, param.orthantwise_start, param.orthantwise_end); xnorm = owlqn_x1norm(x, param.orthantwise_start, param.orthantwise_end);
fx += xnorm * param.orthantwise_c; fx += xnorm * param.orthantwise_c;
owlqn_pseudo_gradient( owlqn_pseudo_gradient(pg, x, g, n, param.orthantwise_c,
pg, x, g, n, param.orthantwise_start, param.orthantwise_end);
param.orthantwise_c, param.orthantwise_start, param.orthantwise_end
);
} }
/* Store the initial value of the objective function. */ /* Store the initial value of the objective function. */
@ -444,7 +379,8 @@ int lbfgs(
} else { } else {
vec2norm(&gnorm, pg, n); vec2norm(&gnorm, pg, n);
} }
if (xnorm < 1.0) xnorm = 1.0; if (xnorm < 1.0)
xnorm = 1.0;
if (gnorm / xnorm <= param.epsilon) { if (gnorm / xnorm <= param.epsilon) {
ret = LBFGS_ALREADY_MINIMIZED; ret = LBFGS_ALREADY_MINIMIZED;
goto lbfgs_exit; goto lbfgs_exit;
@ -467,10 +403,8 @@ int lbfgs(
ls = linesearch(n, x, &fx, g, d, &step, xp, gp, w, &cd, &param); ls = linesearch(n, x, &fx, g, d, &step, xp, gp, w, &cd, &param);
} else { } else {
ls = linesearch(n, x, &fx, g, d, &step, xp, pg, w, &cd, &param); ls = linesearch(n, x, &fx, g, d, &step, xp, pg, w, &cd, &param);
owlqn_pseudo_gradient( owlqn_pseudo_gradient(pg, x, g, n, param.orthantwise_c,
pg, x, g, n, param.orthantwise_start, param.orthantwise_end);
param.orthantwise_c, param.orthantwise_start, param.orthantwise_end
);
} }
if (ls < 0) { if (ls < 0) {
/* Revert to the previous point. */ /* Revert to the previous point. */
@ -490,7 +424,8 @@ int lbfgs(
/* Report the progress. */ /* Report the progress. */
if (cd.proc_progress) { if (cd.proc_progress) {
if ((ret = cd.proc_progress(cd.instance, x, g, fx, xnorm, gnorm, step, cd.n, k, ls))) { if ((ret = cd.proc_progress(cd.instance, x, g, fx, xnorm, gnorm, step,
cd.n, k, ls))) {
goto lbfgs_exit; goto lbfgs_exit;
} }
} }
@ -500,7 +435,8 @@ int lbfgs(
The criterion is given by the following formula: The criterion is given by the following formula:
|g(x)| / \max(1, |x|) < \epsilon |g(x)| / \max(1, |x|) < \epsilon
*/ */
if (xnorm < 1.0) xnorm = 1.0; if (xnorm < 1.0)
xnorm = 1.0;
if (gnorm / xnorm <= param.epsilon) { if (gnorm / xnorm <= param.epsilon) {
/* Convergence. */ /* Convergence. */
ret = LBFGS_SUCCESS; ret = LBFGS_SUCCESS;
@ -529,7 +465,7 @@ int lbfgs(
pf[k % param.past] = fx; pf[k % param.past] = fx;
} }
if (param.max_iterations != 0 && param.max_iterations < k+1) { if (param.max_iterations != 0 && param.max_iterations < k + 1) {
/* Maximum number of iterations. */ /* Maximum number of iterations. */
ret = LBFGSERR_MAXIMUMITERATION; ret = LBFGSERR_MAXIMUMITERATION;
break; break;
@ -548,7 +484,8 @@ int lbfgs(
Compute scalars ys and yy: Compute scalars ys and yy:
ys = y^t \cdot s = 1 / \rho. ys = y^t \cdot s = 1 / \rho.
yy = y^t \cdot y. yy = y^t \cdot y.
Notice that yy is used for scaling the hessian matrix H_0 (Cholesky factor). Notice that yy is used for scaling the hessian matrix H_0 (Cholesky
factor).
*/ */
vecdot(&ys, it->y, it->s, n); vecdot(&ys, it->y, it->s, n);
vecdot(&yy, it->y, it->y, n); vecdot(&yy, it->y, it->y, n);
@ -575,7 +512,7 @@ int lbfgs(
} }
j = end; j = end;
for (i = 0;i < bound;++i) { for (i = 0; i < bound; ++i) {
j = (j + m - 1) % m; /* if (--j == -1) j = m-1; */ j = (j + m - 1) % m; /* if (--j == -1) j = m-1; */
it = &lm[j]; it = &lm[j];
/* \alpha_{j} = \rho_{j} s^{t}_{j} \cdot q_{k+1}. */ /* \alpha_{j} = \rho_{j} s^{t}_{j} \cdot q_{k+1}. */
@ -587,7 +524,7 @@ int lbfgs(
vecscale(d, ys / yy, n); vecscale(d, ys / yy, n);
for (i = 0;i < bound;++i) { for (i = 0; i < bound; ++i) {
it = &lm[j]; it = &lm[j];
/* \beta_{j} = \rho_{j} y^t_{j} \cdot \gamma_{i}. */ /* \beta_{j} = \rho_{j} y^t_{j} \cdot \gamma_{i}. */
vecdot(&beta, it->y, d, n); vecdot(&beta, it->y, d, n);
@ -601,7 +538,7 @@ int lbfgs(
Constrain the search direction for orthant-wise updates. Constrain the search direction for orthant-wise updates.
*/ */
if (param.orthantwise_c != 0.) { if (param.orthantwise_c != 0.) {
for (i = param.orthantwise_start;i < param.orthantwise_end;++i) { for (i = param.orthantwise_start; i < param.orthantwise_end; ++i) {
if (d[i] * pg[i] >= 0) { if (d[i] * pg[i] >= 0) {
d[i] = 0; d[i] = 0;
} }
@ -624,7 +561,7 @@ lbfgs_exit:
/* Free memory blocks used by this function. */ /* Free memory blocks used by this function. */
if (lm != NULL) { if (lm != NULL) {
for (i = 0;i < m;++i) { for (i = 0; i < m; ++i) {
vecfree(lm[i].s); vecfree(lm[i].s);
vecfree(lm[i].y); vecfree(lm[i].y);
} }
@ -640,22 +577,13 @@ lbfgs_exit:
return ret; return ret;
} }
static int line_search_backtracking(int n, lbfgsfloatval_t *x,
lbfgsfloatval_t *f, lbfgsfloatval_t *g,
static int line_search_backtracking( lbfgsfloatval_t *s, lbfgsfloatval_t *stp,
int n, const lbfgsfloatval_t *xp,
lbfgsfloatval_t *x, const lbfgsfloatval_t *gp,
lbfgsfloatval_t *f, lbfgsfloatval_t *wp, callback_data_t *cd,
lbfgsfloatval_t *g, const lbfgs_parameter_t *param) {
lbfgsfloatval_t *s,
lbfgsfloatval_t *stp,
const lbfgsfloatval_t* xp,
const lbfgsfloatval_t* gp,
lbfgsfloatval_t *wp,
callback_data_t *cd,
const lbfgs_parameter_t *param
)
{
int count = 0; int count = 0;
lbfgsfloatval_t width, dg; lbfgsfloatval_t width, dg;
lbfgsfloatval_t finit, dginit = 0., dgtest; lbfgsfloatval_t finit, dginit = 0., dgtest;
@ -701,13 +629,13 @@ static int line_search_backtracking(
if (dg < param->wolfe * dginit) { if (dg < param->wolfe * dginit) {
width = inc; width = inc;
} else { } else {
if(param->linesearch == LBFGS_LINESEARCH_BACKTRACKING_WOLFE) { if (param->linesearch == LBFGS_LINESEARCH_BACKTRACKING_WOLFE) {
/* Exit with the regular Wolfe condition. */ /* Exit with the regular Wolfe condition. */
return count; return count;
} }
/* Check the strong Wolfe condition. */ /* Check the strong Wolfe condition. */
if(dg > -param->wolfe * dginit) { if (dg > -param->wolfe * dginit) {
width = dec; width = dec;
} else { } else {
/* Exit with the strong Wolfe condition. */ /* Exit with the strong Wolfe condition. */
@ -733,22 +661,11 @@ static int line_search_backtracking(
} }
} }
static int line_search_backtracking_owlqn( static int line_search_backtracking_owlqn(
int n, int n, lbfgsfloatval_t *x, lbfgsfloatval_t *f, lbfgsfloatval_t *g,
lbfgsfloatval_t *x, lbfgsfloatval_t *s, lbfgsfloatval_t *stp, const lbfgsfloatval_t *xp,
lbfgsfloatval_t *f, const lbfgsfloatval_t *gp, lbfgsfloatval_t *wp, callback_data_t *cd,
lbfgsfloatval_t *g, const lbfgs_parameter_t *param) {
lbfgsfloatval_t *s,
lbfgsfloatval_t *stp,
const lbfgsfloatval_t* xp,
const lbfgsfloatval_t* gp,
lbfgsfloatval_t *wp,
callback_data_t *cd,
const lbfgs_parameter_t *param
)
{
int i, count = 0; int i, count = 0;
lbfgsfloatval_t width = 0.5, norm = 0.; lbfgsfloatval_t width = 0.5, norm = 0.;
lbfgsfloatval_t finit = *f, dgtest; lbfgsfloatval_t finit = *f, dgtest;
@ -759,7 +676,7 @@ static int line_search_backtracking_owlqn(
} }
/* Choose the orthant for the new point. */ /* Choose the orthant for the new point. */
for (i = 0;i < n;++i) { for (i = 0; i < n; ++i) {
wp[i] = (xp[i] == 0.) ? -gp[i] : xp[i]; wp[i] = (xp[i] == 0.) ? -gp[i] : xp[i];
} }
@ -781,7 +698,7 @@ static int line_search_backtracking_owlqn(
++count; ++count;
dgtest = 0.; dgtest = 0.;
for (i = 0;i < n;++i) { for (i = 0; i < n; ++i) {
dgtest += (x[i] - xp[i]) * gp[i]; dgtest += (x[i] - xp[i]) * gp[i];
} }
@ -807,22 +724,13 @@ static int line_search_backtracking_owlqn(
} }
} }
static int line_search_morethuente(int n, lbfgsfloatval_t *x,
lbfgsfloatval_t *f, lbfgsfloatval_t *g,
static int line_search_morethuente( lbfgsfloatval_t *s, lbfgsfloatval_t *stp,
int n, const lbfgsfloatval_t *xp,
lbfgsfloatval_t *x, const lbfgsfloatval_t *gp,
lbfgsfloatval_t *f, lbfgsfloatval_t *wa, callback_data_t *cd,
lbfgsfloatval_t *g, const lbfgs_parameter_t *param) {
lbfgsfloatval_t *s,
lbfgsfloatval_t *stp,
const lbfgsfloatval_t* xp,
const lbfgsfloatval_t* gp,
lbfgsfloatval_t *wa,
callback_data_t *cd,
const lbfgs_parameter_t *param
)
{
int count = 0; int count = 0;
int brackt, stage1, uinfo = 0; int brackt, stage1, uinfo = 0;
lbfgsfloatval_t dg; lbfgsfloatval_t dg;
@ -881,14 +789,18 @@ static int line_search_morethuente(
} }
/* Clip the step in the range of [stpmin, stpmax]. */ /* Clip the step in the range of [stpmin, stpmax]. */
if (*stp < param->min_step) *stp = param->min_step; if (*stp < param->min_step)
if (param->max_step < *stp) *stp = param->max_step; *stp = param->min_step;
if (param->max_step < *stp)
*stp = param->max_step;
/* /*
If an unusual termination is to occur then let If an unusual termination is to occur then let
stp be the lowest point obtained so far. stp be the lowest point obtained so far.
*/ */
if ((brackt && ((*stp <= stmin || stmax <= *stp) || param->max_linesearch <= count + 1 || uinfo != 0)) || (brackt && (stmax - stmin <= param->xtol * stmax))) { if ((brackt && ((*stp <= stmin || stmax <= *stp) ||
param->max_linesearch <= count + 1 || uinfo != 0)) ||
(brackt && (stmax - stmin <= param->xtol * stmax))) {
*stp = stx; *stp = stx;
} }
@ -928,7 +840,8 @@ static int line_search_morethuente(
return LBFGSERR_MAXIMUMLINESEARCH; return LBFGSERR_MAXIMUMLINESEARCH;
} }
if (*f <= ftest1 && fabs(dg) <= param->gtol * (-dginit)) { if (*f <= ftest1 && fabs(dg) <= param->gtol * (-dginit)) {
/* The sufficient decrease condition and the directional derivative condition hold. */ /* The sufficient decrease condition and the directional derivative
* condition hold. */
return count; return count;
} }
@ -936,7 +849,8 @@ static int line_search_morethuente(
In the first stage we seek a step for which the modified In the first stage we seek a step for which the modified
function has a nonpositive value and nonnegative derivative. function has a nonpositive value and nonnegative derivative.
*/ */
if (stage1 && *f <= ftest1 && min2(param->ftol, param->gtol) * dginit <= dg) { if (stage1 && *f <= ftest1 &&
min2(param->ftol, param->gtol) * dginit <= dg) {
stage1 = 0; stage1 = 0;
} }
@ -960,12 +874,8 @@ static int line_search_morethuente(
Call update_trial_interval() to update the interval of Call update_trial_interval() to update the interval of
uncertainty and to compute the new step. uncertainty and to compute the new step.
*/ */
uinfo = update_trial_interval( uinfo = update_trial_interval(&stx, &fxm, &dgxm, &sty, &fym, &dgym, stp,
&stx, &fxm, &dgxm, &fm, &dgm, stmin, stmax, &brackt);
&sty, &fym, &dgym,
stp, &fm, &dgm,
stmin, stmax, &brackt
);
/* Reset the function and gradient values for f. */ /* Reset the function and gradient values for f. */
fx = fxm + stx * dgtest; fx = fxm + stx * dgtest;
@ -977,12 +887,8 @@ static int line_search_morethuente(
Call update_trial_interval() to update the interval of Call update_trial_interval() to update the interval of
uncertainty and to compute the new step. uncertainty and to compute the new step.
*/ */
uinfo = update_trial_interval( uinfo = update_trial_interval(&stx, &fx, &dgx, &sty, &fy, &dgy, stp, f,
&stx, &fx, &dgx, &dg, stmin, stmax, &brackt);
&sty, &fy, &dgy,
stp, f, &dg,
stmin, stmax, &brackt
);
} }
/* /*
@ -1000,13 +906,10 @@ static int line_search_morethuente(
return LBFGSERR_LOGICERROR; return LBFGSERR_LOGICERROR;
} }
/** /**
* Define the local variables for computing minimizers. * Define the local variables for computing minimizers.
*/ */
#define USES_MINIMIZER \ #define USES_MINIMIZER lbfgsfloatval_t a, d, gamma, theta, p, q, r, s;
lbfgsfloatval_t a, d, gamma, theta, p, q, r, s;
/** /**
* Find a minimizer of an interpolated cubic function. * Find a minimizer of an interpolated cubic function.
@ -1028,7 +931,8 @@ static int line_search_morethuente(
/* gamma = s*sqrt((theta/s)**2 - (du/s) * (dv/s)) */ \ /* gamma = s*sqrt((theta/s)**2 - (du/s) * (dv/s)) */ \
a = theta / s; \ a = theta / s; \
gamma = s * sqrt(a * a - ((du) / s) * ((dv) / s)); \ gamma = s * sqrt(a * a - ((du) / s) * ((dv) / s)); \
if ((v) < (u)) gamma = -gamma; \ if ((v) < (u)) \
gamma = -gamma; \
p = gamma - (du) + theta; \ p = gamma - (du) + theta; \
q = gamma - (du) + gamma + (dv); \ q = gamma - (du) + gamma + (dv); \
r = p / q; \ r = p / q; \
@ -1056,12 +960,13 @@ static int line_search_morethuente(
/* gamma = s*sqrt((theta/s)**2 - (du/s) * (dv/s)) */ \ /* gamma = s*sqrt((theta/s)**2 - (du/s) * (dv/s)) */ \
a = theta / s; \ a = theta / s; \
gamma = s * sqrt(max2(0, a * a - ((du) / s) * ((dv) / s))); \ gamma = s * sqrt(max2(0, a * a - ((du) / s) * ((dv) / s))); \
if ((u) < (v)) gamma = -gamma; \ if ((u) < (v)) \
gamma = -gamma; \
p = gamma - (dv) + theta; \ p = gamma - (dv) + theta; \
q = gamma - (dv) + gamma + (du); \ q = gamma - (dv) + gamma + (du); \
r = p / q; \ r = p / q; \
if (r < 0. && gamma != 0.) { \ if (r < 0. && gamma != 0.) { \
(cm) = (v) - r * d; \ (cm) = (v)-r * d; \
} else if (a < 0) { \ } else if (a < 0) { \
(cm) = (xmax); \ (cm) = (xmax); \
} else { \ } else { \
@ -1122,21 +1027,13 @@ static int line_search_morethuente(
* guaranteed sufficient decrease. ACM Transactions on Mathematical * guaranteed sufficient decrease. ACM Transactions on Mathematical
* Software (TOMS), Vol 20, No 3, pp. 286-307, 1994. * Software (TOMS), Vol 20, No 3, pp. 286-307, 1994.
*/ */
static int update_trial_interval( static int update_trial_interval(lbfgsfloatval_t *x, lbfgsfloatval_t *fx,
lbfgsfloatval_t *x, lbfgsfloatval_t *dx, lbfgsfloatval_t *y,
lbfgsfloatval_t *fx, lbfgsfloatval_t *fy, lbfgsfloatval_t *dy,
lbfgsfloatval_t *dx, lbfgsfloatval_t *t, lbfgsfloatval_t *ft,
lbfgsfloatval_t *y,
lbfgsfloatval_t *fy,
lbfgsfloatval_t *dy,
lbfgsfloatval_t *t,
lbfgsfloatval_t *ft,
lbfgsfloatval_t *dt, lbfgsfloatval_t *dt,
const lbfgsfloatval_t tmin, const lbfgsfloatval_t tmin,
const lbfgsfloatval_t tmax, const lbfgsfloatval_t tmax, int *brackt) {
int *brackt
)
{
int bound; int bound;
int dsign = fsigndiff(dt, dx); int dsign = fsigndiff(dt, dx);
lbfgsfloatval_t mc; /* minimizer of an interpolated cubic. */ lbfgsfloatval_t mc; /* minimizer of an interpolated cubic. */
@ -1270,8 +1167,10 @@ static int update_trial_interval(
} }
/* Clip the new trial value in [tmin, tmax]. */ /* Clip the new trial value in [tmin, tmax]. */
if (tmax < newt) newt = tmax; if (tmax < newt)
if (newt < tmin) newt = tmin; newt = tmax;
if (newt < tmin)
newt = tmin;
/* /*
Redefine the new trial value if it is close to the upper bound Redefine the new trial value if it is close to the upper bound
@ -1280,9 +1179,11 @@ static int update_trial_interval(
if (*brackt && bound) { if (*brackt && bound) {
mq = *x + 0.66 * (*y - *x); mq = *x + 0.66 * (*y - *x);
if (*x < *y) { if (*x < *y) {
if (mq < newt) newt = mq; if (mq < newt)
newt = mq;
} else { } else {
if (newt < mq) newt = mq; if (newt < mq)
newt = mq;
} }
} }
@ -1291,45 +1192,31 @@ static int update_trial_interval(
return 0; return 0;
} }
static lbfgsfloatval_t owlqn_x1norm(const lbfgsfloatval_t *x, const int start,
const int n) {
static lbfgsfloatval_t owlqn_x1norm(
const lbfgsfloatval_t* x,
const int start,
const int n
)
{
int i; int i;
lbfgsfloatval_t norm = 0.; lbfgsfloatval_t norm = 0.;
for (i = start;i < n;++i) { for (i = start; i < n; ++i) {
norm += fabs(x[i]); norm += fabs(x[i]);
} }
return norm; return norm;
} }
static void owlqn_pseudo_gradient( static void owlqn_pseudo_gradient(lbfgsfloatval_t *pg, const lbfgsfloatval_t *x,
lbfgsfloatval_t* pg, const lbfgsfloatval_t *g, const int n,
const lbfgsfloatval_t* x, const lbfgsfloatval_t c, const int start,
const lbfgsfloatval_t* g, const int end) {
const int n,
const lbfgsfloatval_t c,
const int start,
const int end
)
{
int i; int i;
/* Compute the negative of gradients. */ /* Compute the negative of gradients. */
for (i = 0;i < start;++i) { for (i = 0; i < start; ++i) {
pg[i] = g[i]; pg[i] = g[i];
} }
/* Compute the psuedo-gradients. */ /* Compute the psuedo-gradients. */
for (i = start;i < end;++i) { for (i = start; i < end; ++i) {
if (x[i] < 0.) { if (x[i] < 0.) {
/* Differentiable. */ /* Differentiable. */
pg[i] = g[i] - c; pg[i] = g[i] - c;
@ -1349,21 +1236,16 @@ static void owlqn_pseudo_gradient(
} }
} }
for (i = end;i < n;++i) { for (i = end; i < n; ++i) {
pg[i] = g[i]; pg[i] = g[i];
} }
} }
static void owlqn_project( static void owlqn_project(lbfgsfloatval_t *d, const lbfgsfloatval_t *sign,
lbfgsfloatval_t* d, const int start, const int end) {
const lbfgsfloatval_t* sign,
const int start,
const int end
)
{
int i; int i;
for (i = start;i < end;++i) { for (i = start; i < end; ++i) {
if (d[i] * sign[i] <= 0) { if (d[i] * sign[i] <= 0) {
d[i] = 0; d[i] = 0;
} }

View File

@ -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_)