Пример #1
0
func main() {
	var h hophash.HashMap

	var coder hophash.HashCoder
	coder = new(fnvHash)

	h = hophash.NewHophash(coder, 32, uint64(math.Pow(2, 16)))

	h.Add("key1", "value1")
	val, ok := h.Get("key1")
	if ok {
		fmt.Println(val)
	} else {
		fmt.Println("no value present")
	}
}
Пример #2
0
func testAddGet(h hophash.HashMap, index uint64, b *testing.B) {
	str := strconv.FormatUint(index, 10)
	key := "key" + str
	err := h.Add(key, str)

	if err != nil {
		b.Log("Warning could not add key: " + key)

		value, ok := h.Get(key)
		if !ok {
			fmt.Println(index)
			b.Fatal("value not found")
			return
		}

		if value != str {
			b.Fatal("value not same as what was added")
			return
		}
	}

}
Пример #3
0
func testAddFirstGetLater(b *testing.B, power int) {
	var h hophash.HashMap
	var coder hophash.HashCoder
	coder = new(fnvHash)
	h = hophash.NewHophash(coder, 32, power)

	n := uint64(math.Pow(2, float64(power)))
	firstPart := uint64(n / 10)

	// do 10% inserts first

	for i := uint64(0); i < firstPart; i++ {
		str := strconv.FormatUint(i, 10)
		h.Add(str, str)
	}

	// now do contains with some misses and so hits
	// if "i" is even we should try successfull hit
	// if not we will try a miss
	for i := (n - firstPart); i < n; i++ {
		if i%2 == 0 {
			str := strconv.FormatUint((i / 100), 10)
			value, ok := h.Get(str)
			if !ok {
				b.Fatal("value not found")
				return
			}
			if value != str {
				b.Fatal("value not same as what was added")
				return
			}
		} else {
			str := strconv.FormatUint(i, 10)
			_, ok := h.Get(str)
			if ok {
				b.Fatal("This key should not have been found")
			}
		}
	}
}