# -*- coding: utf-8 -*-

import math
import random
import hashlib
import numpy as np
from OUEdisturb import SingleDataEncode

def RapporParams(epsilon):
    """计算OUE法扰动概率"""
    # epsilon 隐私预算
    
    temp_data=math.pow(math.e,epsilon*0.5)
    prob_p = float(temp_data)/(temp_data+1)
    prob_q = 1.0/(temp_data+1)
    
    return [prob_p,prob_q]

def SingleDataDisturb(encode_single_user_data,rappor_params):
    """扰动单个用户数据"""
    # encode_single_user_data 单个用户数据编码
    # rappor_params rappor法扰动概率
    
    prob_p=rappor_params[0]
    prob_q=rappor_params[1]
    temp_length=len(encode_single_user_data)
    noise_single_user_data=[0 for _ in xrange(temp_length)]
    
    for i in xrange(temp_length):
        temp_number=random.random()
        if encode_single_user_data[i]==0:
            if temp_number < prob_q: 
                noise_single_user_data[i]=1
            else:
                noise_single_user_data[i]=0
        else:
            if temp_number < prob_p: 
                noise_single_user_data[i]=1
            else:
                noise_single_user_data[i]=0
    
    return noise_single_user_data
    
def RapporAllDataDisturb(all_user_data,range_list,layer_number,rappor_params):
    """扰动所有用户数据"""
    # all_user_data 所有用户数据列表
    # range_list 四分树各层结点范围
    # layer_number 四分树的高度
    # rappor_params rappor法扰动概率
    
    noise_all_user_data=[]
    
    for i in all_user_data:
        encode_single_user_data=SingleDataEncode(i,range_list,layer_number)
        noise_single_user_data=SingleDataDisturb(encode_single_user_data,rappor_params)
        noise_all_user_data.append(noise_single_user_data)
    
    return noise_all_user_data






