commit f0d07c3663a33b75bbac7f7b5eadc78f2bc07eb8
parent b3119fa41e56dda88ee79a789e774e1330f2db8f
Author: William Casarin <jb55@jb55.com>
Date: Sat, 22 Jul 2023 21:12:53 -0700
ndb: remove TagIterators and just use sequences
Still learning...
Diffstat:
2 files changed, 14 insertions(+), 55 deletions(-)
diff --git a/nostrdb/NdbTagIterator.swift b/nostrdb/NdbTagIterator.swift
@@ -7,30 +7,15 @@
import Foundation
-struct TagSequence: Sequence {
+struct TagSequence: Sequence, IteratorProtocol {
let note: NdbNote
let tag: UnsafeMutablePointer<ndb_tag>
+ var index: Int32
var count: UInt16 {
tag.pointee.count
}
- subscript(index: Int) -> NdbTagElem? {
- if index >= tag.pointee.count {
- return nil
- }
-
- return NdbTagElem(note: note, tag: tag, index: Int32(index))
- }
-
- func makeIterator() -> TagIterator {
- return TagIterator(note: note, tag: tag)
- }
-}
-
-struct TagIterator: IteratorProtocol {
- typealias Element = NdbTagElem
-
mutating func next() -> NdbTagElem? {
guard index < tag.pointee.count else { return nil }
let el = NdbTagElem(note: note, tag: tag, index: index)
@@ -47,23 +32,8 @@ struct TagIterator: IteratorProtocol {
return NdbTagElem(note: note, tag: tag, index: Int32(index))
}
-
- var index: Int32
- let note: NdbNote
- var tag: UnsafeMutablePointer<ndb_tag>
-
- var count: UInt16 {
- tag.pointee.count
- }
-
- init(note: NdbNote, tag: UnsafeMutablePointer<ndb_tag>) {
- self.note = note
- self.tag = tag
- self.index = 0
- }
}
-
func ndb_maybe_pointee<T>(_ p: UnsafeMutablePointer<T>!) -> T? {
guard p != nil else { return nil }
return p.pointee
diff --git a/nostrdb/NdbTagsIterator.swift b/nostrdb/NdbTagsIterator.swift
@@ -7,7 +7,7 @@
import Foundation
-struct TagsIterator: IteratorProtocol {
+struct TagsSequence: Sequence, IteratorProtocol {
typealias Element = TagSequence
var done: Bool
@@ -17,7 +17,7 @@ struct TagsIterator: IteratorProtocol {
mutating func next() -> TagSequence? {
guard !done else { return nil }
- let tag_seq = TagSequence(note: note, tag: self.iter.tag)
+ let tag_seq = TagSequence(note: note, tag: self.iter.tag, index: self.iter.index)
let ok = ndb_tags_iterate_next(&self.iter)
done = ok == 0
@@ -25,25 +25,6 @@ struct TagsIterator: IteratorProtocol {
return tag_seq
}
- var count: UInt16 {
- return iter.tag.pointee.count
- }
-
- init(note: NdbNote) {
- self.iter = ndb_iterator()
- let res = ndb_tags_iterate_start(note.note, &self.iter)
- self.done = res == 0
- self.note = note
- }
-}
-
-struct TagsSequence: Sequence {
- let note: NdbNote
-
- var count: UInt16 {
- note.note.pointee.tags.count
- }
-
subscript(index: Int) -> Iterator.Element? {
var i = 0
for element in self {
@@ -55,7 +36,15 @@ struct TagsSequence: Sequence {
return nil
}
- func makeIterator() -> TagsIterator {
- return .init(note: note)
+ var count: UInt16 {
+ return iter.tag.pointee.count
+ }
+
+ init(note: NdbNote) {
+ self.iter = ndb_iterator()
+ let res = ndb_tags_iterate_start(note.note, &self.iter)
+ self.done = res == 0
+ self.note = note
}
}
+