edges.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //Copyright 2013 GoGraphviz Authors
  2. //
  3. //Licensed under the Apache License, Version 2.0 (the "License");
  4. //you may not use this file except in compliance with the License.
  5. //You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. //Unless required by applicable law or agreed to in writing, software
  10. //distributed under the License is distributed on an "AS IS" BASIS,
  11. //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. //See the License for the specific language governing permissions and
  13. //limitations under the License.
  14. package gographviz
  15. import (
  16. "sort"
  17. )
  18. // Edge represents an Edge.
  19. type Edge struct {
  20. Src string
  21. SrcPort string
  22. Dst string
  23. DstPort string
  24. Dir bool
  25. Attrs Attrs
  26. }
  27. // Edges represents a set of Edges.
  28. type Edges struct {
  29. SrcToDsts map[string]map[string][]*Edge
  30. DstToSrcs map[string]map[string][]*Edge
  31. Edges []*Edge
  32. }
  33. // NewEdges creates a blank set of Edges.
  34. func NewEdges() *Edges {
  35. return &Edges{make(map[string]map[string][]*Edge), make(map[string]map[string][]*Edge), make([]*Edge, 0)}
  36. }
  37. // Add adds an Edge to the set of Edges.
  38. func (edges *Edges) Add(edge *Edge) {
  39. if _, ok := edges.SrcToDsts[edge.Src]; !ok {
  40. edges.SrcToDsts[edge.Src] = make(map[string][]*Edge)
  41. }
  42. if _, ok := edges.SrcToDsts[edge.Src][edge.Dst]; !ok {
  43. edges.SrcToDsts[edge.Src][edge.Dst] = make([]*Edge, 0)
  44. }
  45. edges.SrcToDsts[edge.Src][edge.Dst] = append(edges.SrcToDsts[edge.Src][edge.Dst], edge)
  46. if _, ok := edges.DstToSrcs[edge.Dst]; !ok {
  47. edges.DstToSrcs[edge.Dst] = make(map[string][]*Edge)
  48. }
  49. if _, ok := edges.DstToSrcs[edge.Dst][edge.Src]; !ok {
  50. edges.DstToSrcs[edge.Dst][edge.Src] = make([]*Edge, 0)
  51. }
  52. edges.DstToSrcs[edge.Dst][edge.Src] = append(edges.DstToSrcs[edge.Dst][edge.Src], edge)
  53. edges.Edges = append(edges.Edges, edge)
  54. }
  55. // Sorted returns a sorted list of Edges.
  56. func (edges Edges) Sorted() []*Edge {
  57. es := make(edgeSorter, len(edges.Edges))
  58. copy(es, edges.Edges)
  59. sort.Sort(es)
  60. return es
  61. }
  62. type edgeSorter []*Edge
  63. func (es edgeSorter) Len() int { return len(es) }
  64. func (es edgeSorter) Swap(i, j int) { es[i], es[j] = es[j], es[i] }
  65. func (es edgeSorter) Less(i, j int) bool {
  66. if es[i].Src < es[j].Src {
  67. return true
  68. } else if es[i].Src > es[j].Src {
  69. return false
  70. }
  71. if es[i].Dst < es[j].Dst {
  72. return true
  73. } else if es[i].Dst > es[j].Dst {
  74. return false
  75. }
  76. if es[i].SrcPort < es[j].SrcPort {
  77. return true
  78. } else if es[i].SrcPort > es[j].SrcPort {
  79. return false
  80. }
  81. if es[i].DstPort < es[j].DstPort {
  82. return true
  83. } else if es[i].DstPort > es[j].DstPort {
  84. return false
  85. }
  86. if es[i].Dir != es[j].Dir {
  87. return es[i].Dir
  88. }
  89. attrs := es[i].Attrs.Copy()
  90. for k, v := range es[j].Attrs {
  91. attrs[k] = v
  92. }
  93. for _, k := range attrs.sortedNames() {
  94. if es[i].Attrs[k] < es[j].Attrs[k] {
  95. return true
  96. } else if es[i].Attrs[k] > es[j].Attrs[k] {
  97. return false
  98. }
  99. }
  100. return false
  101. }