1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 // Slice provides utilities for modifying slices of elements.
14 // It can be wrapped around any slice of which the element type implements
21 // Value returns the reflect.Value of the underlying slice.
22 func (s *Slice) Value() reflect.Value {
26 // MakeSlice wraps a pointer to a slice of Elems.
27 // It replaces the array pointed to by the slice so that subsequent modifications
28 // do not alter the data in a CLDR type.
29 // It panics if an incorrect type is passed.
30 func MakeSlice(slicePtr interface{}) Slice {
31 ptr := reflect.ValueOf(slicePtr)
32 if ptr.Kind() != reflect.Ptr {
33 panic(fmt.Sprintf("MakeSlice: argument must be pointer to slice, found %v", ptr.Type()))
36 if sl.Kind() != reflect.Slice {
37 panic(fmt.Sprintf("MakeSlice: argument must point to a slice, found %v", sl.Type()))
39 intf := reflect.TypeOf((*Elem)(nil)).Elem()
40 if !sl.Type().Elem().Implements(intf) {
41 panic(fmt.Sprintf("MakeSlice: element type of slice (%v) does not implement Elem", sl.Type().Elem()))
43 nsl := reflect.MakeSlice(sl.Type(), sl.Len(), sl.Len())
48 typ: sl.Type().Elem().Elem(),
52 func (s Slice) indexForAttr(a string) []int {
53 for i := iter(reflect.Zero(s.typ)); !i.done(); i.next() {
54 if n, _ := xmlName(i.field()); n == a {
58 panic(fmt.Sprintf("MakeSlice: no attribute %q for type %v", a, s.typ))
61 // Filter filters s to only include elements for which fn returns true.
62 func (s Slice) Filter(fn func(e Elem) bool) {
65 for i := 0; i < sl.Len(); i++ {
67 if fn(vi.Interface().(Elem)) {
72 sl.Set(sl.Slice(0, k))
75 // Group finds elements in s for which fn returns the same value and groups
76 // them in a new Slice.
77 func (s Slice) Group(fn func(e Elem) string) []Slice {
78 m := make(map[string][]reflect.Value)
80 for i := 0; i < sl.Len(); i++ {
82 key := fn(vi.Interface().(Elem))
83 m[key] = append(m[key], vi)
87 keys = append(keys, k)
91 for _, k := range keys {
92 nsl := reflect.New(sl.Type())
93 nsl.Elem().Set(reflect.Append(nsl.Elem(), m[k]...))
94 res = append(res, MakeSlice(nsl.Interface()))
99 // SelectAnyOf filters s to contain only elements for which attr matches
100 // any of the values.
101 func (s Slice) SelectAnyOf(attr string, values ...string) {
102 index := s.indexForAttr(attr)
103 s.Filter(func(e Elem) bool {
104 vf := reflect.ValueOf(e).Elem().FieldByIndex(index)
105 return in(values, vf.String())
109 // SelectOnePerGroup filters s to include at most one element e per group of
110 // elements matching Key(attr), where e has an attribute a that matches any
112 // If more than one element in a group matches a value in v preference
113 // is given to the element that matches the first value in v.
114 func (s Slice) SelectOnePerGroup(a string, v []string) {
115 index := s.indexForAttr(a)
116 grouped := s.Group(func(e Elem) string { return Key(e, a) })
118 sl.Set(sl.Slice(0, 0))
119 for _, g := range grouped {
123 for i := 0; i < gsl.Len(); i++ {
124 vi := gsl.Index(i).Elem().FieldByIndex(index)
126 for ; j < len(v) && v[j] != vi.String(); j++ {
134 sl.Set(reflect.Append(sl, e))
139 // SelectDraft drops all elements from the list with a draft level smaller than d
140 // and selects the highest draft level of the remaining.
141 // This method assumes that the input CLDR is canonicalized.
142 func (s Slice) SelectDraft(d Draft) {
143 s.SelectOnePerGroup("draft", drafts[len(drafts)-2-int(d):])