Documentation
¶
Overview ¶
Package algorithms holds generic algorithms that are useful throughout the project.
Index ¶
- func Search(i int, a, b Interface) bool
- type Interface
- type Ints
- type Strings
- type Subjects
- func (s Subjects) Append(a Interface) Interface
- func (s Subjects) Clone() Interface
- func (s Subjects) Contains(name, kind string) (bool, int)
- func (s Subjects) Len() int
- func (s Subjects) Less(i, j int) bool
- func (s Subjects) Set(i int, a Interface, j int, b Interface)
- func (s Subjects) Slice(i, j int) Interface
- func (s Subjects) Swap(i, j int)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Search ¶
Search will look for the item at a[i] in b. If found, it will return true. Otherwise it will return false.
Example ¶
package main
import (
"fmt"
"sort"
"github.com/google/kf/v2/pkg/kf/algorithms"
)
func main() {
haystack := algorithms.Strings{"a", "c", "d", "b", "x"}
sort.Sort(haystack)
needles := algorithms.Strings{"x", "y", "z", "c"}
for needleIdx := range needles {
fmt.Println(algorithms.Search(needleIdx, needles, haystack))
}
}
Output: true false false true
Types ¶
type Interface ¶
type Interface interface {
sort.Interface
// Set stores the value located at b[j] to a[i].
Set(i int, a Interface, j int, b Interface)
// Slice returns a slice (e.g., s[i:j]) of the object.
Slice(i, j int) Interface
// Append returns i and a appended to each other (e.g., append(i, a...)).
Append(a Interface) Interface
// Clone returns a clone of the object.
Clone() Interface
}
Interface is the interface used by the algorithms.
func Dedupe ¶
Dedupe removes any duplicates from the given collection. This does not alter the input. First element is always chosen.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/google/kf/v2/pkg/kf/algorithms"
)
func main() {
s := []string{"a", "b", "a", "d", "b", "d", "c"}
s = []string(algorithms.Dedupe(algorithms.Strings(s)).(algorithms.Strings))
fmt.Println(strings.Join(s, ", "))
// Outputs: a, b, c, d
}
func Delete ¶
Delete removes any items in 'b' from the 'a' collection. This does not alter the input. It outputs the lengh of the new collection. Therefore, if the interface is wrapping a slice, then the slice should be truncated via the result (e.g., slice[:returnValue]).
Example ¶
package main
import (
"fmt"
"sort"
"strings"
"github.com/google/kf/v2/pkg/kf/algorithms"
)
func main() {
a := []string{"c", "b", "a", "d"}
b := []string{"d", "c"}
a = []string(algorithms.Delete(algorithms.Strings(a), algorithms.Strings(b)).(algorithms.Strings))
// Sort for readability
sort.Sort(algorithms.Strings(a))
fmt.Println(strings.Join(a, ", "))
// Outputs: a, b
}
func Merge ¶
Merge will combine the two collections. It replaces values from a with b if there is a collision. It assumes both a and b have been Deduped. It uses Append to create new memory and therefore does not destroy the input.
Example ¶
package main
import (
"fmt"
"sort"
"github.com/google/kf/v2/pkg/kf/algorithms"
)
func main() {
a := algorithms.Strings{"c", "b", "a", "d"}
b := algorithms.Strings{"d", "c", "e"}
r := algorithms.Merge(a, b)
// Sort for display purposes.
sort.Sort(r)
for _, x := range r.(algorithms.Strings) {
fmt.Println(x)
}
}
Output: a b c d e
type Ints ¶
type Ints []int
Ints implements Interface for a slice of ints.
type Strings ¶
type Strings []string
Strings implements Interface for a slice of strings.