int DepthLimitedSearch( int swapOrder[], int pancake[], int pancakeLength, int depth, int lastSwap ){
  if( IsFinalState(pancake, pancakeLength) ) return depth;
  if( depth == 0 ) return CUTOFF;

  for( int i = 1 ; i < pancakeLength ; i++ ){
    if( i == lastSwap ) continue;
    SwapPancake( pancake, i );
    swapOrder[depth-1] = i;

    int result = DepthLimitedSearch( swapOrder, pancake, pancakeLength, depth-1, i);
    SwapPancake( pancake, i );
    if( result != CUTOFF ) return depth;
    swapOrder[depth-1] = 0;
  }
  return CUTOFF;
}
コード例 #2
0
int Pop() {
	State *currentState;

	currentState = myQueue->queueHead;
	
	if (IsEmpty()) {
		printf("%s", "The queue is empty, Pop operation cannot be done!");
		return 0;
	}

	// Is it the final state?
	if (IsFinalState(currentState)) {
		ShowResults();
		return 1;
	}
	else {

		// Find the position where the number zero of the current matrix is
		FindZeroPosition(currentState->matrix);

		// Is it possible to change the numbers within the matrix?
		if (CanMoveUp()) {
			CreateNewState(currentState, UP);
		}
		if (CanMoveDown()) {
			CreateNewState(currentState, DOWN);
		}
		if (CanMoveLeft()) {
			CreateNewState(currentState, LEFT);
		}
		if (CanMoveRight()) {
			CreateNewState(currentState, RIGHT);
		}
	}

	// Take out from the queue
	// Move the queue first state to the next element
	// Decrement one state of the queue, but we need to store it once we need to show the actions taken, at the end
	myQueue->queueHead = myQueue->queueHead->next;
	myQueue->numberOfElements--;

	// free(currentState);

	return 2;
}
コード例 #3
0
static INT_PTR VoiceState(WPARAM wParam, LPARAM lParam)
{
	VOICE_CALL *in = (VOICE_CALL *) wParam;
	if (in == NULL || in->cbSize < sizeof(VOICE_CALL) || in->moduleName == NULL || in->id == NULL)
		return 0;

	// Check if the call is aready in list
	VoiceCall *call = FindVoiceCall(in->moduleName, in->id, !IsFinalState(in->state));
	if (call == NULL)
		return 0;

	call->AppendCallerID(in->hContact, 
		(in->flags & VOICE_UNICODE) ? WcharToTchar(in->pwszName).get() : CharToTchar(in->pszName).get(),
		(in->flags & VOICE_UNICODE) ? WcharToTchar(in->pwszNumber).get() : CharToTchar(in->pszNumber).get());
	call->secure = (in->flags & VOICE_SECURE) != 0;

	if (in->state == VOICE_STATE_RINGING && call->hContact != NULL)
	{
		int aut = DBGetContactSettingWord(call->hContact, MODULE_NAME, "AutoAccept", AUTO_NOTHING);
		if (aut == AUTO_ACCEPT || aut == AUTO_DROP)
		{
			call->state = VOICE_STATE_RINGING;
			call->Notify(true, false, false, false);

			if (aut == AUTO_ACCEPT)
				Answer(call);
			else
				call->Drop();

			return 0;
		}
	}

	if (in->state == VOICE_STATE_TALKING)
		HoldOtherCalls(call);

	call->SetState(in->state);

	return 0;
}
コード例 #4
0
bool VoiceCall::IsFinished()
{
	return IsFinalState(state);
}
コード例 #5
0
ファイル: cdb_backup_state.c プロジェクト: 50wu/gpdb
/* ProcessInput: loops through the array of PGnotify * elements, and processes them.
 * Based on the current state and the notification message, it changes the current state.
 * It's important to sort the PGnotify* elements so that we don't get the notifications
 * out of the proper order.
 */
void
ProcessInput(BackupStateMachine * pStateMachine)
{
	int			i;

	if (pStateMachine->nArCount == 0)
	{
		/*
		 * NULL input means that the timer on the socket select expired with
		 * no notifications. This is fine, unless we've not yet achieved the
		 * STATE_SET_SERIALIZABLE state. If we've not yet achieved the
		 * STATE_SET_SERIALIZABLE state, we want to timeout after 10 timer
		 * expiries.
		 */
		if (pStateMachine->currentState == STATE_INITIAL || pStateMachine->currentState == STATE_BACKUP_STARTED)
		{
			pStateMachine->nWaits++;
			if (pStateMachine->nWaits == WAIT_COUNT_MAX)
			{
				pStateMachine->currentState = STATE_TIMEOUT;
				pStateMachine->bStatus = false;
			}
		}

		return;
	}


	qsort(pStateMachine->ppNotifyAr, pStateMachine->nArCount, sizeof(PGnotify *), cmpNotifyStructs);

	for (i = 0; i < pStateMachine->nArCount; i++)
	{
		PGnotify   *pNotify = pStateMachine->ppNotifyAr[i];

		/*
		 * Once we are in a final state, we don't move.
		 */
		if (IsFinalState(pStateMachine))
			return;

		/*
		 * If the Notification indicates that the backend failed, we move to
		 * the STATE_BACKUP_ERROR state, irregardless of which state we are
		 * currently in.
		 */
		if (strcasecmp(pStateMachine->pszNotifyRelNameFail, pNotify->relname) == 0)
		{
			pStateMachine->bStatus = false;
			pStateMachine->currentState = STATE_BACKUP_ERROR;
			return;
		}

		/*
		 * Getting here means we didn't get a taskrc of failure.  This is the
		 * simple, non error case.	The order of the taskids should be
		 * TASK_START, TASK_SET_SERIALIZABLE, TASK_GOTLOCKS, TASK_FINISH,
		 * which moves the state from STATE_INITIAL -> STATE_BACKUP_STARTED ->
		 * STATE_SET_SERIALIZABLE -> STATE_GOTLOCKS -> STATE_FINISHED.
		 */
		switch (pStateMachine->currentState)
		{
			case STATE_INITIAL:
				if (strcasecmp(pStateMachine->pszNotifyRelNameStart, pNotify->relname) == 0)
				{
					pStateMachine->currentState = STATE_BACKUP_STARTED;
				}
				else
				{
					pStateMachine->bStatus = false;
					pStateMachine->currentState = STATE_UNEXPECTED_INPUT;
				}
				break;

			case STATE_BACKUP_STARTED:
				if (strcasecmp(pStateMachine->pszNotifyRelNameStart, pNotify->relname) == 0)
					break;

				if (strcasecmp(pStateMachine->pszNotifyRelNameSetSerializable, pNotify->relname) == 0)
				{
					pStateMachine->currentState = STATE_SET_SERIALIZABLE;
					pStateMachine->bReceivedSetSerializable = true;
				}
				else
				{
					pStateMachine->bStatus = false;
					pStateMachine->currentState = STATE_UNEXPECTED_INPUT;
				}
				break;

			case STATE_SET_SERIALIZABLE:
				if (strcasecmp(pStateMachine->pszNotifyRelNameStart, pNotify->relname) == 0)
					break;

				if (strcasecmp(pStateMachine->pszNotifyRelNameSetSerializable, pNotify->relname) == 0)
				{
					pStateMachine->bReceivedSetSerializable = true;
					break;
				}

				if (strcasecmp(pStateMachine->pszNotifyRelNameGotLocks, pNotify->relname) == 0)
				{
					pStateMachine->currentState = STATE_SET_GOTLOCKS;
					pStateMachine->bReceivedGotLocks = true;
				}
				else
				{
					pStateMachine->bStatus = false;
					pStateMachine->currentState = STATE_UNEXPECTED_INPUT;
				}
				break;

			case STATE_SET_GOTLOCKS:
				if (strcasecmp(pStateMachine->pszNotifyRelNameStart, pNotify->relname) == 0)
					break;
				if (strcasecmp(pStateMachine->pszNotifyRelNameSetSerializable, pNotify->relname) == 0)
					break;

				if (strcasecmp(pStateMachine->pszNotifyRelNameGotLocks, pNotify->relname) == 0)
				{
					pStateMachine->bReceivedGotLocks = true;
					break;
				}

				if (strcasecmp(pStateMachine->pszNotifyRelNameSucceed, pNotify->relname) == 0)
				{
					pStateMachine->currentState = STATE_BACKUP_FINISHED;
				}
				else
				{
					pStateMachine->bStatus = false;
					pStateMachine->currentState = STATE_UNEXPECTED_INPUT;
				}
				break;

			default:
				break;
		}
	}
}