package simpledb;

public class HeapPageId implements PageId {

    int tableId;
    int pgNo;

    //.................................................................
    
    /**
     * Constructor. Create a page id structure for a specific page of a
     * specific table.
     *
     * @param tableId The table that is being referenced
     * @param pgNo The page number in that table.
     */
    public HeapPageId(int tableId, int pgNo) {
	this.tableId = tableId;
	this.pgNo = pgNo;
    }
    
    /** @return the table associated with this PageId */
    public int tableid() {
	return tableId;
    }
    
    /** 
     * @return the page number in the table tableid() associated with 
     *   this PageId 
     */
    public int pageno() {
	return pgNo;
    }

    /** 
     * @return a hash code for this page, represented by the concatenation of
     *   the table number and the page number (needed if a PageId is used as a
     *   key in a hash table in the BufferPool, for example.)
     * @see BufferPool 
     */
    public int hashCode() {
	int code = (tableId << 16) + pgNo;
	return code;
    }
    
    /** 
     * Compares one PageId to another.
     *
     * @param o The object to compare against (must be a PageId)
     * @return true if the objects are equal (e.g., page numbers and table
     *   ids are the same)
     */
    public boolean equals(Object o) {
	if (!(o instanceof HeapPageId))
	    return false;
	HeapPageId p = (HeapPageId)o;
	return tableId == p.tableId && pgNo == p.pgNo;
    }
    
    public int[] serialize() {
	int data[] = new int[2];

	data[0] = tableId;
	data[1] = pgNo;

	return data;
    }

    public String toString() {
        return "HeapPageId(" + tableId + ", " + pgNo + ")";
    }
}
