Skip to content
This repository has been archived by the owner on Feb 4, 2021. It is now read-only.

Commit

Permalink
Remove immutable graph initializers...bring em back when they're done.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdboyer committed Mar 22, 2014
1 parent 9c0b0d4 commit cf1f9af
Showing 1 changed file with 0 additions and 66 deletions.
66 changes: 0 additions & 66 deletions graph.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package gogl

import (
"fmt"
"reflect"
)

/* Vertex structures */

// As a rule, gogl tries to place as low a requirement on its vertices as
Expand Down Expand Up @@ -113,64 +108,3 @@ type MutableWeightedGraph interface {
AddEdges(edges ...WeightedEdge)
RemoveEdges(edges ...WeightedEdge)
}

/* Initialization for immutable graphs */

type IGInitializer interface {
EnsureVertex(vertex ...Vertex)
AddEdges(edges ...Edge) bool
GetGraph() Graph
}

type ImmutableGraph interface {
Graph
EnsureVertex(vertex ...Vertex)
AddEdges(edges ...Edge)
}

var immutableGraphs map[string]ImmutableGraph

func CreateImmutableGraph(name string) (*ImmutableGraphInitializer, error) {
template := immutableGraphs[name]
if template == nil {
return nil, fmt.Errorf("gogl: Unregistered graph type %s", name)
}

// Use reflection to make a copy of the graph template
v := reflect.New(reflect.Indirect(reflect.ValueOf(template)).Type()).Interface()
graph, ok := v.(ImmutableGraph)
if !ok {
panic(fmt.Sprintf("gogl: Unable to copy graph template: %s (%v)", name, reflect.ValueOf(v).Kind().String()))
}

initializer := &ImmutableGraphInitializer{
Graph: graph,
}

return initializer, nil
}

// A ImmutableGraphInitializer provides write-only methods to populate an
// immutable graph.
type ImmutableGraphInitializer struct {
Graph ImmutableGraph
}

func (gi *ImmutableGraphInitializer) EnsureVertex(vertices ...Vertex) {
gi.Graph.EnsureVertex(vertices...)
}

func (gi *ImmutableGraphInitializer) AddEdges(edges ...Edge) {
gi.Graph.AddEdges(edges...)
}

func (gi *ImmutableGraphInitializer) GetGraph() Graph {
defer func() { gi.Graph = nil }()
return gi.Graph
}

func init() {
immutableGraphs = map[string]ImmutableGraph{
"ual": NewUndirected(),
}
}

0 comments on commit cf1f9af

Please sign in to comment.