Skip to content

Commit

Permalink
lib: utility functions for manipulating sets of primitive type
Browse files Browse the repository at this point in the history
elements represented using maps

I have seen multiple implementations of string sets and integer sets
and methods the perform set operation and convert between sets and
slices. This CL provides a comprehensive set of reusable utility
functions for handling sets.

NOTE: Most of the code in this CL is auto-generated; focus your review
efforts on doc.go and gen.go.

Change-Id: If8def2d905264a26c75fa63a600dae498a42601b
  • Loading branch information
jsimsa committed Jun 15, 2015
1 parent 2ab4d05 commit ff067cf
Show file tree
Hide file tree
Showing 67 changed files with 8,733 additions and 0 deletions.
224 changes: 224 additions & 0 deletions set/.api
@@ -0,0 +1,224 @@
pkg set, method (Complex128BoolT) Difference(map[complex128]bool, map[complex128]bool)
pkg set, method (Complex128BoolT) FromSlice([]complex128) map[complex128]bool
pkg set, method (Complex128BoolT) Intersection(map[complex128]bool, map[complex128]bool)
pkg set, method (Complex128BoolT) ToSlice(map[complex128]bool) []complex128
pkg set, method (Complex128BoolT) Union(map[complex128]bool, map[complex128]bool)
pkg set, method (Complex128T) Difference(map[complex128]struct{}, map[complex128]struct{})
pkg set, method (Complex128T) FromSlice([]complex128) map[complex128]struct{}
pkg set, method (Complex128T) Intersection(map[complex128]struct{}, map[complex128]struct{})
pkg set, method (Complex128T) ToSlice(map[complex128]struct{}) []complex128
pkg set, method (Complex128T) Union(map[complex128]struct{}, map[complex128]struct{})
pkg set, method (Complex64BoolT) Difference(map[complex64]bool, map[complex64]bool)
pkg set, method (Complex64BoolT) FromSlice([]complex64) map[complex64]bool
pkg set, method (Complex64BoolT) Intersection(map[complex64]bool, map[complex64]bool)
pkg set, method (Complex64BoolT) ToSlice(map[complex64]bool) []complex64
pkg set, method (Complex64BoolT) Union(map[complex64]bool, map[complex64]bool)
pkg set, method (Complex64T) Difference(map[complex64]struct{}, map[complex64]struct{})
pkg set, method (Complex64T) FromSlice([]complex64) map[complex64]struct{}
pkg set, method (Complex64T) Intersection(map[complex64]struct{}, map[complex64]struct{})
pkg set, method (Complex64T) ToSlice(map[complex64]struct{}) []complex64
pkg set, method (Complex64T) Union(map[complex64]struct{}, map[complex64]struct{})
pkg set, method (Float32BoolT) Difference(map[float32]bool, map[float32]bool)
pkg set, method (Float32BoolT) FromSlice([]float32) map[float32]bool
pkg set, method (Float32BoolT) Intersection(map[float32]bool, map[float32]bool)
pkg set, method (Float32BoolT) ToSlice(map[float32]bool) []float32
pkg set, method (Float32BoolT) Union(map[float32]bool, map[float32]bool)
pkg set, method (Float32T) Difference(map[float32]struct{}, map[float32]struct{})
pkg set, method (Float32T) FromSlice([]float32) map[float32]struct{}
pkg set, method (Float32T) Intersection(map[float32]struct{}, map[float32]struct{})
pkg set, method (Float32T) ToSlice(map[float32]struct{}) []float32
pkg set, method (Float32T) Union(map[float32]struct{}, map[float32]struct{})
pkg set, method (Float64BoolT) Difference(map[float64]bool, map[float64]bool)
pkg set, method (Float64BoolT) FromSlice([]float64) map[float64]bool
pkg set, method (Float64BoolT) Intersection(map[float64]bool, map[float64]bool)
pkg set, method (Float64BoolT) ToSlice(map[float64]bool) []float64
pkg set, method (Float64BoolT) Union(map[float64]bool, map[float64]bool)
pkg set, method (Float64T) Difference(map[float64]struct{}, map[float64]struct{})
pkg set, method (Float64T) FromSlice([]float64) map[float64]struct{}
pkg set, method (Float64T) Intersection(map[float64]struct{}, map[float64]struct{})
pkg set, method (Float64T) ToSlice(map[float64]struct{}) []float64
pkg set, method (Float64T) Union(map[float64]struct{}, map[float64]struct{})
pkg set, method (Int16BoolT) Difference(map[int16]bool, map[int16]bool)
pkg set, method (Int16BoolT) FromSlice([]int16) map[int16]bool
pkg set, method (Int16BoolT) Intersection(map[int16]bool, map[int16]bool)
pkg set, method (Int16BoolT) ToSlice(map[int16]bool) []int16
pkg set, method (Int16BoolT) Union(map[int16]bool, map[int16]bool)
pkg set, method (Int16T) Difference(map[int16]struct{}, map[int16]struct{})
pkg set, method (Int16T) FromSlice([]int16) map[int16]struct{}
pkg set, method (Int16T) Intersection(map[int16]struct{}, map[int16]struct{})
pkg set, method (Int16T) ToSlice(map[int16]struct{}) []int16
pkg set, method (Int16T) Union(map[int16]struct{}, map[int16]struct{})
pkg set, method (Int32BoolT) Difference(map[int32]bool, map[int32]bool)
pkg set, method (Int32BoolT) FromSlice([]int32) map[int32]bool
pkg set, method (Int32BoolT) Intersection(map[int32]bool, map[int32]bool)
pkg set, method (Int32BoolT) ToSlice(map[int32]bool) []int32
pkg set, method (Int32BoolT) Union(map[int32]bool, map[int32]bool)
pkg set, method (Int32T) Difference(map[int32]struct{}, map[int32]struct{})
pkg set, method (Int32T) FromSlice([]int32) map[int32]struct{}
pkg set, method (Int32T) Intersection(map[int32]struct{}, map[int32]struct{})
pkg set, method (Int32T) ToSlice(map[int32]struct{}) []int32
pkg set, method (Int32T) Union(map[int32]struct{}, map[int32]struct{})
pkg set, method (Int64BoolT) Difference(map[int64]bool, map[int64]bool)
pkg set, method (Int64BoolT) FromSlice([]int64) map[int64]bool
pkg set, method (Int64BoolT) Intersection(map[int64]bool, map[int64]bool)
pkg set, method (Int64BoolT) ToSlice(map[int64]bool) []int64
pkg set, method (Int64BoolT) Union(map[int64]bool, map[int64]bool)
pkg set, method (Int64T) Difference(map[int64]struct{}, map[int64]struct{})
pkg set, method (Int64T) FromSlice([]int64) map[int64]struct{}
pkg set, method (Int64T) Intersection(map[int64]struct{}, map[int64]struct{})
pkg set, method (Int64T) ToSlice(map[int64]struct{}) []int64
pkg set, method (Int64T) Union(map[int64]struct{}, map[int64]struct{})
pkg set, method (Int8BoolT) Difference(map[int8]bool, map[int8]bool)
pkg set, method (Int8BoolT) FromSlice([]int8) map[int8]bool
pkg set, method (Int8BoolT) Intersection(map[int8]bool, map[int8]bool)
pkg set, method (Int8BoolT) ToSlice(map[int8]bool) []int8
pkg set, method (Int8BoolT) Union(map[int8]bool, map[int8]bool)
pkg set, method (Int8T) Difference(map[int8]struct{}, map[int8]struct{})
pkg set, method (Int8T) FromSlice([]int8) map[int8]struct{}
pkg set, method (Int8T) Intersection(map[int8]struct{}, map[int8]struct{})
pkg set, method (Int8T) ToSlice(map[int8]struct{}) []int8
pkg set, method (Int8T) Union(map[int8]struct{}, map[int8]struct{})
pkg set, method (IntBoolT) Difference(map[int]bool, map[int]bool)
pkg set, method (IntBoolT) FromSlice([]int) map[int]bool
pkg set, method (IntBoolT) Intersection(map[int]bool, map[int]bool)
pkg set, method (IntBoolT) ToSlice(map[int]bool) []int
pkg set, method (IntBoolT) Union(map[int]bool, map[int]bool)
pkg set, method (IntT) Difference(map[int]struct{}, map[int]struct{})
pkg set, method (IntT) FromSlice([]int) map[int]struct{}
pkg set, method (IntT) Intersection(map[int]struct{}, map[int]struct{})
pkg set, method (IntT) ToSlice(map[int]struct{}) []int
pkg set, method (IntT) Union(map[int]struct{}, map[int]struct{})
pkg set, method (StringBoolT) Difference(map[string]bool, map[string]bool)
pkg set, method (StringBoolT) FromSlice([]string) map[string]bool
pkg set, method (StringBoolT) Intersection(map[string]bool, map[string]bool)
pkg set, method (StringBoolT) ToSlice(map[string]bool) []string
pkg set, method (StringBoolT) Union(map[string]bool, map[string]bool)
pkg set, method (StringT) Difference(map[string]struct{}, map[string]struct{})
pkg set, method (StringT) FromSlice([]string) map[string]struct{}
pkg set, method (StringT) Intersection(map[string]struct{}, map[string]struct{})
pkg set, method (StringT) ToSlice(map[string]struct{}) []string
pkg set, method (StringT) Union(map[string]struct{}, map[string]struct{})
pkg set, method (Uint16BoolT) Difference(map[uint16]bool, map[uint16]bool)
pkg set, method (Uint16BoolT) FromSlice([]uint16) map[uint16]bool
pkg set, method (Uint16BoolT) Intersection(map[uint16]bool, map[uint16]bool)
pkg set, method (Uint16BoolT) ToSlice(map[uint16]bool) []uint16
pkg set, method (Uint16BoolT) Union(map[uint16]bool, map[uint16]bool)
pkg set, method (Uint16T) Difference(map[uint16]struct{}, map[uint16]struct{})
pkg set, method (Uint16T) FromSlice([]uint16) map[uint16]struct{}
pkg set, method (Uint16T) Intersection(map[uint16]struct{}, map[uint16]struct{})
pkg set, method (Uint16T) ToSlice(map[uint16]struct{}) []uint16
pkg set, method (Uint16T) Union(map[uint16]struct{}, map[uint16]struct{})
pkg set, method (Uint32BoolT) Difference(map[uint32]bool, map[uint32]bool)
pkg set, method (Uint32BoolT) FromSlice([]uint32) map[uint32]bool
pkg set, method (Uint32BoolT) Intersection(map[uint32]bool, map[uint32]bool)
pkg set, method (Uint32BoolT) ToSlice(map[uint32]bool) []uint32
pkg set, method (Uint32BoolT) Union(map[uint32]bool, map[uint32]bool)
pkg set, method (Uint32T) Difference(map[uint32]struct{}, map[uint32]struct{})
pkg set, method (Uint32T) FromSlice([]uint32) map[uint32]struct{}
pkg set, method (Uint32T) Intersection(map[uint32]struct{}, map[uint32]struct{})
pkg set, method (Uint32T) ToSlice(map[uint32]struct{}) []uint32
pkg set, method (Uint32T) Union(map[uint32]struct{}, map[uint32]struct{})
pkg set, method (Uint64BoolT) Difference(map[uint64]bool, map[uint64]bool)
pkg set, method (Uint64BoolT) FromSlice([]uint64) map[uint64]bool
pkg set, method (Uint64BoolT) Intersection(map[uint64]bool, map[uint64]bool)
pkg set, method (Uint64BoolT) ToSlice(map[uint64]bool) []uint64
pkg set, method (Uint64BoolT) Union(map[uint64]bool, map[uint64]bool)
pkg set, method (Uint64T) Difference(map[uint64]struct{}, map[uint64]struct{})
pkg set, method (Uint64T) FromSlice([]uint64) map[uint64]struct{}
pkg set, method (Uint64T) Intersection(map[uint64]struct{}, map[uint64]struct{})
pkg set, method (Uint64T) ToSlice(map[uint64]struct{}) []uint64
pkg set, method (Uint64T) Union(map[uint64]struct{}, map[uint64]struct{})
pkg set, method (Uint8BoolT) Difference(map[uint8]bool, map[uint8]bool)
pkg set, method (Uint8BoolT) FromSlice([]uint8) map[uint8]bool
pkg set, method (Uint8BoolT) Intersection(map[uint8]bool, map[uint8]bool)
pkg set, method (Uint8BoolT) ToSlice(map[uint8]bool) []uint8
pkg set, method (Uint8BoolT) Union(map[uint8]bool, map[uint8]bool)
pkg set, method (Uint8T) Difference(map[uint8]struct{}, map[uint8]struct{})
pkg set, method (Uint8T) FromSlice([]uint8) map[uint8]struct{}
pkg set, method (Uint8T) Intersection(map[uint8]struct{}, map[uint8]struct{})
pkg set, method (Uint8T) ToSlice(map[uint8]struct{}) []uint8
pkg set, method (Uint8T) Union(map[uint8]struct{}, map[uint8]struct{})
pkg set, method (UintBoolT) Difference(map[uint]bool, map[uint]bool)
pkg set, method (UintBoolT) FromSlice([]uint) map[uint]bool
pkg set, method (UintBoolT) Intersection(map[uint]bool, map[uint]bool)
pkg set, method (UintBoolT) ToSlice(map[uint]bool) []uint
pkg set, method (UintBoolT) Union(map[uint]bool, map[uint]bool)
pkg set, method (UintT) Difference(map[uint]struct{}, map[uint]struct{})
pkg set, method (UintT) FromSlice([]uint) map[uint]struct{}
pkg set, method (UintT) Intersection(map[uint]struct{}, map[uint]struct{})
pkg set, method (UintT) ToSlice(map[uint]struct{}) []uint
pkg set, method (UintT) Union(map[uint]struct{}, map[uint]struct{})
pkg set, method (UintptrBoolT) Difference(map[uintptr]bool, map[uintptr]bool)
pkg set, method (UintptrBoolT) FromSlice([]uintptr) map[uintptr]bool
pkg set, method (UintptrBoolT) Intersection(map[uintptr]bool, map[uintptr]bool)
pkg set, method (UintptrBoolT) ToSlice(map[uintptr]bool) []uintptr
pkg set, method (UintptrBoolT) Union(map[uintptr]bool, map[uintptr]bool)
pkg set, method (UintptrT) Difference(map[uintptr]struct{}, map[uintptr]struct{})
pkg set, method (UintptrT) FromSlice([]uintptr) map[uintptr]struct{}
pkg set, method (UintptrT) Intersection(map[uintptr]struct{}, map[uintptr]struct{})
pkg set, method (UintptrT) ToSlice(map[uintptr]struct{}) []uintptr
pkg set, method (UintptrT) Union(map[uintptr]struct{}, map[uintptr]struct{})
pkg set, type Complex128BoolT struct
pkg set, type Complex128T struct
pkg set, type Complex64BoolT struct
pkg set, type Complex64T struct
pkg set, type Float32BoolT struct
pkg set, type Float32T struct
pkg set, type Float64BoolT struct
pkg set, type Float64T struct
pkg set, type Int16BoolT struct
pkg set, type Int16T struct
pkg set, type Int32BoolT struct
pkg set, type Int32T struct
pkg set, type Int64BoolT struct
pkg set, type Int64T struct
pkg set, type Int8BoolT struct
pkg set, type Int8T struct
pkg set, type IntBoolT struct
pkg set, type IntT struct
pkg set, type StringBoolT struct
pkg set, type StringT struct
pkg set, type Uint16BoolT struct
pkg set, type Uint16T struct
pkg set, type Uint32BoolT struct
pkg set, type Uint32T struct
pkg set, type Uint64BoolT struct
pkg set, type Uint64T struct
pkg set, type Uint8BoolT struct
pkg set, type Uint8T struct
pkg set, type UintBoolT struct
pkg set, type UintT struct
pkg set, type UintptrBoolT struct
pkg set, type UintptrT struct
pkg set, var Complex128 Complex128T
pkg set, var Complex128Bool Complex128BoolT
pkg set, var Complex64 Complex64T
pkg set, var Complex64Bool Complex64BoolT
pkg set, var Float32 Float32T
pkg set, var Float32Bool Float32BoolT
pkg set, var Float64 Float64T
pkg set, var Float64Bool Float64BoolT
pkg set, var Int IntT
pkg set, var Int16 Int16T
pkg set, var Int16Bool Int16BoolT
pkg set, var Int32 Int32T
pkg set, var Int32Bool Int32BoolT
pkg set, var Int64 Int64T
pkg set, var Int64Bool Int64BoolT
pkg set, var Int8 Int8T
pkg set, var Int8Bool Int8BoolT
pkg set, var IntBool IntBoolT
pkg set, var String StringT
pkg set, var StringBool StringBoolT
pkg set, var Uint UintT
pkg set, var Uint16 Uint16T
pkg set, var Uint16Bool Uint16BoolT
pkg set, var Uint32 Uint32T
pkg set, var Uint32Bool Uint32BoolT
pkg set, var Uint64 Uint64T
pkg set, var Uint64Bool Uint64BoolT
pkg set, var Uint8 Uint8T
pkg set, var Uint8Bool Uint8BoolT
pkg set, var UintBool UintBoolT
pkg set, var Uintptr UintptrT
pkg set, var UintptrBool UintptrBoolT
58 changes: 58 additions & 0 deletions set/complex128.go
@@ -0,0 +1,58 @@
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This file was auto-generated via go generate.
// DO NOT UPDATE MANUALLY

package set

var Complex128 = Complex128T{}

type Complex128T struct{}

// FromSlice transforms the given slice to a set.
func (Complex128T) FromSlice(els []complex128) map[complex128]struct{} {
if len(els) == 0 {
return nil
}
result := map[complex128]struct{}{}
for _, el := range els {
result[el] = struct{}{}
}
return result
}

// ToSlice transforms the given set to a slice.
func (Complex128T) ToSlice(s map[complex128]struct{}) []complex128 {
var result []complex128
for el, _ := range s {
result = append(result, el)
}
return result
}

// Difference subtracts s2 from s1, storing the result in s1.
func (Complex128T) Difference(s1, s2 map[complex128]struct{}) {
for el, _ := range s1 {
if _, ok := s2[el]; ok {
delete(s1, el)
}
}
}

// Intersection intersects s1 and s2, storing the result in s1.
func (Complex128T) Intersection(s1, s2 map[complex128]struct{}) {
for el, _ := range s1 {
if _, ok := s2[el]; !ok {
delete(s1, el)
}
}
}

// Union merges s1 and s2, storing the result in s1.
func (Complex128T) Union(s1, s2 map[complex128]struct{}) {
for el, _ := range s2 {
s1[el] = struct{}{}
}
}

0 comments on commit ff067cf

Please sign in to comment.