github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/util/slice/slice.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package slice provides utility methods for common operations on slices. 18 package slice 19 20 import ( 21 "sort" 22 23 utilrand "k8s.io/kubernetes/pkg/util/rand" 24 ) 25 26 // CopyStrings copies the contents of the specified string slice 27 // into a new slice. 28 func CopyStrings(s []string) []string { 29 c := make([]string, len(s)) 30 copy(c, s) 31 return c 32 } 33 34 // SortStrings sorts the specified string slice in place. It returns the same 35 // slice that was provided in order to facilitate method chaining. 36 func SortStrings(s []string) []string { 37 sort.Strings(s) 38 return s 39 } 40 41 // ShuffleStrings copies strings from the specified slice into a copy in random 42 // order. It returns a new slice. 43 func ShuffleStrings(s []string) []string { 44 shuffled := make([]string, len(s)) 45 perm := utilrand.Perm(len(s)) 46 for i, j := range perm { 47 shuffled[j] = s[i] 48 } 49 return shuffled 50 }