stackprobe7s_memo

何処にも披露する見込みの無いものを書き落とす場所

再帰的探索の再起呼び出(し|さない)

再起呼び出し

// protected bool IsInvalid(int index);        :  0 ~ (index - 1) の element の並びが間違っていれば ture そうでなければ false
// protected bool IsEnd(int index);            :  0 ~ (index - 1) の element の並びで完成していれば true そうでなければ false
// protected void Ended(int count);            :  element の並びが完成したことを通知する。count は element の個数
// protected void ResetElement(int index);     :  index の element を初期化する。次の MoveNextElement によって最初の値に遷移する。
// protected bool MoveNextElement(int index);  :  index の element を次の値に遷移する。次の値が無い場合のみ false を返す。

public virtual void Perform()
{
	this.Search(0);
}

private void Search(int index)
{
	if (this.IsInvalid(index))
		return;

	if (this.IsEnd(index))
	{
		this.Ended(index);
		return;
	}
	this.ResetElement(index);

	while (this.MoveNextElement(index))
	{
		this.Search(index + 1);
	}
}

再起呼び出さない

Search()を再帰的に呼び出さないように変更

public override void Perform()
{
	this.Search();
}

private void Search()
{
	int index = -1;

forward:
	if (this.IsInvalid(++index))
		goto back;

	if (this.IsEnd(index))
	{
		this.Ended(index);
		goto back;
	}
	this.ResetElement(index);

next:
	if (this.MoveNextElement(index))
		goto forward;

back:
	if (0 <= --index)
		goto next;
}

再起呼び出さない_2

-= goto;

public override void Perform()
{
	this.Search();
}

private void Search()
{
	bool forward = true;
	int index = 0;

	for (; ; )
	{
		if (forward)
		{
			if (this.IsInvalid(index))
			{
				forward = false;
			}
			else if (this.IsEnd(index))
			{
				this.Ended(index);
				forward = false;
			}
			else
			{
				this.ResetElement(index);
				forward = this.MoveNextElement(index);
			}
		}
		else
		{
			forward = this.MoveNextElement(index);
		}

		if (forward)
		{
			index++;
		}
		else
		{
			if (index <= 0)
				break;

			index--;
		}
	}
}