mirror of
git://jb55.com/damus
synced 2024-09-18 19:23:49 +00:00
ndb: remove TagIterators and just use sequences
Still learning...
This commit is contained in:
parent
b3119fa41e
commit
f0d07c3663
@ -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
|
||||
|
@ -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,6 +25,17 @@ struct TagsIterator: 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
|
||||
}
|
||||
@ -37,25 +48,3 @@ struct TagsIterator: 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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user