예제 #1
0
파일: bench_test.go 프로젝트: Mistobaan/set
func benchApply(b *testing.B, op set.Op, sets [][]int) {
	pivots := pivots(sets)
	n := len(sets) - 1
	data := make(sort.IntSlice, 0, pivots[n])
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		data = data[:0]
		for _, set := range sets {
			data = append(data, set...)
		}
		set.Apply(op, data, pivots)
	}
}
예제 #2
0
파일: apply.go 프로젝트: gasserk/gotalks
func main() {
	// BEGIN OMIT
	a := sort.IntSlice{2, 3, 5, 7, 11, 13}   // primes
	b := sort.IntSlice{0, 1, 2, 3, 5, 8, 13} // Fibonacci numbers
	c := sort.IntSlice{1, 2, 5, 14, 42}      // Catalan numbers
	data := append(append(a, b...), c...)
	pivots := set.Pivots(len(a), len(b), len(c))

	size := set.Apply(set.Inter, data, pivots)
	data = data[:size]
	fmt.Println(data)
	// END OMIT
}
예제 #3
0
파일: apply_test.go 프로젝트: Mistobaan/set
func ExampleApply() {
	sets := []sort.IntSlice{
		{1, 3, 5, 7, 9},  // odds from 1
		{3, 5, 7, 9, 11}, // odds from 3
		{5, 10, 15, 20},  // 5-multiples
		{2, 3, 5, 7, 11}, // primes
	}

	pivots := make([]int, len(sets))
	var orig, data sort.IntSlice

	// concatenate the sets together for use with the set package
	for i, set := range sets {
		pivots[i] = len(set)
		orig = append(orig, set...)
	}

	// transform set sizes into pivots
	pivots = set.Pivots(pivots...)

	tasks := []struct {
		name string
		op   set.Op
	}{
		{"union", set.Union},
		{"inter", set.Inter},
		{"sdiff", set.SymDiff},
	}

	for _, task := range tasks {
		// make a copy of the original data (Apply rearranges the input)
		data = append(data[:0], orig...)
		size := set.Apply(task.op, data, pivots)
		data = data[:size]
		fmt.Printf("%s: %v\n", task.name, data)
	}

	// Output:
	// union: [1 2 3 5 7 9 10 11 15 20]
	// inter: [5]
	// sdiff: [1 2 3 7 10 15 20]
}
예제 #4
0
파일: apply_test.go 프로젝트: Mistobaan/set
func ExampleApply_diff() {
	// a -  b - c - d  cannot be used with Apply (Diff is non-associative)
	// a - (b + c + d) equivalent, using Apply (Union is associative)

	sets := []sort.IntSlice{
		{0, 2, 4, 6, 8, 10},  // positive evens
		{0, 1, 2, 3, 5, 8},   // set of fibonacci numbers
		{5, 10, 15},          // positive 5-multiples
		{2, 3, 5, 7, 11, 13}, // primes
	}

	var data sort.IntSlice

	// for use with (b + c + d)
	exprsets := sets[1:]
	pivots := make([]int, len(exprsets))

	// concatenate the sets together for use with the set package
	for i, set := range exprsets {
		pivots[i] = len(set)
		data = append(data, set...)
	}

	// transform set sizes into pivots
	pivots = set.Pivots(pivots...)

	// calculate x = (b + c + d)
	size := set.Apply(set.Union, data, pivots)

	// concatenate the result to the end of a
	data = append(sets[0], data[:size]...)

	// calculate a - x
	size = set.Diff(data, len(sets[0]))
	data = data[:size]

	fmt.Println("diff:", data)

	// Output:
	// diff: [4 6]
}