add support for sized queues.

This commit is contained in:
Vitor Santos Costa 2009-02-27 22:22:49 +00:00
parent 0b4b0d0a18
commit d98c3f0817
2 changed files with 33 additions and 10 deletions

View File

@ -1417,23 +1417,16 @@ p_nb_create2(void)
/* a non-backtrackable queue is a term of the form $array(Arena,Start,End,Size) plus an Arena. */
static Int
p_nb_queue(void)
static Int
nb_queue(UInt arena_sz)
{
Term queue_arena, queue, ar[5], *nar;
#if COROUTINING
Term delay_queue_arena;
#endif
Term t = Deref(ARG1);
UInt arena_sz = (ASP-H)/16;
DepthArenas++;
if (DepthArenas > 1)
arena_sz /= log(MIN_ARENA_SIZE);
if (arena_sz < MIN_ARENA_SIZE)
arena_sz = MIN_ARENA_SIZE;
if (arena_sz > MAX_ARENA_SIZE)
arena_sz = MAX_ARENA_SIZE;
DepthArenas++;
if (!IsVarTerm(t)) {
if (!IsApplTerm(t)) {
return FALSE;
@ -1478,6 +1471,34 @@ p_nb_queue(void)
return TRUE;
}
static Int
p_nb_queue(void)
{
UInt arena_sz = (ASP-H)/16;
if (DepthArenas > 1)
arena_sz /= log(MIN_ARENA_SIZE);
if (arena_sz < MIN_ARENA_SIZE)
arena_sz = MIN_ARENA_SIZE;
if (arena_sz > MAX_ARENA_SIZE)
arena_sz = MAX_ARENA_SIZE;
return nb_queue(arena_sz);
}
static Int
p_nb_queue_sized(void)
{
Term t = Deref(ARG2);
if (IsVarTerm(t)) {
Yap_Error(INSTANTIATION_ERROR,t,"nb_queue");
return FALSE;
}
if (!IsIntegerTerm(t)) {
Yap_Error(TYPE_ERROR_INTEGER,t,"nb_queue");
return FALSE;
}
return nb_queue((UInt)IntegerOfTerm(t));
}
static CELL *
GetQueue(Term t, char* caller)
{
@ -2576,6 +2597,7 @@ void Yap_InitGlobals(void)
Yap_InitCPredBack("$nb_current", 1, 1, init_current_nb, cont_current_nb, SafePredFlag);
CurrentModule = GLOBALS_MODULE;
Yap_InitCPred("nb_queue", 1, p_nb_queue, 0L);
Yap_InitCPred("nb_queue", 2, p_nb_queue_sized, 0L);
Yap_InitCPred("nb_queue_close", 3, p_nb_queue_close, SafePredFlag);
Yap_InitCPred("nb_queue_enqueue", 2, p_nb_queue_enqueue, 0L);
Yap_InitCPred("nb_queue_dequeue", 2, p_nb_queue_dequeue, SafePredFlag);

View File

@ -17,6 +17,7 @@
:- module(nb, [
nb_queue/1,
nb_queue/2,
nb_queue_close/3,
nb_queue_enqueue/2,
nb_queue_dequeue/2,