github.com/kazu/loncha@v0.6.3/uniq/slice.gtpl (about)

     1  // generated by github.com/kazu/loncha/structer
     2  
     3  // Uniq is functions like a generics
     4  // this is tuning performancem, reduce heap usage.
     5  // Copyright 2009 The Go Authors. All rights reserved.
     6  // Use of this source code is governed by a BSD-style
     7  // license that can be found in the LICENSE file.
     8  package {{.PkgName}}
     9  
    10  import (
    11      "github.com/kazu/loncha"
    12  )
    13  
    14  type {{.Name}}Slice []{{.Name}}
    15  type {{.Name}}PtrSlice []*{{.Name}}
    16  
    17  
    18  func(d {{.Name}}Slice) Uniq(fields ...string) {{.Name}}Slice {
    19  	for _, field := range fields {
    20  		switch field {
    21  			{{range $k, $v := .Fields}}
    22  		case "{{$k}}":
    23  			exists := map[{{$v}}]bool{}
    24  		
    25  			loncha.Filter(&d, func(i int) bool {
    26  				if !exists[d[i].{{$k}}] {
    27  					exists[d[i].{{$k}}] = true
    28  					return true
    29  				}
    30  				return false
    31  			})
    32  
    33  			{{end}}
    34  		}
    35  	}
    36  	return d	
    37  }
    38  
    39  func(d {{.Name}}PtrSlice) Uniq(fields ...string) {{.Name}}PtrSlice {
    40  	for _, field := range fields {
    41  		switch field {
    42  			{{range $k, $v := .Fields}}
    43  		case "{{$k}}":
    44  			exists := map[{{$v}}]bool{}
    45  		
    46  			loncha.Filter(&d, func(i int) bool {
    47  				if !exists[d[i].{{$k}}] {
    48  					exists[d[i].{{$k}}] = true
    49  					return true
    50  				}
    51  				return false
    52  			})
    53  
    54  			{{end}}
    55  		}
    56  	}
    57  	return d	
    58  }