inslice

package module
v0.0.0-...-146fa4d Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2021 License: GPL-3.0 Imports: 2 Imported by: 1

README

InSlice

Go Report Card Travis Build Status CodeCov Code Coverage Godoc

Description

A small range of functions for comparing if item is present in slice and receiving the index results.

Functions

Int

Functions are provided for slice of int, and for slice of ptr to int, as well as "All" versions, for finding all occurrances.

We always compare values, never pointers themselves.

func Int(slice []int, item int, count int) (index []int) {}
func IntVar(slice []*int, item int, count int) (index []int) {}

func IntAll(slice []int, item int) (index []int) {}
func IntVarAll(slice []*int, item int) (index []int) {}
String

Functions are provided for slice of string, and for slice of ptr to string, as well as "All" versions, for finding all occurrances.

We always compare values, never pointers themselves.

func String(slice []string, item string, count int) (index []int) {}
func StringVar(slice []*string, item string, count int) (index []int) {}

func StringAll(slice []string, item string) (index []int) {}
func StringVarAll(slice []*string, item string) (index []int) {}
Uint

Functions are provided for slice of uint, and for slice of ptr to uint, as well as "All" versions, for finding all occurrances.

We always compare values, never pointers themselves.

func Uint(slice []uint, item uint, count int) (index []int) {}
func UintVar(slice []*uint, item uint, count int) (index []int) {}

func UintAll(slice []uint, item uint) (index []int) {}
func UintVarAll(slice []*uint, item uint) (index []int) {}
Reflect

Functions are provided for slice of items, as well as "All" version, for finding all occurrances

Reflect automatically checks if the slice/item contain Ptr to actual values, or actual values. If it's the Ptr, the function will compare the values themselves, not check if the Ptr are pointing to the same place. Hence, no equivalent of previous functions with "Var".

func Reflect(slice interface{}, item interface{}, count int) (index []int, err error) {}
func ReflectAll(slice interface{}, item interface{}) (index []int, err error) {}

Examples

Is item in slice
package main

import "github.com/bestmethod/inslice"

func main() {
    slice := []int{1,2,3}
    item := 2
    if len(inslice.Int(slice, item, 1)) != 0 {
        fmt.Println("Item found in slice")
    }
}
Index for first 2 occurrances of item in slice
package main

import "github.com/bestmethod/inslice"

func main() {
    slice := []string{"a","b","c","a","b","c","a","b","c"}
    item := "b"
    fmt.Println(inslice.String(slice, item, 2))
}
Index of all occurances of item in slice, using interfaces and reflect
package main

import "github.com/bestmethod/inslice"

func main() {
	slice := []string{
		"text1",
		"text2",
		"text3",
		"text2",
	}
	var sliceInterface interface{}
    sliceInterface = slice
    
	st := "text2"
	var itemInterface interface{}
    itemInterface = st
    
    indexes, err := ReflectAll(sliceInterface, itemInterface)
    if err != nil {
        log.Fatalf("I did something wrong: %s", err)
    }
    fmt.Println(indexes)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasInt

func HasInt(slice []int, item int) bool

HasInt returns true if item is found in slice, or false if it hasn't

func HasIntVar

func HasIntVar(slice []*int, item int) bool

HasIntVar returns true if item is found in slice, or false if it hasn't

func HasString

func HasString(slice []string, item string) bool

HasString returns true if item is found in slice, or false if it hasn't

func HasStringVar

func HasStringVar(slice []*string, item string) bool

HasStringVar returns true if item is found in slice, or false if it hasn't

func HasUint

func HasUint(slice []uint, item uint) bool

HasUint returns true if item is found in slice, or false if it hasn't

func HasUintVar

func HasUintVar(slice []*uint, item uint) bool

HasUintVar returns true if item is found in slice, or false if it hasn't

func Int

func Int(slice []int, item int, count int) (index []int)

Int compares an item to a slice of items and return indexes of the first X matched entries.

func IntAll

func IntAll(slice []int, item int) (index []int)

IntAll acts like Int, looking for all occurrences.

func IntFunc

func IntFunc(slice []int, item int, compareFunc CompareInts, count int) (index []int)

IntFunc return up to X results if item matches in slice, using external comparison function. For example, pass ints.Contains as func

func IntFuncAll

func IntFuncAll(slice []int, item int, compareFunc CompareInts) (index []int)

IntFuncAll returns all results if item matches in slice, using external comparison function. For example, pass ints.Contains as func

func IntMatch

func IntMatch(slice []int, item int) (index int)

IntMatch acts like Int, returning the index of the first found match returns -1 if item not found

func IntVar

func IntVar(slice []*int, item int, count int) (index []int)

IntVar compares an item to a slice of item pointers and return indexes of the first X matched entries.

func IntVarAll

func IntVarAll(slice []*int, item int) (index []int)

IntVarAll acts like IntVar, looking for all occurrences.

func IntVarFunc

func IntVarFunc(slice []*int, item int, compareFunc CompareInts, count int) (index []int)

IntVarFunc return up to X results if item matches in slice, using external comparison function. For example, pass ints.Contains as func

func IntVarFuncAll

func IntVarFuncAll(slice []*int, item int, compareFunc CompareInts) (index []int)

IntVarFuncAll returns all results if item matches in slice, using external comparison function. For example, pass ints.Contains as func

func IntVarMatch

func IntVarMatch(slice []*int, item int) (index int)

IntVarMatch acts like Int, returning the index of the first found match returns -1 if item not found

func Reflect

func Reflect(slice interface{}, item interface{}, count int) (index []int, err error)

Reflect performs a comparison on interface type (interface must be of type slice - i.e. slice of anything). The item and/or slice may be pointers to values (e.g. []*int and *int) or any mix of those. Only actual values are evaluated. May be slow compared to other functions.

func ReflectAll

func ReflectAll(slice interface{}, item interface{}) (index []int, err error)

ReflectAll works like Reflect, but returns all items.

func String

func String(slice []string, item string, count int) (index []int)

String compares an item to a slice of items and return indexes of the first X matched entries.

func StringAll

func StringAll(slice []string, item string) (index []int)

StringAll acts like String, looking for all occurrences.

func StringFunc

func StringFunc(slice []string, item string, compareFunc CompareStrings, count int) (index []int)

StringFunc return up to X results if item matches in slice, using external comparison function. For example, pass strings.Contains as func

func StringFuncAll

func StringFuncAll(slice []string, item string, compareFunc CompareStrings) (index []int)

StringFuncAll returns all results if item matches in slice, using external comparison function. For example, pass strings.Contains as func

func StringMatch

func StringMatch(slice []string, item string) (index int)

StringMatch acts like String, returning the index of the first found match returns -1 if item not found

func StringVar

func StringVar(slice []*string, item string, count int) (index []int)

StringVar compares an item to a slice of item pointers and return indexes of the first X matched entries.

func StringVarAll

func StringVarAll(slice []*string, item string) (index []int)

StringVarAll acts like StringVar, looking for all occurrences.

func StringVarFunc

func StringVarFunc(slice []*string, item string, compareFunc CompareStrings, count int) (index []int)

StringVarFunc return up to X results if item matches in slice, using external comparison function. For example, pass strings.Contains as func

func StringVarFuncAll

func StringVarFuncAll(slice []*string, item string, compareFunc CompareStrings) (index []int)

StringVarFuncAll returns all results if item matches in slice, using external comparison function. For example, pass strings.Contains as func

func StringVarMatch

func StringVarMatch(slice []*string, item string) (index int)

StringVarMatch acts like String, returning the index of the first found match returns -1 if item not found

func Uint

func Uint(slice []uint, item uint, count int) (index []int)

Uint compares an item to a slice of items and return indexes of the first X matched entries.

func UintAll

func UintAll(slice []uint, item uint) (index []int)

UintAll acts like Uint, looking for all occurrences.

func UintFunc

func UintFunc(slice []uint, item uint, compareFunc CompareUints, count int) (index []int)

UintFunc return up to X results if item matches in slice, using external comparison function. For example, pass uints.Contains as func

func UintFuncAll

func UintFuncAll(slice []uint, item uint, compareFunc CompareUints) (index []int)

UintFuncAll returns all results if item matches in slice, using external comparison function. For example, pass uints.Contains as func

func UintMatch

func UintMatch(slice []uint, item uint) (index int)

UintMatch acts like Uint, returning the index of the first found match returns -1 if item not found

func UintVar

func UintVar(slice []*uint, item uint, count int) (index []int)

UintVar compares an item to a slice of item pouinters and return indexes of the first X matched entries.

func UintVarAll

func UintVarAll(slice []*uint, item uint) (index []int)

UintVarAll acts like UintVar, looking for all occurrences.

func UintVarFunc

func UintVarFunc(slice []*uint, item uint, compareFunc CompareUints, count int) (index []int)

UintVarFunc return up to X results if item matches in slice, using external comparison function. For example, pass uints.Contains as func

func UintVarFuncAll

func UintVarFuncAll(slice []*uint, item uint, compareFunc CompareUints) (index []int)

UintVarFuncAll returns all results if item matches in slice, using external comparison function. For example, pass uints.Contains as func

func UintVarMatch

func UintVarMatch(slice []*uint, item uint) (index int)

UintVarMatch acts like Uint, returning the index of the first found match returns -1 if item not found

Types

type CompareInts

type CompareInts func(sliceInt int, item int) bool

CompareInts function is used in StringFunc and StringFuncAll to provide a third-party comparison

type CompareStrings

type CompareStrings func(sliceString string, item string) bool

CompareStrings function is used in StringFunc and StringFuncAll to provide a third-party comparison

type CompareUints

type CompareUints func(sliceInt uint, item uint) bool

CompareUints function is used in StringFunc and StringFuncAll to provide a third-party comparison

Jump to

Keyboard shortcuts

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