algorithms

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2019 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package algorithms holds generic algorithms that are useful throughout the project.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Search(i int, a, b Interface) bool

Search will look for the item at a[i] in b. If found, it will return true. Otherwise it will return false.

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

func Dedupe(s Interface) Interface

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/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
}
Output:

func Delete

func Delete(a, b Interface) Interface

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/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
}
Output:

func Merge

func Merge(a, b Interface) Interface

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/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.

func (Ints) Append

func (s Ints) Append(a Interface) Interface

Append implements Interface.

func (Ints) Clone

func (s Ints) Clone() Interface

Clone implements Interface.

func (Ints) Len

func (s Ints) Len() int

Len implements Interface.

func (Ints) Less

func (s Ints) Less(i, j int) bool

Less implements Interface.

func (Ints) Set

func (s Ints) Set(i int, a Interface, j int, b Interface)

Set implements Interface.

func (Ints) Slice

func (s Ints) Slice(i, j int) Interface

Slice implements Interface.

func (Ints) Swap

func (s Ints) Swap(i, j int)

Swap implements Interface.

type Strings

type Strings []string

Strings implements Interface for a slice of strings.

func (Strings) Append

func (s Strings) Append(a Interface) Interface

Append implements Interface.

func (Strings) Clone

func (s Strings) Clone() Interface

Clone implements Interface.

func (Strings) Len

func (s Strings) Len() int

Len implements Interface.

func (Strings) Less

func (s Strings) Less(i, j int) bool

Less implements Interface.

func (Strings) Set

func (s Strings) Set(i int, a Interface, j int, b Interface)

Set implements Interface.

func (Strings) Slice

func (s Strings) Slice(i, j int) Interface

Slice implements Interface.

func (Strings) Swap

func (s Strings) Swap(i, j int)

Swap implements Interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL