more exo stuff

This commit is contained in:
Vítor Santos Costa 2013-04-25 09:48:06 -05:00
parent 52253e3e9e
commit 2c49edb975
4 changed files with 108 additions and 19 deletions

View File

@ -57,7 +57,7 @@ compare(const BITS32 *ip, Int j USES_REGS) {
static void
RangeUDIRefitIndex(struct index_t **ip, UInt b[] USES_REGS)
IntervalUDIRefitIndex(struct index_t **ip, UInt b[] USES_REGS)
{
size_t sz;
struct index_t *it = *ip;
@ -65,7 +65,7 @@ RangeUDIRefitIndex(struct index_t **ip, UInt b[] USES_REGS)
UInt arity = it->arity;
yamop *code;
/* hard-wired implementation for the range case */
/* hard-wired implementation for the Interval case */
Int i = it->udi_arg;
/* it is bound, use hash */
if (it->bmap & b[i]) return;
@ -263,7 +263,7 @@ All(struct index_t *it, BITS32 off USES_REGS)
}
static yamop *
RangeEnterUDIIndex(struct index_t *it USES_REGS)
IntervalEnterUDIIndex(struct index_t *it USES_REGS)
{
Int i = it->udi_arg;
Term t = XREGS[i+1], a1;
@ -324,7 +324,7 @@ RangeEnterUDIIndex(struct index_t *it USES_REGS)
}
static int
RangeRetryUDIIndex(struct index_t *it USES_REGS)
IntervalRetryUDIIndex(struct index_t *it USES_REGS)
{
CELL *w = (CELL*)(B+1);
BITS32 *end = (BITS32 *) w[it->arity+2],
@ -338,7 +338,7 @@ RangeRetryUDIIndex(struct index_t *it USES_REGS)
}
static struct udi_control_block RangeCB;
static struct udi_control_block IntervalCB;
typedef struct exo_udi_access_t {
CRefitExoIndex refit;
@ -347,13 +347,13 @@ typedef struct exo_udi_access_t {
static struct exo_udi_access_t ExoCB;
static void *
RangeUdiInit (Term spec, int arg, int arity) {
ExoCB.refit = RangeUDIRefitIndex;
IntervalUdiInit (Term spec, int arg, int arity) {
ExoCB.refit = IntervalUDIRefitIndex;
return (void *)&ExoCB;
}
static void *
RangeUdiInsert (void *control,
IntervalUdiInsert (void *control,
Term term, int arg, void *data)
{
CACHE_REGS
@ -361,30 +361,30 @@ RangeUdiInsert (void *control,
struct index_t **ip = (struct index_t **)term;
(*ip)->udi_arg = arg-1;
(ExoCB.refit)(ip, LOCAL_ibnds PASS_REGS);
(*ip)->udi_first = (void *)RangeEnterUDIIndex;
(*ip)->udi_next = (void *)RangeRetryUDIIndex;
(*ip)->udi_first = (void *)IntervalEnterUDIIndex;
(*ip)->udi_next = (void *)IntervalRetryUDIIndex;
return control;
}
static int RangeUdiDestroy(void *control)
static int IntervalUdiDestroy(void *control)
{
return TRUE;
}
void Yap_udi_range_init(void) {
UdiControlBlock cb = &RangeCB;
void Yap_udi_Interval_init(void) {
UdiControlBlock cb = &IntervalCB;
memset((void *) cb,0, sizeof(*cb));
/*TODO: ask vitor why this gives a warning*/
cb->decl=Yap_LookupAtom("range");
cb->decl=Yap_LookupAtom("exo_interval");
cb->init= RangeUdiInit;
cb->insert=RangeUdiInsert;
cb->init= IntervalUdiInit;
cb->insert=IntervalUdiInsert;
cb->search=NULL;
cb->destroy=RangeUdiDestroy;
cb->destroy=IntervalUdiDestroy;
Yap_UdiRegister(cb);
}

View File

@ -197,6 +197,7 @@ Subnodes of Library
* Apply:: SWI-Compatible Apply library.
* Association Lists:: Binary Tree Implementation of Association Lists.
* AVL Trees:: Predicates to add and lookup balanced binary trees.
* Exo Intervals:: Play with the UDI and exo-compilation
* Heaps:: Labelled binary tree where the key of each node is less
than or equal to the keys of its children.
* Lambda:: Ulrich Neumerkel's Lambda Library
@ -8698,6 +8699,7 @@ Library, Extensions, Built-ins, Top
* Block Diagram:: Block Diagrams of Prolog code
* Cleanup:: Call With registered Cleanup Calls
* DGraphs:: Directed Graphs Implemented With Red-Black Trees
* Exo Intervals:: Play with the UDI and exo-compilation
* Heaps:: Labelled binary tree where the key of each node is less
than or equal to the keys of its children.
* LAM:: LAM MPI
@ -9044,7 +9046,7 @@ have key @var{Key}.
@end table
@node AVL Trees, Heaps, Association Lists, Library
@node AVL Trees, Exo Intervals, Association Lists, Library
@section AVL Trees
@cindex AVL trees
@ -9082,7 +9084,30 @@ Lookup an element with key @var{Key} in the AVL tree
@end table
@node Heaps, Lists, AVL Trees, Library
@node Exo Intervals, Heaps, AVL Trees, Library
@section AVL Trees
@cindex AVL trees
This package assumes you use UDI exo-indexing, that is:
@example
:- udi(diagnoses(exo_interval,?,?)).
:- load_files(db, [consult(exo)]).
@end example
It is designed to optimise the following type of queries:
@example
?- max(X, diagnoses(X, 9, Y)).
?- min(X, diagnoses(X, 9, 36211117)).
?- X #< Y, min(X, diagnoses(9, X, 36211117)), diagnoses(Y, 9, _).
@end example
The first argument gives the time, the second the patient, and the
third the condition code. The first query should find the last time
the patient 9 had any code reported, the second looks for the first
report of code 36211117, and the last searches for reports after this
one. All queries be bound by constant or log(n) time.
@node Heaps, Lists, Exo Intervals, Library
@section Heaps
@cindex heap

View File

@ -40,6 +40,7 @@ PROGRAMS= \
$(srcdir)/dbqueues.yap \
$(srcdir)/dbusage.yap \
$(srcdir)/dgraphs.yap \
$(srcdir)/exo_interval.yap \
$(srcdir)/expand_macros.yap \
$(srcdir)/gensym.yap \
$(srcdir)/hacks.yap \

63
library/exo_interval.yap Normal file
View File

@ -0,0 +1,63 @@
% This file has been included as an YAP library by Vitor Santos Costa, 2013
% it implements a very simple interval solver designed to interact with the exo
% data-base.
% It assumes simple queries and a contiguous interval,
% and does not really expect to do non-trivial
% constraint propagation and solving.
:- module(exo_interval,
[max/2,
min/2,
(#<)/2,
(#>)/2,
(#=)/2,
op(700, xfx, (#>)),
op(700, xfx, (#<)),
op(700, xfx, (#=))]).
:- meta_predicate
max(?,0),
min(?,0).
max(X, G) :-
attvar(X),
get_attr(X, exo_interval, Atts), !,
throw(error('cannot handle combination of attributes ')).
max(X, G) :-
var(X),
put_attr(X, exo_interval, max),
call(G).
min(X, G) :-
attvar(X),
get_attr(X, exo_interval, Atts), !,
throw(error('cannot handle combination of attributes ')).
min(X, G) :-
var(X),
put_attr(X, exo_interval, min),
call(G).
X #> Y :-
( var(X) -> put_attr(X, exo_interval, '>'(Y) ) ; true ),
( var(Y) -> put_attr(X, exo_interval, '<'(X) ) ; true ),
when((nonvar(X), nonvar(Y)), X > Y).
X #< Y :-
( var(X) -> put_attr(X, exo_interval, '<'(Y) ) ; true ),
( var(Y) -> put_attr(X, exo_interval, '>'(X) ) ; true ),
when((nonvar(X), nonvar(Y)), X < Y).
X #= Y :-
X = Y,
( var(X) -> put_attr(X, exo_interval, '='(Y) ) ; true ).
attribute_goals(X) -->
{ get_attr(X, exo_interval, Op) },
( { Op = max } -> [max(X)] ;
{ Op = min } -> [min(X)] ;
{ Op = '>'(Y) } -> [X #> Y] ;
{ Op = '<'(Y) } -> [X #< Y] ;
{ Op = '='(Y) } -> [X #= Y] ).