forked from cayleygraph/cayley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
all_iterator.go
117 lines (102 loc) · 3.28 KB
/
all_iterator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2014 The Cayley Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package graph
// Defines one of the base iterators, the All iterator. Which, logically
// enough, represents all nodes or all links in the graph.
//
// This particular file is actually vestigal. It's up to the TripleStore to give
// us an All iterator that represents all things in the graph. So this is
// really the All iterator for the MemTripleStore. That said, it *is* one of
// the base iterators, and it helps just to see it here.
import (
"fmt"
"strings"
)
// An All iterator across a range of int64 values, from `max` to `min`.
type Int64AllIterator struct {
BaseIterator
max, min int64
at int64
}
// Creates a new Int64AllIterator with the given range.
func NewInt64AllIterator(min, max int64) *Int64AllIterator {
var all Int64AllIterator
BaseIteratorInit(&all.BaseIterator)
all.max = max
all.min = min
all.at = min
return &all
}
// Start back at the beginning
func (a *Int64AllIterator) Reset() {
a.at = a.min
}
func (a *Int64AllIterator) Close() {
}
func (a *Int64AllIterator) Clone() Iterator {
out := NewInt64AllIterator(a.min, a.max)
out.CopyTagsFrom(a)
return out
}
// Prints the All iterator as just an "all".
func (a *Int64AllIterator) DebugString(indent int) string {
return fmt.Sprintf("%s(%s)", strings.Repeat(" ", indent), a.Type())
}
// Next() on an Int64 all iterator is a simple incrementing counter.
// Return the next integer, and mark it as the result.
func (a *Int64AllIterator) Next() (TSVal, bool) {
NextLogIn(a)
if a.at == -1 {
return NextLogOut(a, nil, false)
}
val := a.at
a.at = a.at + 1
if a.at > a.max {
a.at = -1
}
a.Last = val
return NextLogOut(a, val, true)
}
// The number of elements in an Int64AllIterator is the size of the range.
// The size is exact.
func (a *Int64AllIterator) Size() (int64, bool) {
Size := ((a.max - a.min) + 1)
return Size, true
}
// Check() for an Int64AllIterator is merely seeing if the passed value is
// withing the range, assuming the value is an int64.
func (a *Int64AllIterator) Check(tsv TSVal) bool {
CheckLogIn(a, tsv)
v := tsv.(int64)
if a.min <= v && v <= a.max {
a.Last = v
return CheckLogOut(a, v, true)
}
return CheckLogOut(a, v, false)
}
// The type of this iterator is an "all". This is important, as it puts it in
// the class of "all iterators.
func (a *Int64AllIterator) Type() string { return "all" }
// There's nothing to optimize about this little iterator.
func (a *Int64AllIterator) Optimize() (Iterator, bool) { return a, false }
// Stats for an Int64AllIterator are simple. Super cheap to do any operation,
// and as big as the range.
func (a *Int64AllIterator) GetStats() *IteratorStats {
s, _ := a.Size()
return &IteratorStats{
CheckCost: 1,
NextCost: 1,
Size: s,
}
}