...

Source file src/github.com/thoas/go-funk/join_primitives.go

Documentation: github.com/thoas/go-funk

     1  package funk
     2  
     3  type JoinIntFnc func(lx, rx []int) []int
     4  
     5  // JoinInt combines two int collections using the given join method.
     6  func JoinInt(larr, rarr []int, fnc JoinIntFnc) []int {
     7  	return fnc(larr, rarr)
     8  }
     9  
    10  // InnerJoinInt finds and returns matching data from two int collections.
    11  func InnerJoinInt(lx, rx []int) []int {
    12  	result := make([]int, 0, len(lx)+len(rx))
    13  	rhash := hashSliceInt(rx)
    14  	lhash := make(map[int]struct{}, len(lx))
    15  
    16  	for _, v := range lx {
    17  		_, ok := rhash[v]
    18  		_, alreadyExists := lhash[v]
    19  		if ok && !alreadyExists {
    20  			lhash[v] = struct{}{}
    21  			result = append(result, v)
    22  		}
    23  	}
    24  	return result
    25  }
    26  
    27  // OuterJoinInt finds and returns dissimilar data from two int collections.
    28  func OuterJoinInt(lx, rx []int) []int {
    29  	ljoin := LeftJoinInt(lx, rx)
    30  	rjoin := RightJoinInt(lx, rx)
    31  
    32  	result := make([]int, len(ljoin)+len(rjoin))
    33  	for i, v := range ljoin {
    34  		result[i] = v
    35  	}
    36  	for i, v := range rjoin {
    37  		result[len(ljoin)+i] = v
    38  	}
    39  	return result
    40  }
    41  
    42  // LeftJoinInt finds and returns dissimilar data from the first int collection (left).
    43  func LeftJoinInt(lx, rx []int) []int {
    44  	result := make([]int, 0, len(lx))
    45  	rhash := hashSliceInt(rx)
    46  
    47  	for _, v := range lx {
    48  		_, ok := rhash[v]
    49  		if !ok {
    50  			result = append(result, v)
    51  		}
    52  	}
    53  	return result
    54  }
    55  
    56  // LeftJoinInt finds and returns dissimilar data from the second int collection (right).
    57  func RightJoinInt(lx, rx []int) []int { return LeftJoinInt(rx, lx) }
    58  
    59  func hashSliceInt(arr []int) map[int]struct{} {
    60  	hash := make(map[int]struct{}, len(arr))
    61  	for _, i := range arr {
    62  		hash[i] = struct{}{}
    63  	}
    64  	return hash
    65  }
    66  
    67  type JoinInt32Fnc func(lx, rx []int32) []int32
    68  
    69  // JoinInt32 combines two int32 collections using the given join method.
    70  func JoinInt32(larr, rarr []int32, fnc JoinInt32Fnc) []int32 {
    71  	return fnc(larr, rarr)
    72  }
    73  
    74  // InnerJoinInt32 finds and returns matching data from two int32 collections.
    75  func InnerJoinInt32(lx, rx []int32) []int32 {
    76  	result := make([]int32, 0, len(lx)+len(rx))
    77  	rhash := hashSliceInt32(rx)
    78  	lhash := make(map[int32]struct{}, len(lx))
    79  
    80  	for _, v := range lx {
    81  		_, ok := rhash[v]
    82  		_, alreadyExists := lhash[v]
    83  		if ok && !alreadyExists {
    84  			lhash[v] = struct{}{}
    85  			result = append(result, v)
    86  		}
    87  	}
    88  	return result
    89  }
    90  
    91  // OuterJoinInt32 finds and returns dissimilar data from two int32 collections.
    92  func OuterJoinInt32(lx, rx []int32) []int32 {
    93  	ljoin := LeftJoinInt32(lx, rx)
    94  	rjoin := RightJoinInt32(lx, rx)
    95  
    96  	result := make([]int32, len(ljoin)+len(rjoin))
    97  	for i, v := range ljoin {
    98  		result[i] = v
    99  	}
   100  	for i, v := range rjoin {
   101  		result[len(ljoin)+i] = v
   102  	}
   103  	return result
   104  }
   105  
   106  // LeftJoinInt32 finds and returns dissimilar data from the first int32 collection (left).
   107  func LeftJoinInt32(lx, rx []int32) []int32 {
   108  	result := make([]int32, 0, len(lx))
   109  	rhash := hashSliceInt32(rx)
   110  
   111  	for _, v := range lx {
   112  		_, ok := rhash[v]
   113  		if !ok {
   114  			result = append(result, v)
   115  		}
   116  	}
   117  	return result
   118  }
   119  
   120  // LeftJoinInt32 finds and returns dissimilar data from the second int32 collection (right).
   121  func RightJoinInt32(lx, rx []int32) []int32 { return LeftJoinInt32(rx, lx) }
   122  
   123  func hashSliceInt32(arr []int32) map[int32]struct{} {
   124  	hash := make(map[int32]struct{}, len(arr))
   125  	for _, i := range arr {
   126  		hash[i] = struct{}{}
   127  	}
   128  	return hash
   129  }
   130  
   131  type JoinInt64Fnc func(lx, rx []int64) []int64
   132  
   133  // JoinInt64 combines two int64 collections using the given join method.
   134  func JoinInt64(larr, rarr []int64, fnc JoinInt64Fnc) []int64 {
   135  	return fnc(larr, rarr)
   136  }
   137  
   138  // InnerJoinInt64 finds and returns matching data from two int64 collections.
   139  func InnerJoinInt64(lx, rx []int64) []int64 {
   140  	result := make([]int64, 0, len(lx)+len(rx))
   141  	rhash := hashSliceInt64(rx)
   142  	lhash := make(map[int64]struct{}, len(lx))
   143  
   144  	for _, v := range lx {
   145  		_, ok := rhash[v]
   146  		_, alreadyExists := lhash[v]
   147  		if ok && !alreadyExists {
   148  			lhash[v] = struct{}{}
   149  			result = append(result, v)
   150  		}
   151  	}
   152  	return result
   153  }
   154  
   155  // OuterJoinInt64 finds and returns dissimilar data from two int64 collections.
   156  func OuterJoinInt64(lx, rx []int64) []int64 {
   157  	ljoin := LeftJoinInt64(lx, rx)
   158  	rjoin := RightJoinInt64(lx, rx)
   159  
   160  	result := make([]int64, len(ljoin)+len(rjoin))
   161  	for i, v := range ljoin {
   162  		result[i] = v
   163  	}
   164  	for i, v := range rjoin {
   165  		result[len(ljoin)+i] = v
   166  	}
   167  	return result
   168  }
   169  
   170  // LeftJoinInt64 finds and returns dissimilar data from the first int64 collection (left).
   171  func LeftJoinInt64(lx, rx []int64) []int64 {
   172  	result := make([]int64, 0, len(lx))
   173  	rhash := hashSliceInt64(rx)
   174  
   175  	for _, v := range lx {
   176  		_, ok := rhash[v]
   177  		if !ok {
   178  			result = append(result, v)
   179  		}
   180  	}
   181  	return result
   182  }
   183  
   184  // LeftJoinInt64 finds and returns dissimilar data from the second int64 collection (right).
   185  func RightJoinInt64(lx, rx []int64) []int64 { return LeftJoinInt64(rx, lx) }
   186  
   187  func hashSliceInt64(arr []int64) map[int64]struct{} {
   188  	hash := make(map[int64]struct{}, len(arr))
   189  	for _, i := range arr {
   190  		hash[i] = struct{}{}
   191  	}
   192  	return hash
   193  }
   194  
   195  type JoinStringFnc func(lx, rx []string) []string
   196  
   197  // JoinString combines two string collections using the given join method.
   198  func JoinString(larr, rarr []string, fnc JoinStringFnc) []string {
   199  	return fnc(larr, rarr)
   200  }
   201  
   202  // InnerJoinString finds and returns matching data from two string collections.
   203  func InnerJoinString(lx, rx []string) []string {
   204  	result := make([]string, 0, len(lx)+len(rx))
   205  	rhash := hashSliceString(rx)
   206  	lhash := make(map[string]struct{}, len(lx))
   207  
   208  	for _, v := range lx {
   209  		_, ok := rhash[v]
   210  		_, alreadyExists := lhash[v]
   211  		if ok && !alreadyExists {
   212  			lhash[v] = struct{}{}
   213  			result = append(result, v)
   214  		}
   215  	}
   216  	return result
   217  }
   218  
   219  // OuterJoinString finds and returns dissimilar data from two string collections.
   220  func OuterJoinString(lx, rx []string) []string {
   221  	ljoin := LeftJoinString(lx, rx)
   222  	rjoin := RightJoinString(lx, rx)
   223  
   224  	result := make([]string, len(ljoin)+len(rjoin))
   225  	for i, v := range ljoin {
   226  		result[i] = v
   227  	}
   228  	for i, v := range rjoin {
   229  		result[len(ljoin)+i] = v
   230  	}
   231  	return result
   232  }
   233  
   234  // LeftJoinString finds and returns dissimilar data from the first string collection (left).
   235  func LeftJoinString(lx, rx []string) []string {
   236  	result := make([]string, 0, len(lx))
   237  	rhash := hashSliceString(rx)
   238  
   239  	for _, v := range lx {
   240  		_, ok := rhash[v]
   241  		if !ok {
   242  			result = append(result, v)
   243  		}
   244  	}
   245  	return result
   246  }
   247  
   248  // LeftJoinString finds and returns dissimilar data from the second string collection (right).
   249  func RightJoinString(lx, rx []string) []string { return LeftJoinString(rx, lx) }
   250  
   251  func hashSliceString(arr []string) map[string]struct{} {
   252  	hash := make(map[string]struct{}, len(arr))
   253  	for _, i := range arr {
   254  		hash[i] = struct{}{}
   255  	}
   256  	return hash
   257  }
   258  
   259  type JoinFloat32Fnc func(lx, rx []float32) []float32
   260  
   261  // JoinFloat32 combines two float32 collections using the given join method.
   262  func JoinFloat32(larr, rarr []float32, fnc JoinFloat32Fnc) []float32 {
   263  	return fnc(larr, rarr)
   264  }
   265  
   266  // InnerJoinFloat32 finds and returns matching data from two float32 collections.
   267  func InnerJoinFloat32(lx, rx []float32) []float32 {
   268  	result := make([]float32, 0, len(lx)+len(rx))
   269  	rhash := hashSliceFloat32(rx)
   270  	lhash := make(map[float32]struct{}, len(lx))
   271  
   272  	for _, v := range lx {
   273  		_, ok := rhash[v]
   274  		_, alreadyExists := lhash[v]
   275  		if ok && !alreadyExists {
   276  			lhash[v] = struct{}{}
   277  			result = append(result, v)
   278  		}
   279  	}
   280  	return result
   281  }
   282  
   283  // OuterJoinFloat32 finds and returns dissimilar data from two float32 collections.
   284  func OuterJoinFloat32(lx, rx []float32) []float32 {
   285  	ljoin := LeftJoinFloat32(lx, rx)
   286  	rjoin := RightJoinFloat32(lx, rx)
   287  
   288  	result := make([]float32, len(ljoin)+len(rjoin))
   289  	for i, v := range ljoin {
   290  		result[i] = v
   291  	}
   292  	for i, v := range rjoin {
   293  		result[len(ljoin)+i] = v
   294  	}
   295  	return result
   296  }
   297  
   298  // LeftJoinFloat32 finds and returns dissimilar data from the first float32 collection (left).
   299  func LeftJoinFloat32(lx, rx []float32) []float32 {
   300  	result := make([]float32, 0, len(lx))
   301  	rhash := hashSliceFloat32(rx)
   302  
   303  	for _, v := range lx {
   304  		_, ok := rhash[v]
   305  		if !ok {
   306  			result = append(result, v)
   307  		}
   308  	}
   309  	return result
   310  }
   311  
   312  // LeftJoinFloat32 finds and returns dissimilar data from the second float32 collection (right).
   313  func RightJoinFloat32(lx, rx []float32) []float32 { return LeftJoinFloat32(rx, lx) }
   314  
   315  func hashSliceFloat32(arr []float32) map[float32]struct{} {
   316  	hash := make(map[float32]struct{}, len(arr))
   317  	for _, i := range arr {
   318  		hash[i] = struct{}{}
   319  	}
   320  	return hash
   321  }
   322  
   323  type JoinFloat64Fnc func(lx, rx []float64) []float64
   324  
   325  // JoinFloat64 combines two float64 collections using the given join method.
   326  func JoinFloat64(larr, rarr []float64, fnc JoinFloat64Fnc) []float64 {
   327  	return fnc(larr, rarr)
   328  }
   329  
   330  // InnerJoinFloat64 finds and returns matching data from two float64 collections.
   331  func InnerJoinFloat64(lx, rx []float64) []float64 {
   332  	result := make([]float64, 0, len(lx)+len(rx))
   333  	rhash := hashSliceFloat64(rx)
   334  	lhash := make(map[float64]struct{}, len(lx))
   335  
   336  	for _, v := range lx {
   337  		_, ok := rhash[v]
   338  		_, alreadyExists := lhash[v]
   339  		if ok && !alreadyExists {
   340  			lhash[v] = struct{}{}
   341  			result = append(result, v)
   342  		}
   343  	}
   344  	return result
   345  }
   346  
   347  // OuterJoinFloat64 finds and returns dissimilar data from two float64 collections.
   348  func OuterJoinFloat64(lx, rx []float64) []float64 {
   349  	ljoin := LeftJoinFloat64(lx, rx)
   350  	rjoin := RightJoinFloat64(lx, rx)
   351  
   352  	result := make([]float64, len(ljoin)+len(rjoin))
   353  	for i, v := range ljoin {
   354  		result[i] = v
   355  	}
   356  	for i, v := range rjoin {
   357  		result[len(ljoin)+i] = v
   358  	}
   359  	return result
   360  }
   361  
   362  // LeftJoinFloat64 finds and returns dissimilar data from the first float64 collection (left).
   363  func LeftJoinFloat64(lx, rx []float64) []float64 {
   364  	result := make([]float64, 0, len(lx))
   365  	rhash := hashSliceFloat64(rx)
   366  
   367  	for _, v := range lx {
   368  		_, ok := rhash[v]
   369  		if !ok {
   370  			result = append(result, v)
   371  		}
   372  	}
   373  	return result
   374  }
   375  
   376  // LeftJoinFloat64 finds and returns dissimilar data from the second float64 collection (right).
   377  func RightJoinFloat64(lx, rx []float64) []float64 { return LeftJoinFloat64(rx, lx) }
   378  
   379  func hashSliceFloat64(arr []float64) map[float64]struct{} {
   380  	hash := make(map[float64]struct{}, len(arr))
   381  	for _, i := range arr {
   382  		hash[i] = struct{}{}
   383  	}
   384  	return hash
   385  }
   386  

View as plain text