Example #1
0
func TestCentriodQueue(t *testing.T) {
	testQueue := utils.NewCentroidPriorityQueue()
	var dimensionality = 20
	fakeData := make([]float64, dimensionality, dimensionality)
	input := make([]types.Centroid, 5, 5)
	for i := range input {
		nextCentriod := itemset.NewCentroidSimple(dimensionality, int64(i))
		input[i] = nextCentriod
		for j := 0; j <= i; j++ {
			input[i].UpdateVector(fakeData)
		}
		testQueue.Enqueue(input[i])
	}
	//Remove the third centriod from the testQueue and the input
	testQueue.Remove(3)
	input = append(input[:3], input[4:]...)

	//Add another centoid to both the input and the testQueue
	nextCentriod := itemset.NewCentroidSimple(dimensionality, int64(9))
	input = append([]types.Centroid{nextCentriod}, input...)

	testQueue.Enqueue(nextCentriod)
	if testQueue.Size() != len(input) {
		t.Errorf("priorityQueue is not the correct size expected length: %v, actual length: %v", len(input), testQueue.Size())
	}
	for i, expectedValue := range input {
		actualValue := testQueue.Poll()
		if actualValue.GetID() != expectedValue.GetID() {
			t.Errorf("priorityQueue did not output the correct value at index: %v, expected: %v, actual %v.", i, expectedValue.GetID(), actualValue.GetID())
		}
	}
}
func NewKHHCentroidCounter(k int) *KHHCentroidCounter {
	newK := int(float64(k)*math.Log(float64(k))) * 4
	seed := int64(time.Now().UnixNano() / int64(time.Millisecond))
	priorityQueue := utils.NewCentroidPriorityQueue()
	frequentItems := make(map[int64]types.Centroid)
	countlist := make(map[int64]int64)
	var sketchTable [depth][width]int
	hashVector := make([]int64, depth)
	random := rand.New(rand.NewSource(seed))
	for i := 0; i < depth; i++ {
		hashVector[i] = random.Int63()
	}
	var result = &KHHCentroidCounter{
		depth:       depth,
		width:       width,
		sketchTable: sketchTable,
	}
	result.hashVector = hashVector
	result.k = newK
	result.countlist = countlist
	result.priorityQueue = priorityQueue
	result.frequentItems = frequentItems
	return result
}