redo data structure for sensibility

This commit is contained in:
Barak Michener 2014-08-06 04:07:30 -04:00
parent d102394836
commit 76efc2fcb7

View file

@ -36,8 +36,9 @@ type Materialize struct {
uid uint64 uid uint64
tags graph.Tagger tags graph.Tagger
containsMap map[graph.Value]int containsMap map[graph.Value]int
values []result values [][]result
index int index int
subindex int
subIt graph.Iterator subIt graph.Iterator
hasRun bool hasRun bool
aborted bool aborted bool
@ -86,7 +87,7 @@ func (it *Materialize) TagResults(dst map[string]graph.Value) {
for _, tag := range it.tags.Tags() { for _, tag := range it.tags.Tags() {
dst[tag] = it.Result() dst[tag] = it.Result()
} }
for tag, value := range it.values[it.index].tags { for tag, value := range it.values[it.index][it.subindex].tags {
dst[tag] = value dst[tag] = value
} }
} }
@ -128,7 +129,7 @@ func (it *Materialize) Result() graph.Value {
if it.index >= len(it.values) { if it.index >= len(it.values) {
return nil return nil
} }
return it.values[it.index].id return it.values[it.index][it.subindex].id
} }
func (it *Materialize) SubIterators() []graph.Iterator { func (it *Materialize) SubIterators() []graph.Iterator {
@ -177,14 +178,12 @@ func (it *Materialize) Next() (graph.Value, bool) {
return graph.Next(it.subIt) return graph.Next(it.subIt)
} }
lastVal := it.Result() it.index++
for it.index < len(it.values) { it.subindex = 0
it.index++ if it.index >= len(it.values) {
if it.Result() != lastVal && it.Result() != nil { return graph.NextLogOut(it, nil, false)
return graph.NextLogOut(it, it.Result(), true)
}
} }
return graph.NextLogOut(it, nil, false) return graph.NextLogOut(it, it.Result(), true)
} }
func (it *Materialize) Contains(v graph.Value) bool { func (it *Materialize) Contains(v graph.Value) bool {
@ -197,6 +196,7 @@ func (it *Materialize) Contains(v graph.Value) bool {
} }
if i, ok := it.containsMap[v]; ok { if i, ok := it.containsMap[v]; ok {
it.index = i it.index = i
it.subindex = 0
return graph.ContainsLogOut(it, v, true) return graph.ContainsLogOut(it, v, true)
} }
return graph.ContainsLogOut(it, v, false) return graph.ContainsLogOut(it, v, false)
@ -210,15 +210,13 @@ func (it *Materialize) NextResult() bool {
return it.subIt.NextResult() return it.subIt.NextResult()
} }
i := it.index + 1 it.subindex++
if i == len(it.values) { if it.subindex >= len(it.values[it.index]) {
// Don't go off the end of the world
it.subindex--
return false return false
} }
if it.Result() == it.values[i].id { return true
it.index = i
return true
}
return false
} }
func (it *Materialize) materializeSet() { func (it *Materialize) materializeSet() {
@ -233,14 +231,18 @@ func (it *Materialize) materializeSet() {
it.aborted = true it.aborted = true
break break
} }
if _, ok := it.containsMap[val]; !ok {
it.containsMap[val] = len(it.values)
it.values = append(it.values, nil)
}
index := it.containsMap[val]
tags := make(map[string]graph.Value) tags := make(map[string]graph.Value)
it.subIt.TagResults(tags) it.subIt.TagResults(tags)
it.containsMap[val] = len(it.values) it.values[index] = append(it.values[index], result{id: val, tags: tags})
it.values = append(it.values, result{id: val, tags: tags})
for it.subIt.NextResult() == true { for it.subIt.NextResult() == true {
tags := make(map[string]graph.Value) tags := make(map[string]graph.Value)
it.subIt.TagResults(tags) it.subIt.TagResults(tags)
it.values = append(it.values, result{id: val, tags: tags}) it.values[index] = append(it.values[index], result{id: val, tags: tags})
} }
} }
if it.aborted { if it.aborted {