damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit dd65209a20b51e1837d09384f5fa9355220ce0dd
parent f0d07c3663a33b75bbac7f7b5eadc78f2bc07eb8
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 23 Jul 2023 10:56:12 -0700

Revert "ndb: remove TagIterators and just use sequences"

This reverts commit f0d07c3663a33b75bbac7f7b5eadc78f2bc07eb8.

Diffstat:
Mnostrdb/NdbTagIterator.swift | 34++++++++++++++++++++++++++++++++--
Mnostrdb/NdbTagsIterator.swift | 35+++++++++++++++++++++++------------
2 files changed, 55 insertions(+), 14 deletions(-)

diff --git a/nostrdb/NdbTagIterator.swift b/nostrdb/NdbTagIterator.swift @@ -7,15 +7,30 @@ import Foundation -struct TagSequence: Sequence, IteratorProtocol { +struct TagSequence: Sequence { 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) @@ -32,8 +47,23 @@ struct TagSequence: Sequence, 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 TagsSequence: Sequence, IteratorProtocol { +struct TagsIterator: IteratorProtocol { typealias Element = TagSequence var done: Bool @@ -17,7 +17,7 @@ struct TagsSequence: Sequence, IteratorProtocol { mutating func next() -> TagSequence? { guard !done else { return nil } - let tag_seq = TagSequence(note: note, tag: self.iter.tag, index: self.iter.index) + let tag_seq = TagSequence(note: note, tag: self.iter.tag) let ok = ndb_tags_iterate_next(&self.iter) done = ok == 0 @@ -25,6 +25,25 @@ struct TagsSequence: Sequence, 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 { @@ -36,15 +55,7 @@ struct TagsSequence: Sequence, IteratorProtocol { return nil } - 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 + func makeIterator() -> TagsIterator { + return .init(note: note) } } -