/*
 * Copyright (C) 2008 The Trustees of Columbia University in the City of New York. 
 * All rights reserved.
 *
 * See the file LICENSE included in this distribution for details.
 */

#include "chorddist.h"
#include <stdio.h>

ChordDist::ChordDist() {
  
}


ChordDist::~ChordDist() {
  
}

//Dhruv Apr 20
int32 ChordDist::getClockwise(BigInt dest, BigInt& diff)		//Assume that dest is the objkey of a chorddist object with same params.
{
	if(m_objKey <= dest)
	{
		diff = dest - m_objKey;
		return 0;
	}
	else
	{
		char base[3], keylen[3];                     //we don't expect to have keys longer than 999 bits for now.
	        sprintf(base, "%d", m_uiBase);
        	sprintf(keylen, "%d", m_uiKeyLen);
	        BigInt bBase (base, DEC_DIGIT);
        	BigInt bKLen (keylen, DEC_DIGIT);

		diff = powBigInt(bBase, bKLen) - (m_objKey - dest);
		return 0;
	}
	return 1;
}

//Dhruv Apr 20
int32 ChordDist::getKeyAt(uint32 iPos, BigInt& objKey) {

        char pos[3], base[3], keylen[3];

        sprintf(pos, "%d", iPos);
        sprintf(base, "%d", m_uiBase);
        sprintf(keylen, "%d", m_uiKeyLen);

        BigInt bPos (pos, DEC_DIGIT);
        BigInt bBase (base, DEC_DIGIT);
        BigInt bKLen (keylen, DEC_DIGIT);

        // Now there are 3 BigInts- iPos, m_uiBase, m_objKey.
        objKey = (powBigInt(bBase, bPos) + m_objKey) % powBigInt(bBase, bKLen);
        return 0;
}
