Ai
2 Star 0 Fork 0

mirrors_rantav/arrayOperations

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
arrayOperations.go 13.48 KB
一键复制 编辑 原始数据 按行查看 历史
Adam Hanna 提交于 2015-04-03 03:04 +08:00 . more bug fixes
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
package arrayOperations
import (
// "fmt"
// "math"
// "math/rand"
// // "strconv"
// "sort"
// "time"
)
/* ***************************************************************
*
* THIS SECTION IS FOR STRINGS
*
/* *************************************************************** */
// find the intersection of two arrays.
// e.g. a1 = [1 2 2 4 6]; a2 = [2 4 5]
// Intersect(a1, a2) >> [2 4]
func IntersectString(args ...[]string) []string {
// create a map to count all the instances of the strings
arrLength := len(args)
tempMap := make(map[string]int)
for _, arg := range args {
tempArr := DistinctString(arg)
for idx := range tempArr {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr[idx]]; ok {
tempMap[tempArr[idx]]++
} else {
tempMap[tempArr[idx]] = 1
}
}
}
// find the keys equal to the length of the input args
tempArray := make([]string, 0)
for key, val := range tempMap {
if val == arrLength {
tempArray = append(tempArray, key)
}
}
return tempArray
}
// find the intersection of two arrays using a multidimensional array as inputs
func IntersectStringArr(arr [][]string) []string {
// create a map to count all the instances of the strings
arrLength := len(arr)
tempMap := make(map[string]int)
for idx1 := range arr {
tempArr := DistinctString(arr[idx1])
for idx2 := range tempArr {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr[idx2]]; ok {
tempMap[tempArr[idx2]]++
} else {
tempMap[tempArr[idx2]] = 1
}
}
}
// find the keys equal to the length of the input args
tempArray := make([]string, 0)
for key, val := range tempMap {
if val == arrLength {
tempArray = append(tempArray, key)
}
}
return tempArray
}
// find the union of two arrays.
// e.g. a1 = [1 2 2 4 6]; a2 = [2 4 5]
// Union(a1, a2) >> [1 2 4 5 6]
func UnionString(args ...[]string) []string {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[string]uint8)
// write the contents of the arrays as keys to the map. The map values don't matter
for _, arg := range args {
for idx := range arg {
tempMap[arg[idx]] = 0
}
}
// the map keys are now unique instances of all of the array contents
tempArray := make([]string, 0)
for key := range tempMap {
tempArray = append(tempArray, key)
}
return tempArray
}
// find the union of two arrays using a multidimensional array as inputs
func UnionStringArr(arr [][]string) []string {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[string]uint8)
// write the contents of the arrays as keys to the map. The map values don't matter
for idx1 := range arr {
for idx2 := range arr[idx1] {
tempMap[arr[idx1][idx2]] = 0
}
}
// the map keys are now unique instances of all of the array contents
tempArray := make([]string, 0)
for key := range tempMap {
tempArray = append(tempArray, key)
}
return tempArray
}
// find the difference of two arrays.
// e.g. a1 = [1 2 2 4 6]; a2 = [2 4 5]
// Difference(a1, a2) >> [5 6]
func DifferenceString(args ...[]string) []string {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[string]int)
for _, arg := range args {
tempArr := DistinctString(arg)
for idx := range tempArr {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr[idx]]; ok {
tempMap[tempArr[idx]]++
} else {
tempMap[tempArr[idx]] = 1
}
}
}
// write the final val of the diffMap to an array and return
tempArray := make([]string, 0)
for key, val := range tempMap {
if val == 1 {
tempArray = append(tempArray, key)
}
}
return tempArray
}
// find the difference of two arrays using a multidimensional array as inputs
func DifferenceStringArr(arr [][]string) []string {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[string]int)
for idx1 := range arr {
tempArr := DistinctString(arr[idx1])
for idx2 := range tempArr {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr[idx2]]; ok {
tempMap[tempArr[idx2]]++
} else {
tempMap[tempArr[idx2]] = 1
}
}
}
// write the final val of the diffMap to an array and return
tempArray := make([]string, 0)
for key, val := range tempMap {
if val == 1 {
tempArray = append(tempArray, key)
}
}
return tempArray
}
// Remove duplicate values from one array.
// e.g. a1 = [1 2 2 4 6]
// Distinct(a1) >> [1 2 4 6]
func DistinctString(arg []string) []string {
tempMap := make(map[string]uint8)
for idx := range arg {
tempMap[arg[idx]] = 0
}
tempArray := make([]string, 0)
for key := range tempMap {
tempArray = append(tempArray, key)
}
return tempArray
}
/* ***************************************************************
*
* THIS SECTION IS FOR uint64's
*
/* *************************************************************** */
// find the intersection of two arrays.
// e.g. a1 = [1 2 2 4 6]; a2 = [2 4 5]
// Intersect(a1, a2) >> [2 4]
func IntersectUint64(args ...[]uint64) []uint64 {
// create a map to count all the instances of the strings
arrLength := len(args)
tempMap := make(map[uint64]int)
for _, arg := range args {
tempArr := DistinctUint64(arg)
for idx := range tempArr {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr[idx]]; ok {
tempMap[tempArr[idx]]++
} else {
tempMap[tempArr[idx]] = 1
}
}
}
// find the keys equal to the length of the input args
tempArray := make([]uint64, 0)
for key, val := range tempMap {
if val == arrLength {
tempArray = append(tempArray, key)
}
}
return tempArray
}
// find the intersection of two arrays of distinct vals.
func DistinctIntersectUint64(args ...[]uint64) []uint64 {
// create a map to count all the instances of the strings
arrLength := len(args)
tempMap := make(map[uint64]int)
for _, arg := range args {
for idx := range arg {
// how many times have we encountered this elem?
if _, ok := tempMap[arg[idx]]; ok {
tempMap[arg[idx]]++
} else {
tempMap[arg[idx]] = 1
}
}
}
// find the keys equal to the length of the input args
tempArray := make([]uint64, 0)
for key, val := range tempMap {
if val == arrLength {
tempArray = append(tempArray, key)
}
}
return tempArray
}
func sortedIntersectUintHelper(a1 []uint64, a2 []uint64) []uint64 {
intersection := make([]uint64, 0)
n1 := len(a1)
n2 := len(a2)
i := 0
j := 0
for i < n1 && j < n2 {
switch {
case a1[i] > a2[j]:
j++
case a2[j] > a1[i]:
i++
default:
intersection = append(intersection, a1[i])
i++
j++
}
}
return intersection
}
// find the intersection of two sorted arrays
// assumes no dupes!
// NOTE(@adam-hanna): further improve performance by sorting from smallest to largest
// array length?
func SortedIntersectUint64(args ...[]uint64) []uint64 {
// create an array to hold the intersection and write the first array to it
tempIntersection := args[0]
argsLen := len(args)
for k := 1; k < argsLen; k++ {
// do we have any intersections?
switch len(tempIntersection) {
case 0:
// nope! Give them an empty array!
return tempIntersection
default:
// yup, keep chugging
tempIntersection = sortedIntersectUintHelper(tempIntersection, args[k])
}
}
return tempIntersection
}
// find the intersection of two arrays using a multidimensional array as inputs
func IntersectUint64Arr(arr [][]uint64) []uint64 {
// create a map to count all the instances of the strings
arrLength := len(arr)
tempMap := make(map[uint64]int)
for idx1 := range arr {
tempArr := DistinctUint64(arr[idx1])
for idx2 := range tempArr {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr[idx2]]; ok {
tempMap[tempArr[idx2]]++
} else {
tempMap[tempArr[idx2]] = 1
}
}
}
// find the keys equal to the length of the input args
tempArray := make([]uint64, 0)
for key, val := range tempMap {
if val == arrLength {
tempArray = append(tempArray, key)
}
}
return tempArray
}
// find the intersection of two arrays using a multidimensional array as inputs
// assumes no dupes and only works on arrays which are sorted
func SortedIntersectUint64Arr(arr [][]uint64) []uint64 {
// create an array to hold the intersection and write the first array to it
tempIntersection := arr[0]
argsLen := len(arr)
for k := 1; k < argsLen; k++ {
// do we have any intersections?
switch len(tempIntersection) {
case 0:
// nope! Give them an empty array!
return tempIntersection
default:
// yup, keep chugging
tempIntersection = sortedIntersectUintHelper(tempIntersection, arr[k])
}
}
return tempIntersection
}
// find the intersection of two distinct arrays using a multidimensional array as inputs
func DistinctIntersectUint64Arr(arr [][]uint64) []uint64 {
// create a map to count all the instances of the strings
arrLength := len(arr)
tempMap := make(map[uint64]int)
for idx1 := range arr {
for idx2 := range arr[idx1] {
// how many times have we encountered this elem?
if _, ok := tempMap[arr[idx1][idx2]]; ok {
tempMap[arr[idx1][idx2]]++
} else {
tempMap[arr[idx1][idx2]] = 1
}
}
}
// find the keys equal to the length of the input args
tempArray := make([]uint64, 0)
for key, val := range tempMap {
if val == arrLength {
tempArray = append(tempArray, key)
}
}
return tempArray
}
// find the union of two arrays.
// e.g. a1 = [1 2 2 4 6]; a2 = [2 4 5]
// Union(a1, a2) >> [1 2 4 5 6]
func UnionUint64(args ...[]uint64) []uint64 {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[uint64]uint8)
// write the contents of the arrays as keys to the map. The map values don't matter
for _, arg := range args {
for idx := range arg {
tempMap[arg[idx]] = 0
}
}
// the map keys are now unique instances of all of the array contents
tempArray := make([]uint64, 0)
for key := range tempMap {
tempArray = append(tempArray, key)
}
return tempArray
}
// find the union of two arrays using a multidimensional array as inputs
func UnionUint64Arr(arr [][]uint64) []uint64 {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[uint64]uint8)
// write the contents of the arrays as keys to the map. The map values don't matter
for idx1 := range arr {
for idx2 := range arr[idx1] {
tempMap[arr[idx1][idx2]] = 0
}
}
// the map keys are now unique instances of all of the array contents
tempArray := make([]uint64, 0)
for key := range tempMap {
tempArray = append(tempArray, key)
}
return tempArray
}
// find the difference of two arrays.
// e.g. a1 = [1 2 2 4 6]; a2 = [2 4 5]
// Difference(a1, a2) >> [5 6]
func DifferenceUint64(args ...[]uint64) []uint64 {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[uint64]int)
for _, arg := range args {
tempArr := DistinctUint64(arg)
for idx := range tempArr {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr[idx]]; ok {
tempMap[tempArr[idx]]++
} else {
tempMap[tempArr[idx]] = 1
}
}
}
// write the final val of the diffMap to an array and return
tempArray := make([]uint64, 0)
for key, val := range tempMap {
if val == 1 {
tempArray = append(tempArray, key)
}
}
return tempArray
}
// find the difference of two arrays using a multidimensional array as inputs
func DifferenceUint64Arr(arr [][]uint64) []uint64 {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[uint64]int)
for idx1 := range arr {
tempArr := DistinctUint64(arr[idx1])
for idx2 := range tempArr {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr[idx2]]; ok {
tempMap[tempArr[idx2]]++
} else {
tempMap[tempArr[idx2]] = 1
}
}
}
// write the final val of the diffMap to an array and return
tempArray := make([]uint64, 0)
for key, val := range tempMap {
if val == 1 {
tempArray = append(tempArray, key)
}
}
return tempArray
}
// Remove duplicate values from one array.
// e.g. a1 = [1 2 2 4 6]
// Distinct(a1) >> [1 2 4 6]
func DistinctUint64(arg []uint64) []uint64 {
tempMap := make(map[uint64]uint8)
for idx := range arg {
tempMap[arg[idx]] = 0
}
tempArray := make([]uint64, 0)
for key := range tempMap {
tempArray = append(tempArray, key)
}
return tempArray
}
/* ***************************************************************
*
* THIS SECTION IS FOR a little helper benchmark script which can be
* used to test the relative changes in performance when making
* changes to these algorithms.
*
* To use, un-comment out the packages in import.
*
/* *************************************************************** */
/*
func main() {
aBig := make([][]uint64, 0)
a1 := make([]uint64, 0)
a2 := make([]uint64, 0)
aBig = append(aBig, a1)
aBig = append(aBig, a2)
i := 1
j := 1
for i = 1; i <= 7; i++ {
// create the arrays
arrLength := len(a1)
for j = 0; j < ((int(math.Pow(10, float64(i))) / 2) - arrLength); j++ {
a1 = append(a1, uint64(rand.Int()))
a2 = append(a2, uint64(rand.Int()))
}
// get distinct vals
a1 = DistinctUint64(a1)
a2 = DistinctUint64(a2)
// sort the arrs
sort.Sort(uintArray(a1))
sort.Sort(uintArray(a2))
// now run the test
t0 := time.Now()
SortedIntersectUint64Arr(aBig)
t1 := time.Now()
fmt.Printf("n of %v took %v to run.\n", len(a1)+len(a2), t1.Sub(t0))
}
}
// Need to create some helpers for our sort.Sort
type uintArray []uint64
func (s uintArray) Len() int { return len(s) }
func (s uintArray) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s uintArray) Less(i, j int) bool { return s[i] < s[j] }
*/
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_rantav/arrayOperations.git
git@gitee.com:mirrors_rantav/arrayOperations.git
mirrors_rantav
arrayOperations
arrayOperations
1ad18bd2cc6b

搜索帮助