'''
Created on Sep 12, 2012
@author: Dilum Bandara
@version: 0.1
@license: Apache License v2.0

   Copyright 2012 H. M. N. Dilum Bandara and Anura P. Jayasumana, Colorado State
   University

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
'''

import Config
import copy

class BaseHash(object):
    '''
    Base Hash class. Locality preserving hash functions
    '''
    def __init__(self, maxKey):
        '''
        Constructor
        @param maxKey: Maximum key 
        '''
        self.maxKey = maxKey


    def ULPHash(self, att):
        '''
        Uniform Locality Preserving hash for given attribute (from LORM paper)        
        @param att: Attribute to hash [name, value]
        @return: Hash value
        '''
        attrib = copy.deepcopy(att)
        if attrib[0] == 'X':
            if attrib[1] < Config.MIN_X: assert(False)
            if attrib[1] > Config.MAX_X: assert(False)
            return long(((attrib[1] - Config.MIN_X)/ \
                         float(Config.MAX_X - Config.MIN_X)) * self.maxKey)
        elif attrib[0] == 'Y':
            if attrib[1] < Config.MIN_Y: assert(False)
            if attrib[1] > Config.MAX_Y: assert(False)
            return long(((attrib[1] - Config.MIN_Y)/ \
                float(Config.MAX_Y - Config.MIN_Y)) * self.maxKey)
        elif attrib[0] == 'ELEV':
            if attrib[1] < Config.MIN_ELEV: assert(False)
            if attrib[1] > Config.MAX_ELEV: assert(False)
            return long(((attrib[1] - Config.MIN_ELEV)/ \
                float(Config.MAX_ELEV - Config.MIN_ELEV)) * self.maxKey)
        elif attrib[0] == 'DD':
            if attrib[1] < Config.MIN_DD: assert(False) 
            if attrib[1] > Config.MAX_DD: assert(False) 
            return long(((attrib[1] - Config.MIN_DD)/ \
                float(Config.MAX_DD - Config.MIN_DD)) * self.maxKey)
        elif attrib[0] == 'FF':
            if attrib[1] < Config.MIN_FF: assert(False)
            if attrib[1] > Config.MAX_FF: assert(False) 
            return long(((attrib[1] - Config.MIN_FF)/ \
                float(Config.MAX_FF - Config.MIN_FF)) * self.maxKey)
        elif attrib[0] == 'P':
            if attrib[1] < Config.MIN_P: assert(False) 
            if attrib[1] > Config.MAX_P: assert(False) 
            return long(((attrib[1] - Config.MIN_P)/ \
                float(Config.MAX_P - Config.MIN_P)) * self.maxKey)
        elif attrib[0] == 'RH':
            if attrib[1] < Config.MIN_RH: assert(False) 
            if attrib[1] > Config.MAX_RH: assert(False) 
            return long(((attrib[1] - Config.MIN_RH)/ \
                float(Config.MAX_RH - Config.MIN_RH)) * self.maxKey)
        elif attrib[0] == 'T':
            if attrib[1] < Config.MIN_T: assert(False)
            if attrib[1] > Config.MAX_T: assert(False)
            return long(((attrib[1] - Config.MIN_T)/ \
                float(Config.MAX_T - Config.MIN_T)) * self.maxKey)
        else: assert(False), 'Unknown attribute ' + attrib[0]