This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
/
channel.go
52 lines (43 loc) · 1.75 KB
/
channel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package model
import "github.com/google/shenzhen-go/source"
// Channel represents connections between pins.
type Channel struct {
Name string `json:"-"`
Type *source.Type `json:"-"`
Capacity int `json:"cap"`
// Cache of pins this channel is attached to
Pins map[NodePin]struct{} `json:"-"`
}
// NodePin is a simple tuple of node name, pin name.
type NodePin struct{ Node, Pin string }
func (np NodePin) String() string { return np.Node + "." + np.Pin }
// AddPin is sugar for `c.Pins[NodePin{Node: node, Pin: pin}] = struct{}{}`.
// It doesn't update the node.
func (c *Channel) AddPin(node, pin string) {
c.Pins[NodePin{Node: node, Pin: pin}] = struct{}{}
}
// HasPin is sugar for `_, found := c.Pins[NodePin{Node: node, Pin: pin}]; found`.
func (c *Channel) HasPin(node, pin string) bool {
_, found := c.Pins[NodePin{Node: node, Pin: pin}]
return found
}
// RemovePin is sugar for `delete(c.Pins, NodePin{Node: node, Pin: pin})`.
// It doesn't update the node.
// If the channel now has fewer than 2 pins, it can be deleted, but
// RemovePin does not do that.
func (c *Channel) RemovePin(node, pin string) {
delete(c.Pins, NodePin{Node: node, Pin: pin})
}