unique – github.com/mpvl/unique Index | Examples | Files

package unique

import "github.com/mpvl/unique"

Package unique provides primitives for finding unique elements of types that implement sort.Interface.

Index

Examples

Functions

func Float64s

func Float64s(a *[]float64)

Float64s removes duplicate elements from a sorted slice of float64s.

Example

Code:play 

package main

import (
	"fmt"

	"github.com/mpvl/unique"
)

func main() {
	a := []float64{1.1, 2.2, 2.2, 3.3, 3.3, 3.3}
	unique.Float64s(&a)
	fmt.Println(a)

}

Output:

[1.1 2.2 3.3]

func Float64sAreUnique

func Float64sAreUnique(a []float64) bool

Float64sAreUnique tests whether a slice of float64s is sorted and its elements are unique.

func Ints

func Ints(a *[]int)

Ints removes duplicate elements from a sorted slice of ints.

Example

Code:play 

package main

import (
	"fmt"

	"github.com/mpvl/unique"
)

func main() {
	a := []int{1, 2, 2, 3, 3, 3}
	unique.Ints(&a)
	fmt.Println(a)

}

Output:

[1 2 3]

func IntsAreUnique

func IntsAreUnique(a []int) bool

IntsAreUnique tests whether a slice of ints is sorted and its elements are unique.

func IsUniqued

func IsUniqued(data sort.Interface) bool

IsUniqued reports whether the elements in data are sorted and unique.

Example

Code:play 

package main

import (
	"fmt"
	"sort"

	"github.com/mpvl/unique"
)

func main() {
	fmt.Println(unique.IsUniqued(sort.IntSlice([]int{1, 2, 3})))
	fmt.Println(unique.IsUniqued(sort.IntSlice([]int{1, 2, 2, 3})))
	// false because it isn't sorted as well.
	fmt.Println(unique.IsUniqued(sort.IntSlice([]int{3, 2, 1})))
	fmt.Println(unique.IsUniqued(sort.IntSlice([]int{})))

}

Output:

true
false
false
true

func Sort

func Sort(data Interface)

Sort sorts and removes duplicate entries from data.

Example

Code:play 

package main

import (
	"fmt"

	"github.com/mpvl/unique"
)

func main() {
	a := []int{3, 3, 2, 3, 2, 1}
	unique.Sort(unique.IntSlice{&a})
	fmt.Println(a)

}

Output:

[1 2 3]

func Strings

func Strings(a *[]string)

Strings removes duplicate elements from a sorted slice of strings.

Example

Code:play 

package main

import (
	"fmt"

	"github.com/mpvl/unique"
)

func main() {
	a := []string{"1", "2", "2", "3", "3", "3"}
	unique.Strings(&a)
	fmt.Println(a)

}

Output:

[1 2 3]

func StringsAreUnique

func StringsAreUnique(a []string) bool

StringsAreUnique tests whether a slice of strings is sorted and its elements are unique.

func ToFront

func ToFront(data sort.Interface) (n int)

ToFront reports the number of unique elements of data which it moves to the first n positions. It assumes sort.IsSorted(data).

Example

Code:play 

package main

import (
	"fmt"
	"sort"

	"github.com/mpvl/unique"
)

func main() {
	a := []int{1, 2, 2, 3, 3, 3}
	fmt.Println(a[:unique.ToFront(sort.IntSlice(a))])

}

Output:

[1 2 3]

func Unique

func Unique(data Interface)

Unique removes duplicate elements from data. It assumes sort.IsSorted(data).

Types

type Float64Slice

type Float64Slice struct{ P *[]float64 }

Float64Slice attaches the methods of Interface to []float64.

func (Float64Slice) Len

func (p Float64Slice) Len() int

func (Float64Slice) Less

func (p Float64Slice) Less(i, j int) bool

func (Float64Slice) Swap

func (p Float64Slice) Swap(i, j int)

func (Float64Slice) Truncate

func (p Float64Slice) Truncate(n int)

type IntSlice

type IntSlice struct{ P *[]int }

IntSlice attaches the methods of Interface to []int.

func (IntSlice) Len

func (p IntSlice) Len() int

func (IntSlice) Less

func (p IntSlice) Less(i, j int) bool

func (IntSlice) Swap

func (p IntSlice) Swap(i, j int)

func (IntSlice) Truncate

func (p IntSlice) Truncate(n int)

type Interface

type Interface interface {
	sort.Interface

	// Truncate reduces the length to the first n elements.
	Truncate(n int)
}

Types that implement unique.Interface can have duplicate elements removed by the functionality in this package.

type StringSlice

type StringSlice struct{ P *[]string }

StringSlice attaches the methods of Interface to []string.

func (StringSlice) Len

func (p StringSlice) Len() int

func (StringSlice) Less

func (p StringSlice) Less(i, j int) bool

func (StringSlice) Swap

func (p StringSlice) Swap(i, j int)

func (StringSlice) Truncate

func (p StringSlice) Truncate(n int)

Source Files

unique.go

Version
v0.0.0-20150818121801-cbe035fff7de (latest)
Published
Aug 18, 2015
Platform
linux/amd64
Imports
1 packages
Last checked
6 hours ago

Tools for package owners.