Exemplo n.º 1
0
func TestStringKeyOMapInsertion(t *testing.T) {
	wordForWord := omap.NewCaseFoldedKeyed()
	for _, word := range []string{"one", "Two", "THREE", "four", "Five"} {
		wordForWord.Insert(word, word)
	}
	var words []string
	wordForWord.Do(func(_, value interface{}) {
		words = append(words, value.(string))
	})
	actual, expected := strings.Join(words, ""), "FivefouroneTHREETwo"
	if actual != expected {
		t.Errorf("%q != %q", actual, expected)
	}
}
Exemplo n.º 2
0
func main() {
	words := []string{"Puttering", "About", "in", "a", "Small", "Land"}
	wordForWord := omap.NewCaseFoldedKeyed()
	fmt.Println(wordForWord.Len(), "words")
	for _, word := range words {
		wordForWord.Insert(word, strings.ToUpper(word))
	}
	wordForWord.Do(func(key, value interface{}) {
		fmt.Printf("%v→%v\n", key, value)
	})
	fmt.Println("length before deleting:", wordForWord.Len())
	_, containsSmall := wordForWord.Find("small")
	fmt.Println("contains small:", containsSmall)
	for _, key := range []string{"big", "medium", "small"} {
		fmt.Printf("%t ", wordForWord.Delete(key))
	}
	_, containsSmall = wordForWord.Find("small")
	fmt.Println("\nlength after deleting: ", wordForWord.Len())
	fmt.Println("contains small:", containsSmall)

	showMap(wordForWord, words, "words", 20)
	searchMap(wordForWord, "small", "big")

	fmt.Println()

	distanceForPoint := omap.New(func(a, b interface{}) bool {
		α, β := a.(*Point), b.(*Point)
		if α.X != β.X {
			return α.X < β.X
		}
		return α.Y < β.Y
	})
	fmt.Println(distanceForPoint.Len(), "points")
	points := []*Point{{3, 1}, {1, 2}, {2, 3}, {1, 3}, {3, 2}, {2, 1}, {2, 2}}
	for _, point := range points {
		distance := math.Hypot(float64(point.X), float64(point.Y))
		distanceForPoint.Insert(point, distance)
	}
	distanceForPoint.Do(func(key, value interface{}) {
		fmt.Printf("%v → %.2v\n", key, value)
	})
	// No &distanceForPoint because it is already a pointer
	showMap(distanceForPoint, points, "points", 5)
	searchMap(distanceForPoint, &Point{1, 1}, &Point{3, 2})
}