btreecursoruos.java

来自「国外的数据结构与算法分析用书」· Java 代码 · 共 85 行

JAVA
85
字号
/* BtreeCursorUos.java
 * ---------------------------------------------
 * Copyright (c) 2001 University of Saskatchewan
 * All Rights Reserved
 * --------------------------------------------- */
 
package dslib.file;

import dslib.base.*;
import dslib.exception.NoCurrentItemUosException;

/**	A cursor for the Btree data structure. */
public class BtreeCursorUos implements KeyedCursorUos
{
	/**	The file address of the current block. */
	protected long address;

	/**	The current block. */
	protected BtreeBlockUos block;

	/**	The index for the current item in the current block of the leaf. */
	protected int index;
	
	/**	Create a new cursor. <br>
		Analysis: Time = O(1) 
		@param curAddress The file address of the block that has the current item
		@param curBlock The block that has the current item
		@param curIndex The index of the current item in the block */
	public BtreeCursorUos(long curAddress, BtreeBlockUos curBlock, int curIndex)
	{
		address = curAddress;
		block = curBlock;
		index = curIndex;
	}

	/**	Is there a current item?. <br>
		Analysis: Time = O(1) */
	public boolean itemExists()
	{
		return address != -1;
	}

	/**	The key associated with the current item. <br>
		Analysis: Time = O(1) <br>
		PRECONDITION:  <br>
		<ul>
			itemExists()
		</ul> */
	public Comparable itemKey() throws NoCurrentItemUosException
	{
		if (!itemExists())
			throw new NoCurrentItemUosException("A current item must exist");
			
		return block.getRecord(index).key;
	}

	/**	The current item in the BtreeFile. <br>
		Analysis: Time = O(1) <br>
		PRECONDITION:  <br>
		<ul>
			itemExists()
		</ul> */
	public Object item() throws NoCurrentItemUosException
	{
		if (!itemExists())
			throw new NoCurrentItemUosException("A current item must exist");
			
		return block.getRecord(index).item;
	}

	/**	The current key-item pair. <br>
		Analysis: Time = O(1) <br>
		PRECONDITION:  <br>
		<ul>
			itemExists()
		</ul> */
	public PairUos keyItemPair() throws NoCurrentItemUosException
	{
		if (!itemExists())
			throw new NoCurrentItemUosException("A current item must exist");
			
		return new PairUos(itemKey(), item());
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?