sslice

package module
Version: v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2019 License: MIT Imports: 1 Imported by: 0

README

Sorted Slice

godoc Build Status goreportcard License

Package sslice provides functionalities for creating a slice which is always sorted. SortedSlice uses binary search to find the index at which the element must be inserted.

Getting Started

import (
    "github.com/yaa110/sslice"
    "fmt"
)

func main() {
    // Create a new instance of SortedSlice
    ss := sslice.New(false)

    // Push elemets
    ss.Push(
        sslice.String("abcd"),
        sslice.String("a"),
        sslice.String("ab"),
    )

    // Push another element
    ss.Push(sslice.String("abc"))

    // Iterate over elements
    //      Output:
    //          0: a
    //          1: ab
    //          2: abc
    //          3: abcd
    ss.Iter(func(index int, elem sslice.SortableElement) bool {
        fmt.Printf("%d: %v\n", index, elem)
        return true // or return false to break the loop
    })
}

For more information please refer to Documentations or check the Example file.

Sortable Elements

All types which implement SortableElement interface could be pushed to SortedSlice. The package sslice provides String, Int, Uint, Int32, Uint32, Int64 and Uint64 aliases to be used.

SortableElement also could be implemented for custom structs:

import "github.com/yaa110/sslice"

type Custom struct {
    field int // Comparable field
    // ...
}

// Compare implements `Compare` method of `SortableElement`
func (receiver *Custom) Compare(other sslice.SortableElement) int {
    if receiver.field < other.(*Custom).field {
        return -1 // receiver is Less than other
    }
    if receiver.field > other.(*Custom).field {
        return 1 // receiver is greater than other
    }
    return 0 // receiver equals other
}

Then Custom struct could be pushed to a SortedSlice:

ss := sslice.New(false)
c1 := &Custom{ field: 1 }
c2 := &Custom{ field: 2 }
ss.push(c1, c2)

Documentation

Overview

Package sslice provides functionalities for creating a slice which is always sorted.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Int

type Int int

Int is an alias to int which implements `SortableElement`

func (Int) Compare added in v0.3.0

func (i Int) Compare(other SortableElement) int

Compare implements `Compare` method of `SortableElement`

type Int32

type Int32 int32

Int32 is an alias to int32 which implements `SortableElement`

func (Int32) Compare added in v0.3.0

func (i Int32) Compare(other SortableElement) int

Compare implements `Compare` method of `SortableElement`

type Int64

type Int64 int64

Int64 is an alias to int64 which implements `SortableElement`

func (Int64) Compare added in v0.3.0

func (i Int64) Compare(other SortableElement) int

Compare implements `Compare` method of `SortableElement`

type SortableElement

type SortableElement interface {
	// Compare compares two SortableElement (`receiver` and `other`)
	//
	// Returns:
	// * `1` if `receiver` is greater than `other`
	// * `-1` if `other` is greater than `receiver`
	// * `0` if `receiver` equals `other`
	Compare(other SortableElement) int
}

SortableElement represents an interface to be implemented for elements of `SortedSlice`

type SortedSlice

type SortedSlice struct {
	// contains filtered or unexported fields
}

SortedSlice represents a slice which is always sorted.

func New

func New(reversed bool) SortedSlice

New creates a new instance of `SortedSlice`.

func (*SortedSlice) Cap

func (ss *SortedSlice) Cap() int

Cap returns the capacity of SortedSlice

func (*SortedSlice) Contains added in v0.2.0

func (ss *SortedSlice) Contains(elem SortableElement) bool

Contains returns true if `ss` containbs `elem`

func (*SortedSlice) Get

func (ss *SortedSlice) Get(i int) SortableElement

Get returns the element at index `i`

func (*SortedSlice) IndexOf added in v0.2.0

func (ss *SortedSlice) IndexOf(elem SortableElement) int

IndexOf returns the first occurrence index of `elem` in `ss` or -1 if `ss` does not contain `elem`

func (*SortedSlice) Iter

func (ss *SortedSlice) Iter(fn func(int, SortableElement) bool)

Iter returns the inner slice of SortedSlice. A false return value of `fn` will break the loop.

func (*SortedSlice) LastIndexOf added in v0.2.0

func (ss *SortedSlice) LastIndexOf(elem SortableElement) int

LastIndexOf returns the last occurrence index of `elem` in `ss` or -1 if `ss` does not contain `elem`

func (*SortedSlice) Len

func (ss *SortedSlice) Len() int

Len returns the length of SortedSlice

func (*SortedSlice) Merge

func (ss *SortedSlice) Merge(other SortedSlice)

Merge merges `other` to `ss`

func (*SortedSlice) Pop

func (ss *SortedSlice) Pop() (elem SortableElement)

Pop removes the last elemt and returns it

func (*SortedSlice) Push

func (ss *SortedSlice) Push(elems ...SortableElement)

Push appends `elems` to sorted slice of `ss`

func (*SortedSlice) Remove

func (ss *SortedSlice) Remove(i uint64)

Remove removes an element at the index `i`

func (*SortedSlice) Reverse

func (ss *SortedSlice) Reverse()

Reverse toggles the order of slice

func (*SortedSlice) Shift

func (ss *SortedSlice) Shift() (elem SortableElement)

Shift removes the first elemt and returns it

type String

type String string

String is an alias to string which implements `SortableElement`

func (String) Compare added in v0.3.0

func (s String) Compare(other SortableElement) int

Compare implements `Compare` method of `SortableElement`

type Uint

type Uint uint

Uint is an alias to uint which implements `SortableElement`

func (Uint) Compare added in v0.3.0

func (u Uint) Compare(other SortableElement) int

Compare implements `Compare` method of `SortableElement`

type Uint32

type Uint32 uint32

Uint32 is an alias to uint32 which implements `SortableElement`

func (Uint32) Compare added in v0.3.0

func (u Uint32) Compare(other SortableElement) int

Compare implements `Compare` method of `SortableElement`

type Uint64

type Uint64 uint64

Uint64 is an alias to uint64 which implements `SortableElement`

func (Uint64) Compare added in v0.3.0

func (u Uint64) Compare(other SortableElement) int

Compare implements `Compare` method of `SortableElement`

Source Files

Jump to

Keyboard shortcuts

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