gonum.org/v1/gonum@v0.14.0/graph/encoding/encoding.go (about)

     1  // Copyright ©2017 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package encoding
     6  
     7  import "gonum.org/v1/gonum/graph"
     8  
     9  // Builder is a graph that can have user-defined nodes and edges added.
    10  type Builder interface {
    11  	graph.Graph
    12  	graph.Builder
    13  }
    14  
    15  // MultiBuilder is a graph that can have user-defined nodes and edges added.
    16  type MultiBuilder interface {
    17  	graph.Multigraph
    18  	graph.MultigraphBuilder
    19  }
    20  
    21  // AttributeSetter is implemented by types that can set an encoded graph
    22  // attribute.
    23  type AttributeSetter interface {
    24  	SetAttribute(Attribute) error
    25  }
    26  
    27  // Attributer defines graph.Node or graph.Edge values that can
    28  // specify graph attributes.
    29  type Attributer interface {
    30  	Attributes() []Attribute
    31  }
    32  
    33  // Attribute is an encoded key value attribute pair use in graph encoding.
    34  type Attribute struct {
    35  	Key, Value string
    36  }
    37  
    38  // Attributes is a helper type providing simple attribute handling.
    39  type Attributes []Attribute
    40  
    41  // Attributes returns all of the receiver's attributes.
    42  func (a *Attributes) Attributes() []Attribute {
    43  	return *a
    44  }
    45  
    46  // SetAttribute sets attr in the receiver. Calling SetAttribute with an
    47  // Attribute with a Key that is in the collection replaces the existing
    48  // value and calling with an empty Value removes the attribute from the
    49  // collection if it exists. SetAttribute always returns nil.
    50  func (a *Attributes) SetAttribute(attr Attribute) error {
    51  	if attr.Key == "" {
    52  		return nil
    53  	}
    54  	for i, v := range *a {
    55  		if v.Key == attr.Key {
    56  			if attr.Value == "" {
    57  				(*a)[i] = (*a)[len(*a)-1]
    58  				*a = (*a)[:len(*a)-1]
    59  				return nil
    60  			}
    61  			(*a)[i].Value = attr.Value
    62  			return nil
    63  		}
    64  	}
    65  	if attr.Value != "" {
    66  		*a = append(*a, attr)
    67  	}
    68  	return nil
    69  }