fix nth_instance: fix bugs and actually add two versions (obs from Theofrastos Mantadelis)

This commit is contained in:
Vítor Manuel de Morais Santos Costa 2009-11-18 13:06:37 +00:00
parent f31aeff952
commit a316090f8c
4 changed files with 40 additions and 21 deletions

View File

@ -3993,7 +3993,7 @@ find_next_clause(DBRef ref0)
UNLOCK(cl->ClLock); UNLOCK(cl->ClLock);
} }
#else #else
if (!DynamicFlags(newp) & InUseMask) { if (!(DynamicFlags(newp) & InUseMask)) {
DynamicFlags(newp) |= InUseMask; DynamicFlags(newp) |= InUseMask;
TRAIL_CLREF(ClauseCodeToDynamicClause(newp)); TRAIL_CLREF(ClauseCodeToDynamicClause(newp));
} }
@ -5489,7 +5489,7 @@ Yap_InitDBPreds(void)
Yap_InitCPred("total_erased", 4, p_total_erased, SyncPredFlag); Yap_InitCPred("total_erased", 4, p_total_erased, SyncPredFlag);
Yap_InitCPred("key_erased_statistics", 5, p_key_erased_statistics, SyncPredFlag); Yap_InitCPred("key_erased_statistics", 5, p_key_erased_statistics, SyncPredFlag);
Yap_InitCPred("heap_space_info", 3, p_heap_space_info, SyncPredFlag); Yap_InitCPred("heap_space_info", 3, p_heap_space_info, SyncPredFlag);
Yap_InitCPred("$nth_instance", 4, p_nth_instance, SyncPredFlag); Yap_InitCPred("$nth_instance", 3, p_nth_instance, SyncPredFlag);
Yap_InitCPred("$nth_instancep", 3, p_nth_instancep, SyncPredFlag|HiddenPredFlag); Yap_InitCPred("$nth_instancep", 3, p_nth_instancep, SyncPredFlag|HiddenPredFlag);
Yap_InitCPred("$jump_to_next_dynamic_clause", 0, p_jump_to_next_dynamic_clause, SyncPredFlag|HiddenPredFlag); Yap_InitCPred("$jump_to_next_dynamic_clause", 0, p_jump_to_next_dynamic_clause, SyncPredFlag|HiddenPredFlag);
Yap_InitCPred("$install_thread_local", 2, p_install_thread_local, SafePredFlag|HiddenPredFlag); Yap_InitCPred("$install_thread_local", 2, p_install_thread_local, SafePredFlag|HiddenPredFlag);

View File

@ -7132,12 +7132,12 @@ Yap_NthClause(PredEntry *ap, Int ncls)
yamop **jlbl = NULL; yamop **jlbl = NULL;
/* search every clause */ /* search every clause */
if (ncls == 1) if (ncls > ap->cs.p_code.NOfClauses)
return NULL;
else if (ncls == 1)
return to_clause(ap->cs.p_code.FirstClause,ap); return to_clause(ap->cs.p_code.FirstClause,ap);
else if (ncls == ap->cs.p_code.NOfClauses) else if (ncls == ap->cs.p_code.NOfClauses)
return to_clause(ap->cs.p_code.LastClause,ap); return to_clause(ap->cs.p_code.LastClause,ap);
else if (ncls > ap->cs.p_code.NOfClauses)
return NULL;
else if (ncls < 0) else if (ncls < 0)
return NULL; return NULL;

View File

@ -5966,16 +5966,6 @@ elements of the internal data-base that match the key.
match the reference. match the reference.
@end itemize @end itemize
@item nth_instance(?@var{K},?@var{Index},@var{T},?@var{R})
@findex nth_recorded/3
@saindex nth_recorded/3
@cnindex nth_recorded/3
Fetches the @var{Index}nth entry in the internal database under the
key @var{K}. Entries are numbered from one. If the key @var{K} are the
@var{Index} are bound, a reference is unified with @var{R}. Otherwise,
the reference @var{R} must be given, and the term the system will find
the matching key and index.
@item erase(+@var{R}) @item erase(+@var{R})
@findex erase/1 @findex erase/1
@saindex erase/1 @saindex erase/1
@ -6015,6 +6005,27 @@ Defines the relation: @var{K} is a currently defined database key whose
name is the atom @var{A}. It can be used to generate all the keys for name is the atom @var{A}. It can be used to generate all the keys for
the internal data-base. the internal data-base.
@item nth_instance(?@var{Key},?@var{Index},?@var{R})
@findex nth_instance/3
@saindex nth_instance/3
@cnindex nth_instance/3
Fetches the @var{Index}nth entry in the internal database under the key
@var{Key}. Entries are numbered from one. If the key @var{Key} or the
@var{Index} are bound, a reference is unified with @var{R}. Otherwise,
the reference @var{R} must be given, and YAP will find
the matching key and index.
@item nth_instance(?@var{Key},?@var{Index},@var{T},?@var{R})
@findex nth_instance/4
@saindex nth_instance/4
@cnindex nth_instance/4
Fetches the @var{Index}nth entry in the internal database under the key
@var{Key}. Entries are numbered from one. If the key @var{Key} or the
@var{Index} are bound, a reference is unified with @var{R}. Otherwise,
the reference @var{R} must be given, and YAP will find
the matching key and index.
@item key_statistics(+@var{K},-@var{Entries},-@var{Size},-@var{IndexSize}) @item key_statistics(+@var{K},-@var{Entries},-@var{Size},-@var{IndexSize})
@findex key_statistics/4 @findex key_statistics/4
@snindex key_statistics/4 @snindex key_statistics/4

View File

@ -472,12 +472,20 @@ callable(V) :- var(V), !, fail.
callable(V) :- atom(V), !. callable(V) :- atom(V), !.
callable(V) :- functor(V,_,Ar), Ar > 0. callable(V) :- functor(V,_,Ar), Ar > 0.
nth_instance(X,Y,Z) :- nth_instance(Key,Index,Ref) :-
nonvar(X), var(Y), var(Z), !, nonvar(Key), var(Index), var(Ref), !,
recorded(X,_,Z), recorded(Key,_,Ref),
'$nth_instance'(_,Y,Z). '$nth_instance'(_,Index,Ref).
nth_instance(X,Y,Z) :- nth_instance(Key,Index,Ref) :-
'$nth_instance'(X,Y,Z). '$nth_instance'(Key,Index,Ref).
nth_instance(Key,Index,T,Ref) :-
nonvar(Key), var(Index), var(Ref), !,
recorded(Key,T,Ref),
'$nth_instance'(_,Index,Ref).
nth_instance(Key,Index,T,Ref) :-
'$nth_instance'(Key,Index,Ref),
instance(Ref,T).
nb_current(GlobalVariable, Val) :- nb_current(GlobalVariable, Val) :-
var(GlobalVariable), !, var(GlobalVariable), !,