subgraphs.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. // SubGraph represents a Subgraph.
  19. type SubGraph struct {
  20. Attrs Attrs
  21. Name string
  22. }
  23. // NewSubGraph creates a new Subgraph.
  24. func NewSubGraph(name string) *SubGraph {
  25. return &SubGraph{
  26. Attrs: make(Attrs),
  27. Name: name,
  28. }
  29. }
  30. // SubGraphs represents a set of SubGraphs.
  31. type SubGraphs struct {
  32. SubGraphs map[string]*SubGraph
  33. }
  34. // NewSubGraphs creates a new blank set of SubGraphs.
  35. func NewSubGraphs() *SubGraphs {
  36. return &SubGraphs{make(map[string]*SubGraph)}
  37. }
  38. // Add adds and creates a new Subgraph to the set of SubGraphs.
  39. func (subgraphs *SubGraphs) Add(name string) {
  40. if _, ok := subgraphs.SubGraphs[name]; !ok {
  41. subgraphs.SubGraphs[name] = NewSubGraph(name)
  42. }
  43. }
  44. // Remove removes a subgraph
  45. func (subgraphs *SubGraphs) Remove(name string) {
  46. delete(subgraphs.SubGraphs, name)
  47. }
  48. // Sorted returns a sorted list of SubGraphs.
  49. func (subgraphs *SubGraphs) Sorted() []*SubGraph {
  50. keys := make([]string, 0)
  51. for key := range subgraphs.SubGraphs {
  52. keys = append(keys, key)
  53. }
  54. sort.Strings(keys)
  55. s := make([]*SubGraph, len(keys))
  56. for i, key := range keys {
  57. s[i] = subgraphs.SubGraphs[key]
  58. }
  59. return s
  60. }