loncha

package module
v0.4.5 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2021 License: MIT Imports: 5 Imported by: 3

README

loncha

A high-performance slice Utilities for Go.

  • high-perfirmance slice filter/finder.
  • linked-list generics template using Gennry

Installation

slice Utility
go get github.com/kazu/loncha

QuickStart

slice Utility

slice utility dosent use reflect/interface operation.

    import "github.com/kazu/loncha"

    type GameObject struct {
        ID int
        Name string
        Pos []float
    }

    ...

    var objs []GameObject

    loncha.Find(&objs, func(i int) bool {
        return objs[i].ID == 6
    } 

    loncha.Filter(&objs, func(i int) bool {
        return objs[i].ID == 12
    } 

	loncha.Delete(&objs, func(i int) bool {
		return objs[i].ID == 555
	})

    // find one object with conditions.
    obj, err := Select(&objs, func(i int) bool {
		return slice[i].ID < 50
	})

    err = loncha.Shuffle(objs, 2)

    loncha.Reverse(objs)


generate double-linked list of linux kernel list_head type

define base struct

package game_object

import (
    "github.com/kazu/loncha/list_head"


type Player struct {
    ID int
    Name string
    Hp int
    list_head.ListHead
}

generate linked-list

    $ go get go get github.com/cheekybits/genny
    $ wget -q -O - "https://github.com/kazu/loncha/master/container_list/list.go" | genny  gen "ListEntry=Player" > player_list.go
    $ wget -q -O - "https://github.com/kazu/loncha/master/container_list/list_test.go" | genny  gen "ListEntry=Player" > player_list_test.go

benchmark Result

loncha.Uniq vs hand Uniq vs go-funk.Uniq
loncha.Uniq-16         	    			1000	    997543 ns/op	  548480 B/op	   16324 allocs/op
loncha.UniqWithSort-16 	    			1000	   2237924 ns/op	     256 B/op	       7 allocs/op
loncha.UniqWithSort(sort)-16         	1000	    260283 ns/op	     144 B/op	       4 allocs/op
hand_Uniq-16                          	1000	    427765 ns/op	  442642 B/op	       8 allocs/op
hand_Uniq_iface-16                    	1000	    808895 ns/op	  632225 B/op	    6322 allocs/op
go-funk.Uniq-16                       	1000	   1708396 ns/op	  655968 B/op	   10004 allocs/op
loncha.Filter vs go-funk.Filter
loncha.Filter-16         	     100	     89142 ns/op	   82119 B/op	       4 allocs/op
loncha.Filter_pointer-16 	     100	       201 ns/op	       0 B/op	       0 allocs/op
hand_Filter_pointer-16   	     100	     24432 ns/op	   81921 B/op	       1 allocs/op
go-funk.Filter-16        	     100	   2370492 ns/op	  640135 B/op	   20004 allocs/op
go-funk.Filter_pointer-16        100	      1048 ns/op	      64 B/op	       2 allocs/op

References

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ERR_SLICE_TYPE           error = errors.New("parameter must be slice or pointer of slice")
	ERR_POINTER_SLICE_TYPE   error = errors.New("parameter must be pointer of slice")
	ERR_NOT_FOUND            error = errors.New("data is not found")
	ERR_ELEMENT_INVALID_TYPE error = errors.New("slice element is invalid type")
	ERR_INVALID_INDEX        error = errors.New("invalid index")
)

Functions

func Contain

func Contain(slice interface{}, fn CondFunc) bool

Contain get return true which fn condition is true.

func Delete

func Delete(slice interface{}, funcs ...CondFunc) error

Delete ... Delete element with mached funcs

func Filter

func Filter(slice interface{}, funcs ...CondFunc) error

Filter ... Filter element with mached funcs

func Find

func Find(slice interface{}, fn CondFunc) (interface{}, error)

Find is value of slice if fn is true. if slice is not pointer of slice or empty, return error

func IndexOf

func IndexOf(slice interface{}, fn CondFunc) (int, error)

IndexOf gets the index at which the first match fn is true. if not found. return -1. return error if slice is not pointer of the slice.

func LastIndexOf added in v0.1.1

func LastIndexOf(slice interface{}, fn CondFunc) (int, error)

IndexOf gets the index at which the last match fn is true. if not found. return -1. return error if slice is not pointer of the slice.

func Reverse added in v0.1.2

func Reverse(slice interface{})

Reverse ... Transforms an array such that the first element will become the last, the second element will become the second to last, etc.

func Select

func Select(slice interface{}, fn CondFunc) (interface{}, error)

Select ... return all element on match of CondFunc

func Shuffle

func Shuffle(slice interface{}, seeds ...int64) (e error)

Shuffle is shuffing slice order. if slice is not pointer of slice or not slice, return error

func Uniq

func Uniq(slice interface{}, fn IdentFn) error

Uniq is deduplicate using fn . if slice is not pointer of slice or empty, return error

func Uniq2

func Uniq2(slice interface{}, fn CompareFunc) error

Uniq2 is deduplicate using fn . if slice is not pointer of slice or empty, return error

func UniqWithSort

func UniqWithSort(slice interface{}, fn CompareFunc) (int, error)

UniqWithSort is deduplicating using fn, sorting before dedup. if slice is not pointer of slice or empty, return error

Types

type CompareFunc

type CompareFunc func(i, j int) bool

type CondFunc

type CondFunc func(idx int) bool

type IdentFn

type IdentFn func(i int) interface{}

Directories

Path Synopsis
ContainerListWriter is a base of http://golang.org/pkg/container/list/ this is tuning performancem, reduce heap usage.
ContainerListWriter is a base of http://golang.org/pkg/container/list/ this is tuning performancem, reduce heap usage.
Package loncha/list_head is like a kernel's LIST_HEAD list_head is used by loncha/gen/containers_list
Package loncha/list_head is like a kernel's LIST_HEAD list_head is used by loncha/gen/containers_list
Package list_head ...
Package list_head ...

Jump to

Keyboard shortcuts

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