
// Index class
// ===========

import java.io.*;

import java.util.*;

public class Index
/*	methods include
		size
		elementAt
		contains
		indexOf
		addElement
		open
		close
		save
		load
		print
*/
{

	Vector v; // the index's home in main memory
	RandomAccessFile file; // the index's home on the backing store

	public Index () // constructor
	{
		v = new Vector(10);
	}

	public int size()
	// return the number of index elements in the index
	{
		return v.size();
	}

	public IndexElem elementAt(int pos)
	// return the index element at the requested position
	{
		return (IndexElem) v.elementAt(pos);
	}



	public boolean contains(String searchKey)
	// given a search key, this method looks at each index element.
	// if it finds the required key, it returns "true"; "false" otherwise.
	{
		boolean res = false;
		Enumeration e = v.elements();
		IndexElem ie;
		while (e.hasMoreElements() && res == false)
		{
			ie = (IndexElem)e.nextElement();
			res = searchKey.equals(ie.getKey());
		}
		return res;
	} // end of method "contains"

	public int indexOf(String searchKey)
	// given a search key, this method examines each index element.
	// when it finds a match, it returns the position of the element
	// within the index; 0 otherwise.
	{
		boolean flag = false;
		int res = -1, count = 0;
		Enumeration e = v.elements();
		IndexElem ie;
		while (e.hasMoreElements() && flag == false)
		{
			ie = (IndexElem)e.nextElement();
			flag = searchKey.equals(ie.getKey());
			if (flag) res = count;
			count++;
		}
		return res;
	} // end of method "indexOf"

	public void addElement(IndexElem pie)
	// this method adds an index element to the index, maintaining
	// the sorted order
	{
	// find out where the element belongs

		boolean flag = false;
		int cres = 0, res = 0, count = 0;
		Enumeration e = v.elements();
		String add, current;
		IndexElem ie;

		// special case -- empty vector
		int vSize = v.size();
		if (vSize == 0)
		{
			v.addElement(pie);
			return;
		}

		add = pie.getKey();

		while (e.hasMoreElements() && flag == false)
		{
			ie = (IndexElem)e.nextElement();
			current = ie.getKey();
			cres = add.compareTo(current);
			if (cres <= 0)
			{
				flag = true;
				res = count;
			}
			count++;
		}

		// if the new string is larger than the last element
		if (flag == false) res = count;

		// at last we know where the new index element belongs ..
		v.insertElementAt(pie,res);
	} // end of method "addElement"

	// input/output methods; for the persistency of the index

	public boolean open(String fileName)
	{
		boolean res = true;
		try	{
			file = new RandomAccessFile(fileName,"rw");
		}
		catch (IOException e) {
			System.out.println("problem in Index.open");
			System.exit(0);
			res = false;
		}
		return res;
	} // end of "open"

	public void close()
	{
		try	{
			file.close();
		}
		catch (IOException e)
		    {
		    // not doing anything intelligent
		}
	}

	public void save() throws IOException
	// saves the index elements in the file
	{
		Enumeration e = v.elements();
		IndexElem ie;
		int count = 0;

		int pos = (int) file.getFilePointer();
		file.seek((long)0);
		pos = (int) file.getFilePointer();

		while (e.hasMoreElements())
		{
			ie = (IndexElem)e.nextElement();
			ie.write(file,count);
			count++;
		}
	} // end of method "save"


	public void load() throws IOException
	// loads the index elements from the file into main memory
	{
		IndexElem ie = new IndexElem();
		boolean goon = true;
		int count = 0;

		file.seek((long)0);

		while (goon)
		{
			try	{
				ie.read(file, count);
				count++;
			}
			catch (IOException e)
			    {
				goon = false;
			}
			if (goon)
			{
				v.addElement(ie);
				ie = new IndexElem();
			}
		}
	} // end of method "load"
	
	public void print()
	// debugging method .. prints out all the index elements in
	// the index
	{
		Enumeration e = v.elements();
		IndexElem ie;
		System.out.println("index contains ..");
		while (e.hasMoreElements())
		{
			ie = (IndexElem)e.nextElement();
			System.out.println(ie);
		}
	} // end of method "print"

} // end of class Index

