File: slice.go

package info (click to toggle)
icingadb 1.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 59,960 kB
  • sloc: ansic: 170,157; asm: 7,097; sql: 4,098; sh: 1,614; cpp: 1,132; makefile: 438; xml: 160
file content (37 lines) | stat: -rw-r--r-- 888 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package utils

import (
	"fmt"
	"reflect"
)

// AnySliceToInterfaceSlice takes a slice of type []T for any T and returns a slice of type []interface{} containing
// the same elements, somewhat like casting []T to []interface{}.
func AnySliceToInterfaceSlice(in interface{}) []interface{} {
	v := reflect.ValueOf(in)
	if v.Kind() != reflect.Slice {
		panic(fmt.Errorf("AnySliceToInterfaceSlice() called on %T instead of a slice type", in))
	}

	out := make([]interface{}, v.Len())
	for i := 0; i < v.Len(); i++ {
		out[i] = v.Index(i).Interface()
	}
	return out
}

func SliceSubsets(in ...string) [][]string {
	result := make([][]string, 0, 1<<len(in))

	for bitset := 0; bitset < (1 << len(in)); bitset++ {
		var subset []string
		for i := 0; i < len(in); i++ {
			if bitset&(1<<i) != 0 {
				subset = append(subset, in[i])
			}
		}
		result = append(result, subset)
	}

	return result
}