slices

package
v0.0.281 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: Apache-2.0 Imports: 0 Imported by: 0

README

slices

Internal utility functions that manipulate slices of strings.

All these methods alter their input slice and do not allocate extra memory.

UniqueStrings is provided as a faster equivalent to github.com/armosec/utils-go/str.SliceStringToUnique.

Trim is the same as the previously private function trimUnique() with similar performances. Code is just more straightforward.

TrimStable has the same intent as Trim but does not alter the order of the input.

TrimUnique and TrimStableUnique combine UniqueStrings and Trim (resp. TrimStable) in a single iteration of the input slice.

Benchmarks

go test -v -run Bench -bench . -benchtime 1s
goos: linux
goarch: amd64
pkg: github.com/kubescape/opa-utils/reporthandling/internal/slices
cpu: AMD Ryzen 7 5800X 8-Core Processor             
BenchmarkUnique
BenchmarkUnique/UniqueStrings_x_8
BenchmarkUnique/UniqueStrings_x_8-16         	 6007258	       192.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkUnique/UniqueStrings_x_16
BenchmarkUnique/UniqueStrings_x_16-16        	 3084657	       386.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkUnique/UniqueStrings_x_32
BenchmarkUnique/UniqueStrings_x_32-16        	 3148867	       387.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkUnique/SliceStringToUnique_x_8
BenchmarkUnique/SliceStringToUnique_x_8-16   	 2188087	       543.4 ns/op	      64 B/op	       1 allocs/op
BenchmarkUnique/SliceStringToUnique_x_16
BenchmarkUnique/SliceStringToUnique_x_16-16  	 2219115	       544.2 ns/op	      64 B/op	       1 allocs/op
BenchmarkUnique/SliceStringToUnique_x_32
BenchmarkUnique/SliceStringToUnique_x_32-16  	 2175615	       548.6 ns/op	      64 B/op	       1 allocs/op
BenchmarkTrim
BenchmarkTrim/Trim_x_8
BenchmarkTrim/Trim_x_8-16                    	 5328590	       222.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrim/Trim_x_16
BenchmarkTrim/Trim_x_16-16                   	 2748080	       422.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrim/Trim_x_32
BenchmarkTrim/Trim_x_32-16                   	 2732326	       425.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrim/TrimStable_x_8
BenchmarkTrim/TrimStable_x_8-16              	 5337442	       216.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrim/TrimStable_x_16
BenchmarkTrim/TrimStable_x_16-16             	 2752138	       435.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrim/TrimStable_x_32
BenchmarkTrim/TrimStable_x_32-16             	 2770256	       432.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrim/TrimSwap_x_8_(original_version)
BenchmarkTrim/TrimSwap_x_8_(original_version)-16         	 5299237	       225.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrim/TrimSwap_x_16_(original_version)
BenchmarkTrim/TrimSwap_x_16_(original_version)-16        	 2744328	       437.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrim/TrimSwap_x_32_(original_version)
BenchmarkTrim/TrimSwap_x_32_(original_version)-16        	 2800281	       440.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrim/TrimUnique_x_8
BenchmarkTrim/TrimUnique_x_8-16                          	 4220919	       284.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrim/TrimUnique_x_16
BenchmarkTrim/TrimUnique_x_16-16                         	 2469658	       482.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrim/TrimUnique_x_32
BenchmarkTrim/TrimUnique_x_32-16                         	 2408644	       494.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrim/TrimStableUnique_x_8
BenchmarkTrim/TrimStableUnique_x_8-16                    	 4250454	       288.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrim/TrimStableUnique_x_16
BenchmarkTrim/TrimStableUnique_x_16-16                   	 2390988	       486.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrim/TrimStableUnique_x_32
BenchmarkTrim/TrimStableUnique_x_32-16                   	 2416376	       495.0 ns/op	       0 B/op	       0 allocs/op
PASS
ok 

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StringInSlice

func StringInSlice(haystack []string, needle string) bool

StringInSlice determines if a needle is in a haystack of strings.

func Trim

func Trim(origin, trimFrom []string) []string

Trim returns the origin slice with all the elements from "trimFrom" removed.

The original order of the original elements may be altered.

NOTE: this function does not allocate extra memory: the input slice is altered in-place.

The returned slice is truncated in capacity to the number of unique elements.

func TrimStable

func TrimStable(origin, trimFrom []string) []string

TrimStable returns the origin slice with all the elements from "trimFrom" removed.

The original order of the original elements is maintained. Memory and CPU efficiency is about the same as Trim().

NOTE: this function does not allocate extra memory: the input slice is altered in-place.

The returned slice is truncated in capacity to the number of unique elements.

func TrimStableUnique

func TrimStableUnique(origin, trimFrom []string) []string

TrimStableUnique combines UniqueStrings and Trim in a single iteration.

It returns the unique elements of the origin slice with all the elements from "trimFrom" removed.

NOTE: this function does not allocate extra memory: the input slice is altered in-place.

The returned slice is truncated in capacity to the number of unique elements.

Calling TrimUnique in combination is slighly more efficient than calling these functions separately (the longer the slices, the larger the savings).

func TrimUnique

func TrimUnique(origin, trimFrom []string) []string

TrimUnique combines UniqueStrings and Trim in a single iteration.

It returns the unique elements of the origin slice with all the elements from "trimFrom" removed.

NOTE: this function does not allocate extra memory: the input slice is altered in-place.

The returned slice is truncated in capacity to the number of unique elements.

Calling TrimUnique in combination is slighly more efficient than calling these functions separately (the longer the slices, the larger the savings).

func UniqueStrings

func UniqueStrings(strSlice []string) []string

UniqueStrings returns the unique (unsorted) values of a slice.

NOTE: this function does not allocate extra memory: the input slice is altered in-place.

The returned slice is truncated in capacity to the number of unique elements.

Types

This section is empty.

Jump to

Keyboard shortcuts

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