# Context¶

The following functions give an access to the heap where all the information on the events that had occurred on all the CSint variables created.

By events we mean changes in the domains of the CSint variables (like known, newMin. newMax, neq) caused by Constraint Propagation or constraint settings.

These functions will be used to create your own generation function, but are not needed when the primitives like cs_search() are used.

int cs_saveContext()

Saves the context, i.e. the domains and the constraints of all the CSint variables that are available when the call is made.

It returns an integer label used to call functions such as cs_forgetSaveContextUntil(), cs_acceptContextUntil() or cs_restoreContextUntil() take an integer label parameter.

void cs_forgetSaveContext()

Cancels the last cs_saveContext() call. This functions should be used instead of cs_acceptContext() inside cs_event* functions.

void cs_restoreAndSaveContext()

The context is restored as same as before the last cs_saveContext() call, and cs_saveContext() is called again.

void cs_acceptContext()

All the changes made on the CSint variables since the last cs_saveContext() call are accepted (i.e., this context cannot be restored any more). This function frees the memory of the heap since the last cs_saveContext() call.

As it cuts possible backtrack until the last cs_saveContext() call, cs_acceptContext() should not be called inside a cs_event* functions.

void cs_acceptAll()

All the changes made on the CSint variables since the first cs_saveContext() call are accepted. This function frees all the memory of the heap.

void cs_restoreContext()

The context is restored as same as before the last cs_saveContext() call.

void cs_restoreAll()

The context is restored as same as before the first cs_saveContext() call.

These following functions take an integer label as an argument. This label must be an integer returned by cs_saveContext().

void cs_forgetSaveContextUntil(int label)

Cancels the all the cs_saveContext() calls until the one referenced by label.

void cs_acceptContextUntil(int label)

All the changes made on the CSint variables since the cs_saveContext() call referenced by label are accepted (i.e., this context cannot be restored any more). This function frees the memory of the heap since the cs_saveContext() call referenced by label.

void cs_restoreContextUntil(int label)

The context is restored as same as before the cs_saveContext() call referenced by label.

Ex: the cs_search() procedure could be implemented as follows:

IZBOOL cs_search(CSint **allvars, int nbVars, CSint* (*findFreeVar)(CSint **allvars, int nbVars)) {
CSint *var = findFreeVar(allvars, nbVars);
if (var) {
int val;
cs_saveContext();

for (val = cs_getMin(var); val <= cs_getMax(var); val = cs_getNextValue(var, val)) {
if (cs_EQ(var, val) && cs_search(allvars, nbVars, findFreeVar))
return TRUE;

cs_restoreAndSaveContext();
}

cs_acceptContext();
return FALSE;
}
else {
return TRUE;
}
}