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 15package gographviz 16 17import ( 18 "sort" 19) 20 21//Represents a Subgraph. 22type SubGraph struct { 23 Attrs Attrs 24 Name string 25} 26 27//Creates a new Subgraph. 28func NewSubGraph(name string) *SubGraph { 29 return &SubGraph{ 30 Attrs: make(Attrs), 31 Name: name, 32 } 33} 34 35//Represents a set of SubGraphs. 36type SubGraphs struct { 37 SubGraphs map[string]*SubGraph 38} 39 40//Creates a new blank set of SubGraphs. 41func NewSubGraphs() *SubGraphs { 42 return &SubGraphs{make(map[string]*SubGraph)} 43} 44 45//Adds and creates a new Subgraph to the set of SubGraphs. 46func (this *SubGraphs) Add(name string) { 47 if _, ok := this.SubGraphs[name]; !ok { 48 this.SubGraphs[name] = NewSubGraph(name) 49 } 50} 51 52func (this *SubGraphs) Sorted() []*SubGraph { 53 keys := make([]string, 0) 54 for key := range this.SubGraphs { 55 keys = append(keys, key) 56 } 57 sort.Strings(keys) 58 s := make([]*SubGraph, len(keys)) 59 for i, key := range keys { 60 s[i] = this.SubGraphs[key] 61 } 62 return s 63} 64