1
0
mirror of git://jb55.com/damus synced 2024-09-16 02:03:45 +00:00

Revert "ndb: remove TagIterators and just use sequences"

This reverts commit f0d07c3663.
This commit is contained in:
William Casarin 2023-07-23 10:56:12 -07:00
parent f0d07c3663
commit dd65209a20
2 changed files with 56 additions and 15 deletions

View File

@ -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

View File

@ -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,17 +25,6 @@ struct TagsSequence: Sequence, IteratorProtocol {
return tag_seq
}
subscript(index: Int) -> Iterator.Element? {
var i = 0
for element in self {
if i == index {
return element
}
i += 1
}
return nil
}
var count: UInt16 {
return iter.tag.pointee.count
}
@ -48,3 +37,25 @@ struct TagsSequence: Sequence, IteratorProtocol {
}
}
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 {
if i == index {
return element
}
i += 1
}
return nil
}
func makeIterator() -> TagsIterator {
return .init(note: note)
}
}