listing
This commit is contained in:
parent
a1f8631844
commit
76cfa609c2
2072
C/utilpreds.c
2072
C/utilpreds.c
File diff suppressed because it is too large
Load Diff
23
CXX/yapi.hh
23
CXX/yapi.hh
@ -12,8 +12,8 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
extern "C"{
|
||||
#include "config.h"
|
||||
extern "C" {
|
||||
#include "YapConfig.h"
|
||||
}
|
||||
|
||||
#if HAVE_GMPXX_H
|
||||
@ -41,17 +41,14 @@ extern "C"{
|
||||
|
||||
extern "C" {
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
// Bad export from Python
|
||||
|
||||
#include <config.h>
|
||||
#include <YapConfig.h>
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
#if YAP_PYTHON
|
||||
|
||||
#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,
|
||||
YAP_Arity, YAP_Term);
|
||||
|
||||
X_API extern void YAP_UserBackCPredicate(const char *name,
|
||||
YAP_UserCPred init,
|
||||
YAP_UserCPred cont,
|
||||
YAP_Arity arity, YAP_Arity extra);
|
||||
X_API extern void YAP_UserBackCPredicate(const char *name, YAP_UserCPred init,
|
||||
YAP_UserCPred cont, YAP_Arity arity,
|
||||
YAP_Arity extra);
|
||||
|
||||
X_API extern void YAP_UserBackCutCPredicate(const char *name,
|
||||
YAP_UserCPred init,
|
||||
YAP_UserCPred cont,
|
||||
YAP_UserCPred cut,
|
||||
YAP_Arity arity, YAP_Arity extra);
|
||||
YAP_UserCPred cut, YAP_Arity arity,
|
||||
YAP_Arity extra);
|
||||
|
||||
X_API extern YAP_Term YAP_ReadBuffer(const char *s, YAP_Term *tp);
|
||||
|
||||
extern YAP_Term YAP_MkcharPTerm(char *s);
|
||||
|
||||
|
||||
}
|
||||
|
||||
class YAPEngine;
|
||||
@ -130,7 +124,6 @@ class YAPModule;
|
||||
class YAPError;
|
||||
class YAPPredicate;
|
||||
|
||||
|
||||
#include "yapa.hh"
|
||||
|
||||
#include "yapie.hh"
|
||||
|
@ -1,41 +1,40 @@
|
||||
#include "config.h"
|
||||
#include "YapConfig.h"
|
||||
#include "udi.h"
|
||||
#include "utarray.h"
|
||||
#include "uthash.h"
|
||||
|
||||
/* Argument Indexing */
|
||||
struct udi_p_args {
|
||||
int arg; //indexed arg
|
||||
void *idxstr; //user indexing structure
|
||||
UdiControlBlock control; //user indexing structure functions
|
||||
int arg; // indexed arg
|
||||
void *idxstr; // user indexing structure
|
||||
UdiControlBlock control; // user indexing structure functions
|
||||
};
|
||||
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 */
|
||||
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
|
||||
* stored in a uthash
|
||||
*/
|
||||
struct udi_info
|
||||
{
|
||||
PredEntry *p; //predicate (need to identify asserts)
|
||||
UT_array *clauselist; //clause list used on returns
|
||||
UT_array *args; //indexed args
|
||||
UT_hash_handle hh; //uthash handle
|
||||
struct udi_info {
|
||||
PredEntry *p; // predicate (need to identify asserts)
|
||||
UT_array *clauselist; // clause list used on returns
|
||||
UT_array *args; // indexed args
|
||||
UT_hash_handle hh; // uthash handle
|
||||
};
|
||||
typedef struct udi_info *UdiInfo;
|
||||
|
||||
/* to ease code for a UdiInfo hash table*/
|
||||
#define HASH_FIND_UdiInfo(head,find,out) \
|
||||
HASH_FIND(hh,head,find,sizeof(PredEntry),out)
|
||||
#define HASH_ADD_UdiInfo(head,p,add) \
|
||||
HASH_ADD_KEYPTR(hh,head,p,sizeof(PredEntry *),add)
|
||||
#define HASH_FIND_UdiInfo(head, find, out) \
|
||||
HASH_FIND(hh, head, find, sizeof(PredEntry), out)
|
||||
#define HASH_ADD_UdiInfo(head, p, add) \
|
||||
HASH_ADD_KEYPTR(hh, head, p, sizeof(PredEntry *), add)
|
||||
|
||||
/* 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);
|
||||
|
||||
/*
|
||||
@ -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) */
|
||||
#include "clause_list.h"
|
||||
struct si_callback_h
|
||||
{
|
||||
struct si_callback_h {
|
||||
clause_list_t cl;
|
||||
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)
|
||||
{
|
||||
si_callback_h_t c = (si_callback_h_t) arg;
|
||||
yamop **cl = (yamop **) utarray_eltptr(c->clauselist, ((YAP_Int) data) - 1);
|
||||
static inline int si_callback(void *key, void *data, void *arg) {
|
||||
si_callback_h_t c = (si_callback_h_t)arg;
|
||||
yamop **cl = (yamop **)utarray_eltptr(c->clauselist, ((YAP_Int)data) - 1);
|
||||
return Yap_ClauseListExtend(c->cl, *cl, c->pred);
|
||||
}
|
||||
|
||||
|
@ -1,23 +1,21 @@
|
||||
/*************************************************************************
|
||||
* *
|
||||
* Yap Prolog *
|
||||
* *
|
||||
* Yap Prolog Was Developed At Nccup - Universidade Do Porto *
|
||||
* *
|
||||
* Copyright L.Damas, V.S.Costa And Universidade Do Porto 1985-1997 *
|
||||
* *
|
||||
**************************************************************************
|
||||
* *
|
||||
* File: Yap.C *
|
||||
* Last Rev: *
|
||||
* Mods: *
|
||||
* Comments: Yap's Main File *
|
||||
* *
|
||||
*************************************************************************/
|
||||
* *
|
||||
* Yap Prolog *
|
||||
* *
|
||||
* Yap Prolog Was Developed At Nccup - Universidade Do Porto *
|
||||
* *
|
||||
* Copyright L.Damas, V.S.Costa And Universidade Do Porto 1985-1997 *
|
||||
* *
|
||||
**************************************************************************
|
||||
* *
|
||||
* File: Yap.C * Last Rev:
|
||||
** Mods: * Comments: Yap's Main File *
|
||||
* *
|
||||
*************************************************************************/
|
||||
/* static char SccsId[] = "X 4.3.3"; */
|
||||
|
||||
#include "YapConfig.h"
|
||||
#include "YapInterface.h"
|
||||
#include "config.h"
|
||||
|
||||
#include "cut_c.h"
|
||||
|
||||
@ -112,9 +110,8 @@ static bool exec_top_level(int BootMode, YAP_init_args *iap) {
|
||||
livegoal = YAP_FullLookupAtom("live");
|
||||
}
|
||||
return true;
|
||||
//YAP_Exit(EXIT_SUCCESS);
|
||||
|
||||
}
|
||||
// YAP_Exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
// FILE *debugf;
|
||||
|
||||
|
@ -19,7 +19,7 @@ extern "C" {
|
||||
|
||||
//=== includes ===============================================================
|
||||
#ifdef YAP_KERNEL
|
||||
#include "config.h"
|
||||
#include "YapConfig.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
@ -40,8 +40,8 @@ extern "C" {
|
||||
#include "YapInterface.h"
|
||||
#else
|
||||
#if _YAP_NOT_INSTALLED_
|
||||
#include <YapConfig.h>
|
||||
#include <YapInterface.h>
|
||||
#include <config.h>
|
||||
#else
|
||||
#include <Yap/YapInterface.h>
|
||||
#endif
|
||||
|
@ -31,6 +31,8 @@ The following routines export the YAP internals and architecture.
|
||||
|
||||
#define _yap_c_interface_h 1
|
||||
|
||||
#include "YapConfig.h"
|
||||
|
||||
#define __YAP_PROLOG__ 1
|
||||
|
||||
#ifndef YAPVERSION
|
||||
|
@ -13,8 +13,8 @@
|
||||
* *
|
||||
*************************************************************************/
|
||||
|
||||
#include "YapConfig.h"
|
||||
#include "YapInterface.h"
|
||||
#include "config.h"
|
||||
#include <math.h>
|
||||
#if defined(__MINGW32__) || _MSC_VER
|
||||
#include <windows.h>
|
||||
|
@ -1,67 +1,56 @@
|
||||
/*************************************************************************
|
||||
* *
|
||||
* YAP Prolog *
|
||||
* *
|
||||
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
||||
* *
|
||||
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
||||
* *
|
||||
**************************************************************************
|
||||
* *
|
||||
* File: random.c *
|
||||
* Last rev: *
|
||||
* mods: *
|
||||
* comments: regular expression interpreter *
|
||||
* *
|
||||
*************************************************************************/
|
||||
* *
|
||||
* YAP Prolog *
|
||||
* *
|
||||
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
||||
* *
|
||||
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
||||
* *
|
||||
**************************************************************************
|
||||
* *
|
||||
* File: random.c * Last rev:
|
||||
** mods: * comments: regular expression interpreter *
|
||||
* *
|
||||
*************************************************************************/
|
||||
|
||||
#include "config.h"
|
||||
#include "YapInterface.h"
|
||||
#include <math.h>
|
||||
#if defined(__MINGW32__) || _MSC_VER
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
X_API void init_random( void );
|
||||
X_API void init_random(void);
|
||||
|
||||
static short a1 = 27314, b1 = 9213, c1 = 17773;
|
||||
|
||||
static YAP_Bool
|
||||
p_random(void)
|
||||
{
|
||||
static YAP_Bool p_random(void) {
|
||||
double fli;
|
||||
long int t1, t2, t3;
|
||||
|
||||
t1 = (a1 * 171) % 30269;
|
||||
t2 = (b1 * 172) % 30307;
|
||||
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;
|
||||
b1 = t2;
|
||||
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
|
||||
p_setrand(void)
|
||||
{
|
||||
static YAP_Bool p_setrand(void) {
|
||||
a1 = YAP_IntOfTerm(YAP_ARG1);
|
||||
b1 = YAP_IntOfTerm(YAP_ARG2);
|
||||
c1 = YAP_IntOfTerm(YAP_ARG3);
|
||||
return(TRUE);
|
||||
return (TRUE);
|
||||
}
|
||||
|
||||
static YAP_Bool
|
||||
p_getrand(void)
|
||||
{
|
||||
return(YAP_Unify(YAP_ARG1,YAP_MkIntTerm(a1)) &&
|
||||
YAP_Unify(YAP_ARG2,YAP_MkIntTerm(b1)) &&
|
||||
YAP_Unify(YAP_ARG3,YAP_MkIntTerm(c1)));
|
||||
static YAP_Bool p_getrand(void) {
|
||||
return (YAP_Unify(YAP_ARG1, YAP_MkIntTerm(a1)) &&
|
||||
YAP_Unify(YAP_ARG2, YAP_MkIntTerm(b1)) &&
|
||||
YAP_Unify(YAP_ARG3, YAP_MkIntTerm(c1)));
|
||||
}
|
||||
|
||||
X_API void
|
||||
init_random(void)
|
||||
{
|
||||
X_API void init_random(void) {
|
||||
YAP_UserCPredicate("random", p_random, 1);
|
||||
YAP_UserCPredicate("setrand", p_setrand, 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 hinst, DWORD reason, LPVOID reserved)
|
||||
{
|
||||
switch (reason)
|
||||
{
|
||||
int WINAPI win_random(HANDLE hinst, DWORD reason, LPVOID reserved) {
|
||||
switch (reason) {
|
||||
case DLL_PROCESS_ATTACH:
|
||||
break;
|
||||
case DLL_PROCESS_DETACH:
|
||||
|
@ -50,7 +50,7 @@ static char sccsid[] = "@(#)regcomp.c 8.5 (Berkeley) 3/20/94";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include "YapInterface.h"
|
||||
#include "config.h"
|
||||
|
||||
#if HAVE_SYS_TYPES_H
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
|
@ -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
|
||||
* representations for state sets.
|
||||
*/
|
||||
#include "config.h"
|
||||
#include "YapInterface.h"
|
||||
|
||||
#ifndef HAVE_REGEXEC
|
||||
|
||||
#include "YapInterface.h"
|
||||
#if HAVE_SYS_TYPES_H
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
@ -68,8 +67,8 @@ static char sccsid[] = "@(#)regexec.c 8.3 (Berkeley) 3/20/94";
|
||||
#include <limits.h>
|
||||
#endif
|
||||
|
||||
#include "utils.h"
|
||||
#include "regex2.h"
|
||||
#include "utils.h"
|
||||
#include "yapregex.h"
|
||||
|
||||
#if used
|
||||
|
@ -22,7 +22,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#if HAVE_SYS_TYPES_H
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
|
@ -40,6 +40,8 @@
|
||||
#ifndef _REGEX_H_
|
||||
#define _REGEX_H_
|
||||
|
||||
#include "YapInterface.h"
|
||||
|
||||
/* types */
|
||||
typedef int regoff_t;
|
||||
|
||||
@ -97,7 +99,7 @@ typedef struct {
|
||||
|
||||
int yap_regcomp(regex_t *, const char *, int);
|
||||
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 *);
|
||||
|
||||
#endif /* !_REGEX_H_ */
|
||||
|
@ -1,20 +1,19 @@
|
||||
/*************************************************************************
|
||||
* *
|
||||
* YAP Prolog *
|
||||
* *
|
||||
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
||||
* *
|
||||
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
||||
* *
|
||||
**************************************************************************
|
||||
* *
|
||||
* *
|
||||
* YAP Prolog *
|
||||
* *
|
||||
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
||||
* *
|
||||
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
||||
* *
|
||||
**************************************************************************
|
||||
* *
|
||||
*
|
||||
* comments: regular expression interpreter *
|
||||
* *
|
||||
*************************************************************************/
|
||||
* comments: regular expression interpreter *
|
||||
* *
|
||||
*************************************************************************/
|
||||
|
||||
#include "YapInterface.h"
|
||||
#include "config.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
@ -312,8 +311,7 @@ static YAP_Bool rename_file(void) {
|
||||
return (TRUE);
|
||||
}
|
||||
|
||||
|
||||
static YAP_Bool read_link(void) {
|
||||
static YAP_Bool read_link(void) {
|
||||
char *s1 = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
|
||||
#if HAVE_READLINK
|
||||
char buf[MAXPATHLEN + 1];
|
||||
@ -321,13 +319,12 @@ static YAP_Bool rename_file(void) {
|
||||
if (readlink(s1, buf, MAXPATHLEN) < 0)
|
||||
return false;
|
||||
|
||||
|
||||
/* return an error number */
|
||||
if (!YAP_Unify(YAP_ARG2, YAP_MkAtomTerm(YAP_LookupAtom(buf)))) {
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
# if _WIN32
|
||||
#if _WIN32
|
||||
return false;
|
||||
#endif
|
||||
return true;
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -5,25 +5,21 @@
|
||||
version: $ID$
|
||||
*********************************************/
|
||||
|
||||
|
||||
|
||||
/* -------------------------------------- */
|
||||
/* Yap Tagging Scheme */
|
||||
/* -------------------------------------- */
|
||||
|
||||
#include "config.h"
|
||||
#if SIZEOF_INT_P==4
|
||||
#include "YapInterface.h"
|
||||
#if SIZEOF_INT_P == 4
|
||||
#define TAG_LOW_BITS_32 /* 'Tags_32LowTag.h' tagging scheme */
|
||||
#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 SIZE_FLOAT_AS_TERM 1
|
||||
#else
|
||||
#error Unknown tagging scheme
|
||||
#endif /* YAP_SCHEME */
|
||||
|
||||
|
||||
|
||||
/* --------------------------- */
|
||||
/* Defines */
|
||||
/* --------------------------- */
|
||||
@ -44,7 +40,7 @@
|
||||
#define TRIE_MODE_STANDARD 0
|
||||
#define TRIE_MODE_REVERSE 1
|
||||
#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_FLOAT 1
|
||||
@ -53,8 +49,6 @@
|
||||
|
||||
#define BASE_AUXILIARY_TERM_STACK_SIZE 100000
|
||||
|
||||
|
||||
|
||||
/* --------------------------- */
|
||||
/* Structs */
|
||||
/* --------------------------- */
|
||||
@ -71,7 +65,7 @@ typedef struct trie_engine {
|
||||
YAP_Int tries_max_used;
|
||||
YAP_Int entries_max_used;
|
||||
YAP_Int nodes_max_used;
|
||||
} *TrEngine;
|
||||
} * TrEngine;
|
||||
|
||||
#define TrEngine_trie(X) ((X)->first_trie)
|
||||
#define TrEngine_memory(X) ((X)->memory_in_use)
|
||||
@ -89,7 +83,7 @@ typedef struct trie_node {
|
||||
struct trie_node *next;
|
||||
struct trie_node *previous;
|
||||
YAP_Term entry;
|
||||
} *TrNode;
|
||||
} * TrNode;
|
||||
|
||||
#define TrNode_parent(X) ((X)->parent)
|
||||
#define TrNode_child(X) ((X)->child)
|
||||
@ -98,15 +92,16 @@ typedef struct trie_node {
|
||||
#define TrNode_entry(X) ((X)->entry)
|
||||
|
||||
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;
|
||||
int number_of_buckets;
|
||||
int number_of_nodes;
|
||||
} *TrHash;
|
||||
} * TrHash;
|
||||
|
||||
#define TrHash_mark(X) ((X)->parent)
|
||||
#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_seed(X) ((X)->number_of_buckets - 1)
|
||||
#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_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 */
|
||||
@ -129,12 +123,15 @@ typedef struct trie_hash {
|
||||
|
||||
#define TAG_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 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 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 TrieVarIndex(TERM) ((TERM) >> 4)
|
||||
|
||||
@ -144,17 +141,20 @@ typedef struct trie_hash {
|
||||
#define HASH_TERM(TERM, SEED) (((TERM) >> 4) & (SEED))
|
||||
#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 HASH_SAVE_MARK ((YAP_Term) MkTrieVar(BASE_SAVE_MARK))
|
||||
#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 BASE_SAVE_MARK \
|
||||
10000 /* could lead to errors if the number of different variables in a term \
|
||||
is greater than it */
|
||||
#define HASH_SAVE_MARK ((YAP_Term)MkTrieVar(BASE_SAVE_MARK))
|
||||
#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 POP_UP(STACK) *--STACK
|
||||
#define POP_DOWN(STACK) *++STACK
|
||||
#define PUSH_UP(STACK, ITEM, STACK_TOP) \
|
||||
{ if (STACK < STACK_TOP) { \
|
||||
{ \
|
||||
if (STACK < STACK_TOP) { \
|
||||
fprintf(stderr, "**************************************\n"); \
|
||||
fprintf(stderr, " Tries core module: term stack full\n"); \
|
||||
fprintf(stderr, "**************************************\n"); \
|
||||
@ -163,7 +163,8 @@ typedef struct trie_hash {
|
||||
STACK--; \
|
||||
}
|
||||
#define PUSH_DOWN(STACK, ITEM, STACK_TOP) \
|
||||
{ if (STACK > STACK_TOP) { \
|
||||
{ \
|
||||
if (STACK > STACK_TOP) { \
|
||||
fprintf(stderr, "**************************************\n"); \
|
||||
fprintf(stderr, " Tries core module: term stack empty\n"); \
|
||||
fprintf(stderr, "**************************************\n"); \
|
||||
@ -172,12 +173,11 @@ typedef struct trie_hash {
|
||||
STACK++; \
|
||||
}
|
||||
|
||||
|
||||
|
||||
#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) \
|
||||
{ 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_memory(TR_ENGINE) = 0; \
|
||||
TrEngine_tries(TR_ENGINE) = 0; \
|
||||
@ -189,7 +189,8 @@ typedef struct trie_hash {
|
||||
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); \
|
||||
{ \
|
||||
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; \
|
||||
@ -199,7 +200,8 @@ typedef struct trie_hash {
|
||||
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); \
|
||||
{ \
|
||||
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); \
|
||||
@ -207,18 +209,19 @@ typedef struct trie_hash {
|
||||
INCREMENT_MEMORY(CURRENT_TRIE_ENGINE, SIZEOF_TR_HASH); \
|
||||
}
|
||||
#define new_hash_buckets(TR_HASH, NUM_BUCKETS) \
|
||||
{ int i; void **ptr; \
|
||||
{ \
|
||||
int i; \
|
||||
void **ptr; \
|
||||
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--) \
|
||||
*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() \
|
||||
{ YAP_Term *aux_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); \
|
||||
@ -227,87 +230,101 @@ typedef struct trie_hash {
|
||||
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) \
|
||||
{ free_struct(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); \
|
||||
{ \
|
||||
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); \
|
||||
{ \
|
||||
free_struct(STR); \
|
||||
DECREMENT_MEMORY(CURRENT_TRIE_ENGINE, (NUM_BUCKETS)*SIZEOF_TR_BUCKET); \
|
||||
}
|
||||
|
||||
|
||||
|
||||
#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)) \
|
||||
TrEngine_memory_max(TR_ENGINE) = TrEngine_memory(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)) \
|
||||
TrEngine_tries_max(TR_ENGINE) = TrEngine_tries(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)) \
|
||||
TrEngine_entries_max(TR_ENGINE) = TrEngine_entries(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)) \
|
||||
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 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) && \
|
||||
#define IS_FUNCTOR_NODE(N) \
|
||||
(((ApplTag & TrNode_entry(N)) == ApplTag) && \
|
||||
(TrNode_entry(N) != PairInitTag) && (TrNode_entry(N) != PairEndEmptyTag) && \
|
||||
(TrNode_entry(N) != PairEndTermTag))
|
||||
|
||||
|
||||
/* --------------------------- */
|
||||
/* API */
|
||||
/* --------------------------- */
|
||||
|
||||
extern TrEngine core_trie_init_module(void);
|
||||
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_all(TrEngine engine, void (*destruct_function)(TrNode));
|
||||
extern void core_trie_close(TrEngine engine, TrNode node,
|
||||
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 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 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_subtree(TrEngine engine, TrNode node, 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 void core_trie_remove_entry(TrEngine engine, TrNode node,
|
||||
void (*destruct_function)(TrNode));
|
||||
extern void core_trie_remove_subtree(TrEngine engine, TrNode node,
|
||||
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_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_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);
|
||||
|
@ -16,8 +16,7 @@
|
||||
|
||||
/* Written by Eric Blake <e...@byu.net>, 2010. */
|
||||
|
||||
|
||||
#include "config.h"
|
||||
#include "YapConfig.h"
|
||||
|
||||
/* Specification. */
|
||||
#include <stdio.h>
|
||||
@ -32,15 +31,14 @@
|
||||
#if !HAVE_OPEN_MEMSTREAM && !_WIN32
|
||||
|
||||
#if !HAVE_FUNOPEN
|
||||
# error Sorry, not ported to your platform yet
|
||||
#error Sorry, not ported to your platform yet
|
||||
#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. */
|
||||
size_t *len; /* User's argument. Smaller of pos or eof. */
|
||||
size_t pos; /* Current position. */
|
||||
@ -51,18 +49,15 @@ struct data
|
||||
typedef struct data data;
|
||||
|
||||
/* Stupid BSD interface uses int/int instead of ssize_t/size_t. */
|
||||
//verify (sizeof (int) <= sizeof (size_t));
|
||||
//verify (sizeof (int) <= sizeof (ssize_t));
|
||||
// verify (sizeof (int) <= sizeof (size_t));
|
||||
// verify (sizeof (int) <= sizeof (ssize_t));
|
||||
|
||||
static int
|
||||
mem_write (void *c, const char *buf, int n)
|
||||
{
|
||||
static int mem_write(void *c, const char *buf, int n) {
|
||||
data *cookie = c;
|
||||
char *cbuf = *cookie->buf;
|
||||
|
||||
/* Be sure we don't overflow. */
|
||||
if ((ssize_t) (cookie->pos + n) < 0)
|
||||
{
|
||||
if ((ssize_t)(cookie->pos + n) < 0) {
|
||||
errno = EFBIG;
|
||||
return EOF;
|
||||
}
|
||||
@ -70,12 +65,11 @@ mem_write (void *c, const char *buf, int n)
|
||||
quadratic realloc behavior. Overallocate, to accomodate the
|
||||
requirement to always place a trailing NUL not counted by length.
|
||||
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;
|
||||
if (newsize < cookie->pos + n + 1)
|
||||
newsize = cookie->pos + n + 1;
|
||||
cbuf = realloc (cbuf, newsize);
|
||||
cbuf = realloc(cbuf, newsize);
|
||||
if (!cbuf)
|
||||
return EOF;
|
||||
*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
|
||||
intermediate bytges are NUL. */
|
||||
if (cookie->eof < cookie->pos)
|
||||
memset (cbuf + cookie->eof, '\0', cookie->pos - cookie->eof);
|
||||
memmove (cbuf + cookie->pos, buf, n);
|
||||
memset(cbuf + cookie->eof, '\0', cookie->pos - cookie->eof);
|
||||
memmove(cbuf + cookie->pos, buf, n);
|
||||
cookie->pos += n;
|
||||
/* If the user has previously written beyond the current position,
|
||||
remember what the trailing NUL is overwriting. Otherwise,
|
||||
@ -99,9 +93,7 @@ mem_write (void *c, const char *buf, int n)
|
||||
return n;
|
||||
}
|
||||
|
||||
static fpos_t
|
||||
mem_seek (void *c, fpos_t pos, int whence)
|
||||
{
|
||||
static fpos_t mem_seek(void *c, fpos_t pos, int whence) {
|
||||
data *cookie = c;
|
||||
off_t offset = pos;
|
||||
|
||||
@ -109,81 +101,64 @@ mem_seek (void *c, fpos_t pos, int whence)
|
||||
offset = cookie->pos;
|
||||
else if (whence == SEEK_END)
|
||||
offset = cookie->eof;
|
||||
if (offset < 0)
|
||||
{
|
||||
if (offset < 0) {
|
||||
errno = EINVAL;
|
||||
offset = -1;
|
||||
}
|
||||
else if ((size_t) offset != offset)
|
||||
{
|
||||
} else if ((size_t)offset != offset) {
|
||||
errno = ENOSPC;
|
||||
offset = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cookie->pos < cookie->eof)
|
||||
{
|
||||
} else {
|
||||
if (cookie->pos < cookie->eof) {
|
||||
(*cookie->buf)[cookie->pos] = cookie->c;
|
||||
cookie->c = '\0';
|
||||
}
|
||||
cookie->pos = offset;
|
||||
if (cookie->pos < cookie->eof)
|
||||
{
|
||||
if (cookie->pos < cookie->eof) {
|
||||
cookie->c = (*cookie->buf)[cookie->pos];
|
||||
(*cookie->buf)[cookie->pos] = '\0';
|
||||
*cookie->len = cookie->pos;
|
||||
}
|
||||
else
|
||||
} else
|
||||
*cookie->len = cookie->eof;
|
||||
}
|
||||
return offset;
|
||||
}
|
||||
|
||||
static int
|
||||
mem_close (void *c)
|
||||
{
|
||||
static int mem_close(void *c) {
|
||||
data *cookie = c;
|
||||
char *buf;
|
||||
|
||||
/* Be nice and try to reduce excess memory. */
|
||||
buf = realloc (*cookie->buf, *cookie->len + 1);
|
||||
buf = realloc(*cookie->buf, *cookie->len + 1);
|
||||
if (buf)
|
||||
*cookie->buf = buf;
|
||||
free (cookie);
|
||||
free(cookie);
|
||||
return 0;
|
||||
}
|
||||
|
||||
FILE *
|
||||
open_memstream (char **buf, size_t *len)
|
||||
{
|
||||
FILE *open_memstream(char **buf, size_t *len) {
|
||||
FILE *f;
|
||||
data *cookie;
|
||||
|
||||
if (!buf || !len)
|
||||
{
|
||||
if (!buf || !len) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
if (!(cookie = malloc (sizeof *cookie)))
|
||||
if (!(cookie = malloc(sizeof *cookie)))
|
||||
return NULL;
|
||||
if (!(*buf = malloc (INITIAL_ALLOC)))
|
||||
{
|
||||
free (cookie);
|
||||
if (!(*buf = malloc(INITIAL_ALLOC))) {
|
||||
free(cookie);
|
||||
errno = ENOMEM;
|
||||
return NULL;
|
||||
}
|
||||
**buf = '\0';
|
||||
*len = 0;
|
||||
|
||||
f = funopen (cookie, NULL, mem_write, mem_seek, mem_close);
|
||||
if (!f)
|
||||
{
|
||||
f = funopen(cookie, NULL, mem_write, mem_seek, mem_close);
|
||||
if (!f) {
|
||||
int saved_errno = errno;
|
||||
free (cookie);
|
||||
free(cookie);
|
||||
errno = saved_errno;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
cookie->buf = buf;
|
||||
cookie->len = len;
|
||||
cookie->pos = 0;
|
||||
|
@ -13,7 +13,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "YapConfig.h"
|
||||
|
||||
#if _WIN32 || defined(__MINGW32__)
|
||||
#if !defined(MINGW_HAS_SECURE_API)
|
||||
@ -22,10 +22,10 @@
|
||||
//#undef _POSIX_
|
||||
#endif
|
||||
#include "Yap.h"
|
||||
#include "YapEval.h"
|
||||
#include "YapHeap.h"
|
||||
#include "YapText.h"
|
||||
#include "Yatom.h"
|
||||
#include "YapEval.h"
|
||||
#include "yapio.h"
|
||||
|
||||
// Win32 InputOutput Support
|
||||
@ -46,7 +46,6 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
@ -121,7 +120,6 @@
|
||||
/* windows.h does not like absmi.h, this
|
||||
should fix it for now */
|
||||
#include <math.h>
|
||||
#include <math.h>
|
||||
#if HAVE_TIME_H
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
@ -41,7 +41,6 @@ CUDD will generate better/faster code.
|
||||
#include <stdio.h>
|
||||
|
||||
#include "YapInterface.h"
|
||||
#include "config.h"
|
||||
#include "cudd_config.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);
|
||||
DdNode *n0 = (DdNode *)YAP_IntOfTerm(YAP_ARG2);
|
||||
const char *s = YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3));
|
||||
char ** namesp;
|
||||
char **namesp;
|
||||
YAP_Term names = YAP_ARG4;
|
||||
FILE *f;
|
||||
YAP_Int len;
|
||||
@ -850,7 +849,7 @@ static YAP_Bool p_cudd_print_with_names(void) {
|
||||
names = YAP_TailOfTerm(names);
|
||||
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)
|
||||
fclose(f);
|
||||
while (i > 0) {
|
||||
@ -863,8 +862,8 @@ static YAP_Bool p_cudd_print_with_names(void) {
|
||||
|
||||
static YAP_Bool p_cudd_die(void) {
|
||||
DdManager *manager = (DdManager *)YAP_IntOfTerm(YAP_ARG1);
|
||||
//Cudd_FreeTree(manager);
|
||||
//cuddFreeTable(manager);
|
||||
// Cudd_FreeTree(manager);
|
||||
// cuddFreeTable(manager);
|
||||
Cudd_CheckZeroRef(manager);
|
||||
Cudd_Quit(manager);
|
||||
return TRUE;
|
||||
|
@ -186,14 +186,13 @@
|
||||
* *
|
||||
\******************************************************************************/
|
||||
|
||||
|
||||
#include "YapInterface.h"
|
||||
#include "cudd_config.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include "config.h"
|
||||
#include "cudd_config.h"
|
||||
|
||||
#if HAVE_CUDD_UTIL_H
|
||||
#include <cudd/util.h>
|
||||
@ -281,28 +280,33 @@ typedef struct _nodeline {
|
||||
|
||||
/* Initialization */
|
||||
|
||||
DdManager* simpleBDDinit(int varcnt);
|
||||
DdManager* simpleBDDinitNoReOrder(int varcnt);
|
||||
DdManager *simpleBDDinit(int varcnt);
|
||||
DdManager *simpleBDDinitNoReOrder(int varcnt);
|
||||
|
||||
/* BDD Generation */
|
||||
|
||||
DdNode* D_BDDAnd(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_BDDNor(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_BDDAnd(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_BDDNor(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* FileGenerateBDD(DdManager *manager, namedvars varmap, bddfileheader fileheader);
|
||||
DdNode** FileGenerateBDDForest(DdManager *manager, namedvars varmap, bddfileheader fileheader);
|
||||
DdNode* OnlineGenerateBDD(DdManager *manager, namedvars *varmap);
|
||||
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);
|
||||
DdNode *FileGenerateBDD(DdManager *manager, namedvars varmap,
|
||||
bddfileheader fileheader);
|
||||
DdNode **FileGenerateBDDForest(DdManager *manager, namedvars varmap,
|
||||
bddfileheader fileheader);
|
||||
DdNode *OnlineGenerateBDD(DdManager *manager, namedvars *varmap);
|
||||
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);
|
||||
char* getFileName(const char *function);
|
||||
char *getFileName(const char *function);
|
||||
int GetParam(char *inputline, int iParam);
|
||||
char** GetVariableOrder(char *filename, int varcnt);
|
||||
char **GetVariableOrder(char *filename, int varcnt);
|
||||
int LoadVariableData(namedvars varmap, char *filename);
|
||||
|
||||
/* Named variables */
|
||||
@ -313,42 +317,51 @@ namedvars InitNamedVars(int varcnt, int varstart);
|
||||
void EnlargeNamedVars(namedvars *varmap, int newvarcnt);
|
||||
int AddNamedVarAt(namedvars varmap, const char *varname, int index);
|
||||
int AddNamedVar(namedvars varmap, const char *varname);
|
||||
void SetNamedVarValuesAt(namedvars varmap, int index, double dvalue, int ivalue, void *dynvalue);
|
||||
int SetNamedVarValues(namedvars varmap, const char *varname, double dvalue, int ivalue, void *dynvalue);
|
||||
void SetNamedVarValuesAt(namedvars varmap, int index, double dvalue, int ivalue,
|
||||
void *dynvalue);
|
||||
int SetNamedVarValues(namedvars varmap, const char *varname, double dvalue,
|
||||
int ivalue, void *dynvalue);
|
||||
int GetNamedVarIndex(const namedvars varmap, const char *varname);
|
||||
int RepairVarcnt(namedvars *varmap);
|
||||
char* GetNodeVarName(DdManager *manager, namedvars varmap, DdNode *node);
|
||||
char* GetNodeVarNameDisp(DdManager *manager, namedvars varmap, DdNode *node);
|
||||
char *GetNodeVarName(DdManager *manager, namedvars varmap, DdNode *node);
|
||||
char *GetNodeVarNameDisp(DdManager *manager, namedvars varmap, DdNode *node);
|
||||
int all_loaded(namedvars varmap, int disp);
|
||||
|
||||
/* Traversal */
|
||||
|
||||
DdNode* HighNodeOf(DdManager *manager, DdNode *node);
|
||||
DdNode* LowNodeOf(DdManager *manager, DdNode *node);
|
||||
DdNode *HighNodeOf(DdManager *manager, DdNode *node);
|
||||
DdNode *LowNodeOf(DdManager *manager, DdNode *node);
|
||||
|
||||
/* Traversal - History */
|
||||
|
||||
hisqueue* InitHistory(int varcnt);
|
||||
hisqueue *InitHistory(int varcnt);
|
||||
void ReInitHistory(hisqueue *HisQueue, int varcnt);
|
||||
void AddNode(hisqueue *HisQueue, int varstart, DdNode *node, double dvalue, int ivalue, void *dynvalue);
|
||||
hisnode* GetNode(hisqueue *HisQueue, int varstart, DdNode *node);
|
||||
void AddNode(hisqueue *HisQueue, int varstart, DdNode *node, double dvalue,
|
||||
int ivalue, void *dynvalue);
|
||||
hisnode *GetNode(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 */
|
||||
|
||||
bddfileheader ReadFileHeader(char *filename);
|
||||
int CheckFileVersion(const char *version);
|
||||
|
||||
DdNode * LoadNodeDump(DdManager *manager, namedvars varmap, FILE *inputfile);
|
||||
DdNode * LoadNodeRec(DdManager *manager, namedvars varmap, hisqueue *Nodes, FILE *inputfile, nodeline current);
|
||||
DdNode * GetIfExists(DdManager *manager, namedvars varmap, hisqueue *Nodes, char *varname, int nodenum);
|
||||
DdNode *LoadNodeDump(DdManager *manager, namedvars varmap, FILE *inputfile);
|
||||
DdNode *LoadNodeRec(DdManager *manager, namedvars varmap, hisqueue *Nodes,
|
||||
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);
|
||||
void SaveExpand(DdManager *manager, namedvars varmap, hisqueue *Nodes, DdNode *Current, FILE *outputfile);
|
||||
int SaveNodeDump(DdManager *manager, namedvars varmap, DdNode *bdd,
|
||||
char *filename);
|
||||
void SaveExpand(DdManager *manager, namedvars varmap, hisqueue *Nodes,
|
||||
DdNode *Current, FILE *outputfile);
|
||||
void ExpandNodes(hisqueue *Nodes, int index, int nodenum);
|
||||
|
||||
/* Export */
|
||||
|
||||
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);
|
||||
|
@ -184,16 +184,15 @@
|
||||
* *
|
||||
\******************************************************************************/
|
||||
|
||||
|
||||
#include <unistd.h>
|
||||
#include "YapInterface.h"
|
||||
#include "cudd_config.h"
|
||||
#include "pqueue.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include "pqueue.h"
|
||||
#include "config.h"
|
||||
#include "cudd_config.h"
|
||||
#include <unistd.h>
|
||||
#if HAVE_CUDD_UTIL_H
|
||||
#include <cudd/util.h>
|
||||
#elif HAVE_UTIL_H
|
||||
@ -253,7 +252,7 @@ typedef struct _bddfileheader {
|
||||
typedef struct _namedvars {
|
||||
int varcnt;
|
||||
int varstart;
|
||||
char ** vars;
|
||||
char **vars;
|
||||
int *loaded;
|
||||
double *dvalue;
|
||||
int *ivalue;
|
||||
@ -265,7 +264,7 @@ typedef struct _namedvars {
|
||||
typedef struct _hisnode {
|
||||
DdNode *key;
|
||||
double dvalue;
|
||||
double dvalue2;// =0; //needed for expected counts
|
||||
double dvalue2; // =0; //needed for expected counts
|
||||
int ivalue;
|
||||
void *dynvalue;
|
||||
} hisnode;
|
||||
@ -286,27 +285,32 @@ typedef struct _nodeline {
|
||||
|
||||
/* Initialization */
|
||||
|
||||
DdManager* simpleBDDinit(int varcnt);
|
||||
DdManager *simpleBDDinit(int varcnt);
|
||||
|
||||
/* BDD Generation */
|
||||
|
||||
DdNode* D_BDDAnd(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_BDDNor(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_BDDAnd(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_BDDNor(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* FileGenerateBDD(DdManager *manager, namedvars varmap, bddfileheader fileheader);
|
||||
DdNode** FileGenerateBDDForest(DdManager *manager, namedvars varmap, bddfileheader fileheader);
|
||||
DdNode* OnlineGenerateBDD(DdManager *manager, namedvars *varmap);
|
||||
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);
|
||||
DdNode *FileGenerateBDD(DdManager *manager, namedvars varmap,
|
||||
bddfileheader fileheader);
|
||||
DdNode **FileGenerateBDDForest(DdManager *manager, namedvars varmap,
|
||||
bddfileheader fileheader);
|
||||
DdNode *OnlineGenerateBDD(DdManager *manager, namedvars *varmap);
|
||||
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);
|
||||
char* getFileName(const char *function);
|
||||
char *getFileName(const char *function);
|
||||
int GetParam(char *inputline, int iParam);
|
||||
char** GetVariableOrder(char *filename, int varcnt);
|
||||
char **GetVariableOrder(char *filename, int varcnt);
|
||||
int LoadVariableData(namedvars varmap, char *filename);
|
||||
|
||||
/* Named variables */
|
||||
@ -317,43 +321,53 @@ namedvars InitNamedVars(int varcnt, int varstart);
|
||||
void EnlargeNamedVars(namedvars *varmap, int newvarcnt);
|
||||
int AddNamedVarAt(namedvars varmap, const char *varname, int index);
|
||||
int AddNamedVar(namedvars varmap, const char *varname);
|
||||
void SetNamedVarValuesAt(namedvars varmap, int index, double dvalue, int ivalue, void *dynvalue);
|
||||
int SetNamedVarValues(namedvars varmap, const char *varname, double dvalue, int ivalue, void *dynvalue);
|
||||
void SetNamedVarValuesAt(namedvars varmap, int index, double dvalue, int ivalue,
|
||||
void *dynvalue);
|
||||
int SetNamedVarValues(namedvars varmap, const char *varname, double dvalue,
|
||||
int ivalue, void *dynvalue);
|
||||
int GetNamedVarIndex(const namedvars varmap, const char *varname);
|
||||
int RepairVarcnt(namedvars *varmap);
|
||||
const char* GetNodeVarName(DdManager *manager, namedvars varmap, DdNode *node);
|
||||
const char* GetNodeVarNameDisp(DdManager *manager, namedvars varmap, DdNode *node);
|
||||
const char *GetNodeVarName(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_for_deterministic_variables(namedvars varmap, int disp);
|
||||
|
||||
/* Traversal */
|
||||
|
||||
DdNode* HighNodeOf(DdManager *manager, DdNode *node);
|
||||
DdNode* LowNodeOf(DdManager *manager, DdNode *node);
|
||||
DdNode *HighNodeOf(DdManager *manager, DdNode *node);
|
||||
DdNode *LowNodeOf(DdManager *manager, DdNode *node);
|
||||
|
||||
/* Traversal - History */
|
||||
|
||||
hisqueue* InitHistory(int varcnt);
|
||||
hisqueue *InitHistory(int varcnt);
|
||||
void ReInitHistory(hisqueue *HisQueue, int varcnt);
|
||||
void AddNode(hisqueue *HisQueue, int varstart, DdNode *node, double dvalue, int ivalue, void *dynvalue);
|
||||
hisnode* GetNode(hisqueue *HisQueue, int varstart, DdNode *node);
|
||||
void AddNode(hisqueue *HisQueue, int varstart, DdNode *node, double dvalue,
|
||||
int ivalue, void *dynvalue);
|
||||
hisnode *GetNode(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 */
|
||||
|
||||
bddfileheader ReadFileHeader(char *filename);
|
||||
int CheckFileVersion(const char *version);
|
||||
|
||||
DdNode * LoadNodeDump(DdManager *manager, namedvars varmap, FILE *inputfile);
|
||||
DdNode * LoadNodeRec(DdManager *manager, namedvars varmap, hisqueue *Nodes, FILE *inputfile, nodeline current);
|
||||
DdNode * GetIfExists(DdManager *manager, namedvars varmap, hisqueue *Nodes, char *varname, int nodenum);
|
||||
DdNode *LoadNodeDump(DdManager *manager, namedvars varmap, FILE *inputfile);
|
||||
DdNode *LoadNodeRec(DdManager *manager, namedvars varmap, hisqueue *Nodes,
|
||||
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);
|
||||
void SaveExpand(DdManager *manager, namedvars varmap, hisqueue *Nodes, DdNode *Current, FILE *outputfile);
|
||||
int SaveNodeDump(DdManager *manager, namedvars varmap, DdNode *bdd,
|
||||
char *filename);
|
||||
void SaveExpand(DdManager *manager, namedvars varmap, hisqueue *Nodes,
|
||||
DdNode *Current, FILE *outputfile);
|
||||
void ExpandNodes(hisqueue *Nodes, int index, int nodenum);
|
||||
|
||||
/* Export */
|
||||
|
||||
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);
|
||||
|
@ -190,7 +190,7 @@ form
|
||||
variable=value
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "YapInterface.h"
|
||||
#include "cudd_config.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
@ -7,7 +7,6 @@ This package uses the library cudd, see http://vlsi.colorado.edu/~fabio/CUDD/
|
||||
for the relative license.
|
||||
|
||||
*/
|
||||
#include "config.h"
|
||||
#include "cudd_config.h"
|
||||
#include <stdio.h>
|
||||
#if HAVE_CUDDINT_H
|
||||
@ -18,7 +17,9 @@ for the relative license.
|
||||
|
||||
#include "YapInterface.h"
|
||||
|
||||
typedef struct { int var, value; } factor;
|
||||
typedef struct {
|
||||
int var, value;
|
||||
} factor;
|
||||
|
||||
typedef struct {
|
||||
int nFact;
|
||||
|
@ -9,7 +9,6 @@ for the relative license.
|
||||
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "cudd_config.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
@ -366,7 +365,7 @@ static YAP_Bool add_var(void) {
|
||||
probTerm = arg2;
|
||||
p0 = 1;
|
||||
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));
|
||||
bVar2mVar_ex[ex][boolVars_ex[ex] + i] = nVars_ex[ex] - 1;
|
||||
probs_ex[ex][boolVars_ex[ex] + i] = p / p0;
|
||||
|
@ -1,23 +1,22 @@
|
||||
#ifndef __MYDDAS_H__
|
||||
#define __MYDDAS_H__
|
||||
|
||||
#include "config.h"
|
||||
#include "YapConfig.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#include "myddas_types.h"
|
||||
|
||||
/* Passar para o myddas_statictics.h ???????? */
|
||||
#ifdef MYDDAS_STATS
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
||||
typedef struct myddas_global *MYDDAS_GLOBAL;
|
||||
#include "myddas_util.h"
|
||||
|
||||
|
||||
//extern void Yap_InitMYDDAS_SQLITE3Preds(void);
|
||||
//extern void Yap_InitBackMYDDAS_SQLITE3Preds(void);
|
||||
// extern void Yap_InitMYDDAS_SQLITE3Preds(void);
|
||||
// extern void Yap_InitBackMYDDAS_SQLITE3Preds(void);
|
||||
|
||||
#ifdef MYDDAS_STATS
|
||||
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;
|
||||
#endif
|
||||
|
||||
#include "myddas_structs.h"
|
||||
#include "MyddasProto.h"
|
||||
|
||||
#include "myddas_structs.h"
|
||||
|
||||
#endif /*__MYDDAS_H__*/
|
||||
|
422
packages/yap-lbfgs/liblbfgs-1.10/lib/lbfgs.c
vendored
422
packages/yap-lbfgs/liblbfgs-1.10/lib/lbfgs.c
vendored
@ -61,20 +61,18 @@ distributing the effieicnt and explanatory implementation in an open source
|
||||
licence.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif/*HAVE_CONFIG_H*/
|
||||
#include "YapConfig.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <lbfgs.h>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define inline __inline
|
||||
#endif/*_MSC_VER*/
|
||||
#endif /*_MSC_VER*/
|
||||
|
||||
#if defined(USE_SSE) && defined(__SSE2__) && LBFGS_FLOAT == 64
|
||||
/* Use SSE2 optimization for 64bit double precision. */
|
||||
@ -111,147 +109,86 @@ struct tag_iteration_data {
|
||||
typedef struct tag_iteration_data iteration_data_t;
|
||||
|
||||
static const lbfgs_parameter_t _defparam = {
|
||||
6, 1e-5, 0, 1e-5,
|
||||
0, LBFGS_LINESEARCH_DEFAULT, 40,
|
||||
1e-20, 1e20, 1e-4, 0.9, 0.9, 1.0e-16,
|
||||
0.0, 0, -1,
|
||||
6, 1e-5, 0, 1e-5, 0, LBFGS_LINESEARCH_DEFAULT,
|
||||
40, 1e-20, 1e20, 1e-4, 0.9, 0.9,
|
||||
1.0e-16, 0.0, 0, -1,
|
||||
};
|
||||
|
||||
/* Forward function declarations. */
|
||||
|
||||
typedef int (*line_search_proc)(
|
||||
int n,
|
||||
lbfgsfloatval_t *x,
|
||||
lbfgsfloatval_t *f,
|
||||
lbfgsfloatval_t *g,
|
||||
lbfgsfloatval_t *s,
|
||||
lbfgsfloatval_t *stp,
|
||||
const lbfgsfloatval_t* xp,
|
||||
const lbfgsfloatval_t* gp,
|
||||
lbfgsfloatval_t *wa,
|
||||
typedef int (*line_search_proc)(int n, lbfgsfloatval_t *x, lbfgsfloatval_t *f,
|
||||
lbfgsfloatval_t *g, 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
|
||||
);
|
||||
const lbfgs_parameter_t *param);
|
||||
|
||||
static int line_search_backtracking(
|
||||
int n,
|
||||
lbfgsfloatval_t *x,
|
||||
lbfgsfloatval_t *f,
|
||||
lbfgsfloatval_t *g,
|
||||
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
|
||||
);
|
||||
static int line_search_backtracking(int n, lbfgsfloatval_t *x,
|
||||
lbfgsfloatval_t *f, lbfgsfloatval_t *g,
|
||||
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);
|
||||
|
||||
static int line_search_backtracking_owlqn(
|
||||
int n,
|
||||
lbfgsfloatval_t *x,
|
||||
lbfgsfloatval_t *f,
|
||||
lbfgsfloatval_t *g,
|
||||
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 n, lbfgsfloatval_t *x, lbfgsfloatval_t *f, lbfgsfloatval_t *g,
|
||||
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(
|
||||
int n,
|
||||
lbfgsfloatval_t *x,
|
||||
lbfgsfloatval_t *f,
|
||||
lbfgsfloatval_t *g,
|
||||
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
|
||||
);
|
||||
static int line_search_morethuente(int n, lbfgsfloatval_t *x,
|
||||
lbfgsfloatval_t *f, lbfgsfloatval_t *g,
|
||||
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);
|
||||
|
||||
static int update_trial_interval(
|
||||
lbfgsfloatval_t *x,
|
||||
lbfgsfloatval_t *fx,
|
||||
lbfgsfloatval_t *dx,
|
||||
lbfgsfloatval_t *y,
|
||||
lbfgsfloatval_t *fy,
|
||||
lbfgsfloatval_t *dy,
|
||||
lbfgsfloatval_t *t,
|
||||
lbfgsfloatval_t *ft,
|
||||
static int update_trial_interval(lbfgsfloatval_t *x, lbfgsfloatval_t *fx,
|
||||
lbfgsfloatval_t *dx, lbfgsfloatval_t *y,
|
||||
lbfgsfloatval_t *fy, lbfgsfloatval_t *dy,
|
||||
lbfgsfloatval_t *t, lbfgsfloatval_t *ft,
|
||||
lbfgsfloatval_t *dt,
|
||||
const lbfgsfloatval_t tmin,
|
||||
const lbfgsfloatval_t tmax,
|
||||
int *brackt
|
||||
);
|
||||
const lbfgsfloatval_t tmax, int *brackt);
|
||||
|
||||
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);
|
||||
|
||||
static void owlqn_pseudo_gradient(
|
||||
lbfgsfloatval_t* pg,
|
||||
const lbfgsfloatval_t* x,
|
||||
const lbfgsfloatval_t* g,
|
||||
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_pseudo_gradient(lbfgsfloatval_t *pg, const lbfgsfloatval_t *x,
|
||||
const lbfgsfloatval_t *g, 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);
|
||||
|
||||
#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 /= 8;
|
||||
n *= 8;
|
||||
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__))
|
||||
n = round_out_variables(n);
|
||||
#endif/*defined(USE_SSE)*/
|
||||
return (lbfgsfloatval_t*)vecalloc(sizeof(lbfgsfloatval_t) * n);
|
||||
#endif /*defined(USE_SSE)*/
|
||||
return (lbfgsfloatval_t *)vecalloc(sizeof(lbfgsfloatval_t) * n);
|
||||
}
|
||||
|
||||
void lbfgs_free(lbfgsfloatval_t *x)
|
||||
{
|
||||
vecfree(x);
|
||||
}
|
||||
void lbfgs_free(lbfgsfloatval_t *x) { vecfree(x); }
|
||||
|
||||
void lbfgs_parameter_init(lbfgs_parameter_t *param)
|
||||
{
|
||||
void lbfgs_parameter_init(lbfgs_parameter_t *param) {
|
||||
memmove(param, &_defparam, sizeof(*param));
|
||||
}
|
||||
|
||||
int lbfgs(
|
||||
int n,
|
||||
lbfgsfloatval_t *x,
|
||||
lbfgsfloatval_t *ptr_fx,
|
||||
lbfgs_evaluate_t proc_evaluate,
|
||||
lbfgs_progress_t proc_progress,
|
||||
void *instance,
|
||||
lbfgs_parameter_t *_param
|
||||
)
|
||||
{
|
||||
int lbfgs(int n, lbfgsfloatval_t *x, lbfgsfloatval_t *ptr_fx,
|
||||
lbfgs_evaluate_t proc_evaluate, lbfgs_progress_t proc_progress,
|
||||
void *instance, lbfgs_parameter_t *_param) {
|
||||
int ret;
|
||||
int i, j, k, ls, end, bound;
|
||||
lbfgsfloatval_t step;
|
||||
@ -280,7 +217,7 @@ int lbfgs(
|
||||
#if defined(USE_SSE) && (defined(__SSE__) || defined(__SSE2__))
|
||||
/* Round out the number of variables. */
|
||||
n = round_out_variables(n);
|
||||
#endif/*defined(USE_SSE)*/
|
||||
#endif /*defined(USE_SSE)*/
|
||||
|
||||
/* Check the input parameters for errors. */
|
||||
if (n <= 0) {
|
||||
@ -290,10 +227,10 @@ int lbfgs(
|
||||
if (n % 8 != 0) {
|
||||
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;
|
||||
}
|
||||
#endif/*defined(USE_SSE)*/
|
||||
#endif /*defined(USE_SSE)*/
|
||||
if (param.epsilon < 0.) {
|
||||
return LBFGSERR_INVALID_EPSILON;
|
||||
}
|
||||
@ -364,11 +301,11 @@ int lbfgs(
|
||||
}
|
||||
|
||||
/* Allocate working space. */
|
||||
xp = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t));
|
||||
g = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t));
|
||||
gp = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t));
|
||||
d = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t));
|
||||
w = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t));
|
||||
xp = (lbfgsfloatval_t *)vecalloc(n * sizeof(lbfgsfloatval_t));
|
||||
g = (lbfgsfloatval_t *)vecalloc(n * sizeof(lbfgsfloatval_t));
|
||||
gp = (lbfgsfloatval_t *)vecalloc(n * sizeof(lbfgsfloatval_t));
|
||||
d = (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) {
|
||||
ret = LBFGSERR_OUTOFMEMORY;
|
||||
goto lbfgs_exit;
|
||||
@ -376,7 +313,7 @@ int lbfgs(
|
||||
|
||||
if (param.orthantwise_c != 0.) {
|
||||
/* 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) {
|
||||
ret = LBFGSERR_OUTOFMEMORY;
|
||||
goto lbfgs_exit;
|
||||
@ -384,19 +321,19 @@ int lbfgs(
|
||||
}
|
||||
|
||||
/* 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) {
|
||||
ret = LBFGSERR_OUTOFMEMORY;
|
||||
goto lbfgs_exit;
|
||||
}
|
||||
|
||||
/* Initialize the limited memory. */
|
||||
for (i = 0;i < m;++i) {
|
||||
for (i = 0; i < m; ++i) {
|
||||
it = &lm[i];
|
||||
it->alpha = 0;
|
||||
it->ys = 0;
|
||||
it->s = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t));
|
||||
it->y = (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));
|
||||
if (it->s == NULL || it->y == NULL) {
|
||||
ret = LBFGSERR_OUTOFMEMORY;
|
||||
goto lbfgs_exit;
|
||||
@ -405,7 +342,7 @@ int lbfgs(
|
||||
|
||||
/* Allocate an array for storing previous values of the objective function. */
|
||||
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. */
|
||||
@ -414,10 +351,8 @@ int lbfgs(
|
||||
/* Compute the L1 norm of the variable and add it to the object value. */
|
||||
xnorm = owlqn_x1norm(x, param.orthantwise_start, param.orthantwise_end);
|
||||
fx += xnorm * param.orthantwise_c;
|
||||
owlqn_pseudo_gradient(
|
||||
pg, x, g, n,
|
||||
param.orthantwise_c, param.orthantwise_start, param.orthantwise_end
|
||||
);
|
||||
owlqn_pseudo_gradient(pg, x, g, n, param.orthantwise_c,
|
||||
param.orthantwise_start, param.orthantwise_end);
|
||||
}
|
||||
|
||||
/* Store the initial value of the objective function. */
|
||||
@ -444,7 +379,8 @@ int lbfgs(
|
||||
} else {
|
||||
vec2norm(&gnorm, pg, n);
|
||||
}
|
||||
if (xnorm < 1.0) xnorm = 1.0;
|
||||
if (xnorm < 1.0)
|
||||
xnorm = 1.0;
|
||||
if (gnorm / xnorm <= param.epsilon) {
|
||||
ret = LBFGS_ALREADY_MINIMIZED;
|
||||
goto lbfgs_exit;
|
||||
@ -467,10 +403,8 @@ int lbfgs(
|
||||
ls = linesearch(n, x, &fx, g, d, &step, xp, gp, w, &cd, ¶m);
|
||||
} else {
|
||||
ls = linesearch(n, x, &fx, g, d, &step, xp, pg, w, &cd, ¶m);
|
||||
owlqn_pseudo_gradient(
|
||||
pg, x, g, n,
|
||||
param.orthantwise_c, param.orthantwise_start, param.orthantwise_end
|
||||
);
|
||||
owlqn_pseudo_gradient(pg, x, g, n, param.orthantwise_c,
|
||||
param.orthantwise_start, param.orthantwise_end);
|
||||
}
|
||||
if (ls < 0) {
|
||||
/* Revert to the previous point. */
|
||||
@ -490,7 +424,8 @@ int lbfgs(
|
||||
|
||||
/* Report the 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;
|
||||
}
|
||||
}
|
||||
@ -500,7 +435,8 @@ int lbfgs(
|
||||
The criterion is given by the following formula:
|
||||
|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) {
|
||||
/* Convergence. */
|
||||
ret = LBFGS_SUCCESS;
|
||||
@ -529,7 +465,7 @@ int lbfgs(
|
||||
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. */
|
||||
ret = LBFGSERR_MAXIMUMITERATION;
|
||||
break;
|
||||
@ -548,7 +484,8 @@ int lbfgs(
|
||||
Compute scalars ys and yy:
|
||||
ys = y^t \cdot s = 1 / \rho.
|
||||
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(&yy, it->y, it->y, n);
|
||||
@ -575,7 +512,7 @@ int lbfgs(
|
||||
}
|
||||
|
||||
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; */
|
||||
it = &lm[j];
|
||||
/* \alpha_{j} = \rho_{j} s^{t}_{j} \cdot q_{k+1}. */
|
||||
@ -587,7 +524,7 @@ int lbfgs(
|
||||
|
||||
vecscale(d, ys / yy, n);
|
||||
|
||||
for (i = 0;i < bound;++i) {
|
||||
for (i = 0; i < bound; ++i) {
|
||||
it = &lm[j];
|
||||
/* \beta_{j} = \rho_{j} y^t_{j} \cdot \gamma_{i}. */
|
||||
vecdot(&beta, it->y, d, n);
|
||||
@ -601,7 +538,7 @@ int lbfgs(
|
||||
Constrain the search direction for orthant-wise updates.
|
||||
*/
|
||||
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) {
|
||||
d[i] = 0;
|
||||
}
|
||||
@ -624,7 +561,7 @@ lbfgs_exit:
|
||||
|
||||
/* Free memory blocks used by this function. */
|
||||
if (lm != NULL) {
|
||||
for (i = 0;i < m;++i) {
|
||||
for (i = 0; i < m; ++i) {
|
||||
vecfree(lm[i].s);
|
||||
vecfree(lm[i].y);
|
||||
}
|
||||
@ -640,22 +577,13 @@ lbfgs_exit:
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int line_search_backtracking(
|
||||
int n,
|
||||
lbfgsfloatval_t *x,
|
||||
lbfgsfloatval_t *f,
|
||||
lbfgsfloatval_t *g,
|
||||
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_backtracking(int n, lbfgsfloatval_t *x,
|
||||
lbfgsfloatval_t *f, lbfgsfloatval_t *g,
|
||||
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;
|
||||
lbfgsfloatval_t width, dg;
|
||||
lbfgsfloatval_t finit, dginit = 0., dgtest;
|
||||
@ -701,13 +629,13 @@ static int line_search_backtracking(
|
||||
if (dg < param->wolfe * dginit) {
|
||||
width = inc;
|
||||
} else {
|
||||
if(param->linesearch == LBFGS_LINESEARCH_BACKTRACKING_WOLFE) {
|
||||
if (param->linesearch == LBFGS_LINESEARCH_BACKTRACKING_WOLFE) {
|
||||
/* Exit with the regular Wolfe condition. */
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Check the strong Wolfe condition. */
|
||||
if(dg > -param->wolfe * dginit) {
|
||||
if (dg > -param->wolfe * dginit) {
|
||||
width = dec;
|
||||
} else {
|
||||
/* Exit with the strong Wolfe condition. */
|
||||
@ -733,22 +661,11 @@ static int line_search_backtracking(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int line_search_backtracking_owlqn(
|
||||
int n,
|
||||
lbfgsfloatval_t *x,
|
||||
lbfgsfloatval_t *f,
|
||||
lbfgsfloatval_t *g,
|
||||
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 n, lbfgsfloatval_t *x, lbfgsfloatval_t *f, lbfgsfloatval_t *g,
|
||||
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;
|
||||
lbfgsfloatval_t width = 0.5, norm = 0.;
|
||||
lbfgsfloatval_t finit = *f, dgtest;
|
||||
@ -759,7 +676,7 @@ static int line_search_backtracking_owlqn(
|
||||
}
|
||||
|
||||
/* 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];
|
||||
}
|
||||
|
||||
@ -781,7 +698,7 @@ static int line_search_backtracking_owlqn(
|
||||
++count;
|
||||
|
||||
dgtest = 0.;
|
||||
for (i = 0;i < n;++i) {
|
||||
for (i = 0; i < n; ++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,
|
||||
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
|
||||
)
|
||||
{
|
||||
static int line_search_morethuente(int n, lbfgsfloatval_t *x,
|
||||
lbfgsfloatval_t *f, lbfgsfloatval_t *g,
|
||||
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 brackt, stage1, uinfo = 0;
|
||||
lbfgsfloatval_t dg;
|
||||
@ -881,14 +789,18 @@ static int line_search_morethuente(
|
||||
}
|
||||
|
||||
/* Clip the step in the range of [stpmin, stpmax]. */
|
||||
if (*stp < param->min_step) *stp = param->min_step;
|
||||
if (param->max_step < *stp) *stp = param->max_step;
|
||||
if (*stp < param->min_step)
|
||||
*stp = param->min_step;
|
||||
if (param->max_step < *stp)
|
||||
*stp = param->max_step;
|
||||
|
||||
/*
|
||||
If an unusual termination is to occur then let
|
||||
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;
|
||||
}
|
||||
|
||||
@ -928,7 +840,8 @@ static int line_search_morethuente(
|
||||
return LBFGSERR_MAXIMUMLINESEARCH;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
@ -936,7 +849,8 @@ static int line_search_morethuente(
|
||||
In the first stage we seek a step for which the modified
|
||||
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;
|
||||
}
|
||||
|
||||
@ -960,12 +874,8 @@ static int line_search_morethuente(
|
||||
Call update_trial_interval() to update the interval of
|
||||
uncertainty and to compute the new step.
|
||||
*/
|
||||
uinfo = update_trial_interval(
|
||||
&stx, &fxm, &dgxm,
|
||||
&sty, &fym, &dgym,
|
||||
stp, &fm, &dgm,
|
||||
stmin, stmax, &brackt
|
||||
);
|
||||
uinfo = update_trial_interval(&stx, &fxm, &dgxm, &sty, &fym, &dgym, stp,
|
||||
&fm, &dgm, stmin, stmax, &brackt);
|
||||
|
||||
/* Reset the function and gradient values for f. */
|
||||
fx = fxm + stx * dgtest;
|
||||
@ -977,12 +887,8 @@ static int line_search_morethuente(
|
||||
Call update_trial_interval() to update the interval of
|
||||
uncertainty and to compute the new step.
|
||||
*/
|
||||
uinfo = update_trial_interval(
|
||||
&stx, &fx, &dgx,
|
||||
&sty, &fy, &dgy,
|
||||
stp, f, &dg,
|
||||
stmin, stmax, &brackt
|
||||
);
|
||||
uinfo = update_trial_interval(&stx, &fx, &dgx, &sty, &fy, &dgy, stp, f,
|
||||
&dg, stmin, stmax, &brackt);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -1000,13 +906,10 @@ static int line_search_morethuente(
|
||||
return LBFGSERR_LOGICERROR;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Define the local variables for computing minimizers.
|
||||
*/
|
||||
#define USES_MINIMIZER \
|
||||
lbfgsfloatval_t a, d, gamma, theta, p, q, r, s;
|
||||
#define USES_MINIMIZER lbfgsfloatval_t a, d, gamma, theta, p, q, r, s;
|
||||
|
||||
/**
|
||||
* 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)) */ \
|
||||
a = theta / s; \
|
||||
gamma = s * sqrt(a * a - ((du) / s) * ((dv) / s)); \
|
||||
if ((v) < (u)) gamma = -gamma; \
|
||||
if ((v) < (u)) \
|
||||
gamma = -gamma; \
|
||||
p = gamma - (du) + theta; \
|
||||
q = gamma - (du) + gamma + (dv); \
|
||||
r = p / q; \
|
||||
@ -1056,12 +960,13 @@ static int line_search_morethuente(
|
||||
/* gamma = s*sqrt((theta/s)**2 - (du/s) * (dv/s)) */ \
|
||||
a = theta / 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; \
|
||||
q = gamma - (dv) + gamma + (du); \
|
||||
r = p / q; \
|
||||
if (r < 0. && gamma != 0.) { \
|
||||
(cm) = (v) - r * d; \
|
||||
(cm) = (v)-r * d; \
|
||||
} else if (a < 0) { \
|
||||
(cm) = (xmax); \
|
||||
} else { \
|
||||
@ -1122,21 +1027,13 @@ static int line_search_morethuente(
|
||||
* guaranteed sufficient decrease. ACM Transactions on Mathematical
|
||||
* Software (TOMS), Vol 20, No 3, pp. 286-307, 1994.
|
||||
*/
|
||||
static int update_trial_interval(
|
||||
lbfgsfloatval_t *x,
|
||||
lbfgsfloatval_t *fx,
|
||||
lbfgsfloatval_t *dx,
|
||||
lbfgsfloatval_t *y,
|
||||
lbfgsfloatval_t *fy,
|
||||
lbfgsfloatval_t *dy,
|
||||
lbfgsfloatval_t *t,
|
||||
lbfgsfloatval_t *ft,
|
||||
static int update_trial_interval(lbfgsfloatval_t *x, lbfgsfloatval_t *fx,
|
||||
lbfgsfloatval_t *dx, lbfgsfloatval_t *y,
|
||||
lbfgsfloatval_t *fy, lbfgsfloatval_t *dy,
|
||||
lbfgsfloatval_t *t, lbfgsfloatval_t *ft,
|
||||
lbfgsfloatval_t *dt,
|
||||
const lbfgsfloatval_t tmin,
|
||||
const lbfgsfloatval_t tmax,
|
||||
int *brackt
|
||||
)
|
||||
{
|
||||
const lbfgsfloatval_t tmax, int *brackt) {
|
||||
int bound;
|
||||
int dsign = fsigndiff(dt, dx);
|
||||
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]. */
|
||||
if (tmax < newt) newt = tmax;
|
||||
if (newt < tmin) newt = tmin;
|
||||
if (tmax < newt)
|
||||
newt = tmax;
|
||||
if (newt < tmin)
|
||||
newt = tmin;
|
||||
|
||||
/*
|
||||
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) {
|
||||
mq = *x + 0.66 * (*y - *x);
|
||||
if (*x < *y) {
|
||||
if (mq < newt) newt = mq;
|
||||
if (mq < newt)
|
||||
newt = mq;
|
||||
} else {
|
||||
if (newt < mq) newt = mq;
|
||||
if (newt < mq)
|
||||
newt = mq;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1291,45 +1192,31 @@ static int update_trial_interval(
|
||||
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;
|
||||
lbfgsfloatval_t norm = 0.;
|
||||
|
||||
for (i = start;i < n;++i) {
|
||||
for (i = start; i < n; ++i) {
|
||||
norm += fabs(x[i]);
|
||||
}
|
||||
|
||||
return norm;
|
||||
}
|
||||
|
||||
static void owlqn_pseudo_gradient(
|
||||
lbfgsfloatval_t* pg,
|
||||
const lbfgsfloatval_t* x,
|
||||
const lbfgsfloatval_t* g,
|
||||
const int n,
|
||||
const lbfgsfloatval_t c,
|
||||
const int start,
|
||||
const int end
|
||||
)
|
||||
{
|
||||
static void owlqn_pseudo_gradient(lbfgsfloatval_t *pg, const lbfgsfloatval_t *x,
|
||||
const lbfgsfloatval_t *g, const int n,
|
||||
const lbfgsfloatval_t c, const int start,
|
||||
const int end) {
|
||||
int i;
|
||||
|
||||
/* Compute the negative of gradients. */
|
||||
for (i = 0;i < start;++i) {
|
||||
for (i = 0; i < start; ++i) {
|
||||
pg[i] = g[i];
|
||||
}
|
||||
|
||||
/* Compute the psuedo-gradients. */
|
||||
for (i = start;i < end;++i) {
|
||||
for (i = start; i < end; ++i) {
|
||||
if (x[i] < 0.) {
|
||||
/* Differentiable. */
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
int i;
|
||||
|
||||
for (i = start;i < end;++i) {
|
||||
for (i = start; i < end; ++i) {
|
||||
if (d[i] * sign[i] <= 0) {
|
||||
d[i] = 0;
|
||||
}
|
||||
|
@ -350,7 +350,7 @@ static YAP_Bool lbfgs_release(void) {
|
||||
/* 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_Term arg = YAP_ArgOfTerm(ar, t);
|
||||
return (lbfgs_parameter_t *)YAP_IntOfTerm(arg);
|
||||
@ -516,7 +516,6 @@ static YAP_Bool lbfgs_set_parameter(void) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
/** @pred lbfgs_get_parameter(+Name,-Value)</h3>
|
||||
Get the current Value for Name
|
||||
*/
|
||||
|
@ -144,7 +144,7 @@ listing(Stream, [MV|MVs]) :- !,
|
||||
'$listing'(_,_,_,_).
|
||||
|
||||
'$funcspec'(Name/Arity,Name,Arity) :- !, atom(Name).
|
||||
'$funcspec'(Name,Name,_) :- atom(Name), !.
|
||||
'$funcspec'(Name,Name,0) :- atom(Name), !.
|
||||
'$funcspec'(Name,_,_) :-
|
||||
'$do_error'(domain_error(predicate_spec,Name),listing(Name)).
|
||||
|
||||
@ -216,7 +216,7 @@ listing(Stream, [MV|MVs]) :- !,
|
||||
'$clause'(Pred, M, Body, _),
|
||||
'$current_module'(Mod),
|
||||
( M \= Mod -> H = M:Pred ; H = Pred ),
|
||||
'$portray_clause'(Stream,(H:-Body)),
|
||||
portray_clause(Stream,(H:-Body)),
|
||||
fail.
|
||||
|
||||
/** @pred portray_clause(+ _S_,+ _C_)
|
||||
|
Reference in New Issue
Block a user