Skip to content

Commit

Permalink
Replace graph map with slice.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed Sep 10, 2023
1 parent 4438f8b commit 65175a3
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions graph.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package rdf

type Graph struct {
triples map[*Triple]struct{}
triples []*Triple
}

func NewGraph(ts ...*Triple) *Graph {
triples := make(map[*Triple]struct{})
for _, t := range ts {
triples[t] = struct{}{}
}
return &Graph{triples}
return &Graph{triples: ts}
}

func (g *Graph) Add(s, p, o Node) {
t := &Triple{s, p, o}
g.triples[t] = struct{}{}
g.triples = append(g.triples, t)
}

// Find returns the first triple matching the given pattern.
Expand Down Expand Up @@ -43,13 +39,18 @@ func (g *Graph) FindAll(s, p, o Node) []*Triple {
}

func (g *Graph) Remove(t *Triple) {
delete(g.triples, t)
for i, other := range g.triples {
if t.Equal(other) {
g.triples = append(g.triples[:i], g.triples[i+1:]...)
return
}
}
}

func (g *Graph) iter() chan *Triple {
ch := make(chan *Triple)
go func() {
for t := range g.triples {
for _, t := range g.triples {
ch <- t
}
close(ch)
Expand Down

0 comments on commit 65175a3

Please sign in to comment.