Debian Codegraph @

containers/containers.go

raw symbols SCIP
1 // Copyright (c) 2015, Emir Pasic. 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.
4
5 // Package containers provides core interfaces and functions for data structures.
6 //
7 // Container is the base interface for all data structures to implement.
8 //
9 // Iterators provide stateful iterators.
10 //
11 // Enumerable provides Ruby inspired (each, select, map, find, any?, etc.) container functions.
12 //
13 // Serialization provides serializers (marshalers) and deserializers (unmarshalers).
14 package containers
15
16 import (
17 "cmp"
18 "slices"
19
20 "github.com/emirpasic/gods/v2/utils"
21 )
22
23 // Container is base interface that all data structures implement.
24 type Container[T any] interface {
25 Empty() bool
26 Size() int
27 Clear()
28 Values() []T
29 String() string
30 }
31
32 // GetSortedValues returns sorted container's elements with respect to the passed comparator.
33 // Does not affect the ordering of elements within the container.
34 func GetSortedValues[T cmp.Ordered](container Container[T]) []T {
35 values := container.Values()
36 if len(values) < 2 {
37 return values
38 }
39 slices.Sort(values)
40 return values
41 }
42
43 // GetSortedValuesFunc is the equivalent of GetSortedValues for containers of values that are not ordered.
44 func GetSortedValuesFunc[T any](container Container[T], comparator utils.Comparator[T]) []T {
45 values := container.Values()
46 if len(values) < 2 {
47 return values
48 }
49 slices.SortFunc(values, comparator)
50 return values
51 }