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

import math
import random
import hashlib
import numpy as np

def AggregateData(noise_all_user_data,layer_number):
    """收集者聚合用户数据"""
    # noise_all_user_data 所有用户噪声数据
    # layer_number 四分树的高度

    noise_aggregate_all_data=[]
    aggregate_level_user_number=[0 for _ in xrange(layer_number)] # 每一层收集的报告数
    
    for i in xrange(layer_number):
        temp_number=int(math.pow(4,i))
        temp_list=[0 for _ in xrange(temp_number)]
        noise_aggregate_all_data.append(temp_list)
    
    for i in noise_all_user_data:
        temp_length=len(i)
        temp_num=int(math.log(temp_length,4)) #判断用户所选择的层数
        aggregate_level_user_number[temp_num] +=1
        for j in xrange(temp_length):
            noise_aggregate_all_data[temp_num][j] +=i[j]
    
    return noise_aggregate_all_data,aggregate_level_user_number

def KrrAggregateData(noise_all_user_data,layer_number):
    """收集者聚合用户数据"""
    # noise_all_user_data 所有用户噪声数据
    # layer_number 四分树的高度

    noise_aggregate_all_data=[]
    aggregate_level_user_number=[0 for _ in xrange(layer_number)] # 每一层收集的报告数
    
    for i in xrange(layer_number):
        temp_number=int(math.pow(4,i))
        temp_list=[0 for _ in xrange(temp_number)]
        noise_aggregate_all_data.append(temp_list)
    
    for i in noise_all_user_data:
        temp_level=i[0]
        temp_index=i[1]
        aggregate_level_user_number[temp_level] +=1
        noise_aggregate_all_data[temp_level][temp_index] +=1
    
    return noise_aggregate_all_data,aggregate_level_user_number

    
