Initial QSfera import
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
|
||||
.vscode
|
||||
vendor
|
||||
+1
@@ -0,0 +1 @@
|
||||
language: go
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2020 Julian Gruber julian@juliangruber.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
'Software'), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
|
||||
# go-intersect
|
||||
|
||||
Find the intersection of two iterable values.
|
||||
|
||||
This library provides multiple implementations which each have their strong and weak points.
|
||||
|
||||
Read the [docs](http://godoc.org/github.com/juliangruber/go-intersect).
|
||||
|
||||
[](https://travis-ci.com/juliangruber/go-intersect)
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
$ go get github.com/juliangruber/go-intersect
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
```go
|
||||
import "github.com/juliangruber/go-intersect"
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
a := []int{1, 2, 3}
|
||||
b := []int{2, 3, 4}
|
||||
fmt.Println(intersect.Simple(a, b))
|
||||
}
|
||||
```
|
||||
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package intersect
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Simple has complexity: O(n^2)
|
||||
func Simple(a interface{}, b interface{}) []interface{} {
|
||||
set := make([]interface{}, 0)
|
||||
av := reflect.ValueOf(a)
|
||||
|
||||
for i := 0; i < av.Len(); i++ {
|
||||
el := av.Index(i).Interface()
|
||||
if contains(b, el) {
|
||||
set = append(set, el)
|
||||
}
|
||||
}
|
||||
|
||||
return set
|
||||
}
|
||||
|
||||
// Sorted has complexity: O(n * log(n)), a needs to be sorted
|
||||
func Sorted(a interface{}, b interface{}) []interface{} {
|
||||
set := make([]interface{}, 0)
|
||||
av := reflect.ValueOf(a)
|
||||
bv := reflect.ValueOf(b)
|
||||
|
||||
for i := 0; i < av.Len(); i++ {
|
||||
el := av.Index(i).Interface()
|
||||
idx := sort.Search(bv.Len(), func(i int) bool {
|
||||
return bv.Index(i).Interface() == el
|
||||
})
|
||||
if idx < bv.Len() && bv.Index(idx).Interface() == el {
|
||||
set = append(set, el)
|
||||
}
|
||||
}
|
||||
|
||||
return set
|
||||
}
|
||||
|
||||
// Hash has complexity: O(n * x) where x is a factor of hash function efficiency (between 1 and 2)
|
||||
func Hash(a interface{}, b interface{}) []interface{} {
|
||||
set := make([]interface{}, 0)
|
||||
hash := make(map[interface{}]bool)
|
||||
av := reflect.ValueOf(a)
|
||||
bv := reflect.ValueOf(b)
|
||||
|
||||
for i := 0; i < av.Len(); i++ {
|
||||
el := av.Index(i).Interface()
|
||||
hash[el] = true
|
||||
}
|
||||
|
||||
for i := 0; i < bv.Len(); i++ {
|
||||
el := bv.Index(i).Interface()
|
||||
if _, found := hash[el]; found {
|
||||
set = append(set, el)
|
||||
}
|
||||
}
|
||||
|
||||
return set
|
||||
}
|
||||
|
||||
func contains(a interface{}, e interface{}) bool {
|
||||
v := reflect.ValueOf(a)
|
||||
|
||||
for i := 0; i < v.Len(); i++ {
|
||||
if v.Index(i).Interface() == e {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user