mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-24 02:11:12 +01:00
Optimize ListSnapshot collection implementation
This commit is contained in:
@@ -78,7 +78,7 @@ extension Internals {
|
||||
|
||||
var numberOfItems: Int {
|
||||
|
||||
return self.structure.allItemIDs.count
|
||||
return self.structure.allItemsCount
|
||||
}
|
||||
|
||||
var numberOfSections: Int {
|
||||
@@ -106,6 +106,59 @@ extension Internals {
|
||||
return self.itemIdentifiers(inSection: identifier).count
|
||||
}
|
||||
|
||||
func itemIdentifier(atAllItemsIndex index: Int) -> NSManagedObjectID? {
|
||||
|
||||
guard index >= 0 else {
|
||||
|
||||
return nil
|
||||
}
|
||||
var remainingIndex = index
|
||||
for section in self.structure.sections {
|
||||
|
||||
let elements = section.elements
|
||||
let sectionCount = elements.count
|
||||
if remainingIndex < sectionCount {
|
||||
|
||||
return elements[remainingIndex].differenceIdentifier
|
||||
}
|
||||
|
||||
remainingIndex -= sectionCount
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func itemIdentifiers(atAllItemsBounds bounds: Range<Int>) -> [NSManagedObjectID] {
|
||||
|
||||
var remainingIndex = bounds.lowerBound
|
||||
var itemIdentifiers: [NSManagedObjectID] = []
|
||||
for section in self.structure.sections {
|
||||
|
||||
let elements = section.elements
|
||||
let sectionCount = elements.count
|
||||
if remainingIndex < sectionCount {
|
||||
|
||||
itemIdentifiers.append(
|
||||
contentsOf: elements[remainingIndex..<min(sectionCount, bounds.count)]
|
||||
.map({ $0.differenceIdentifier })
|
||||
)
|
||||
}
|
||||
else if !itemIdentifiers.isEmpty {
|
||||
|
||||
itemIdentifiers.append(
|
||||
contentsOf: elements.prefix(bounds.count - itemIdentifiers.count)
|
||||
.map({ $0.differenceIdentifier })
|
||||
)
|
||||
}
|
||||
if itemIdentifiers.count >= bounds.count {
|
||||
|
||||
return itemIdentifiers
|
||||
}
|
||||
|
||||
remainingIndex -= sectionCount
|
||||
}
|
||||
return itemIdentifiers
|
||||
}
|
||||
|
||||
func itemIdentifiers(inSection identifier: String) -> [NSManagedObjectID] {
|
||||
|
||||
return self.structure.items(in: identifier)
|
||||
@@ -332,6 +385,14 @@ extension Internals {
|
||||
return self.sections.map({ $0.differenceIdentifier })
|
||||
}
|
||||
|
||||
var allItemsCount: Int {
|
||||
|
||||
return self.sections.reduce(into: 0) { (result, section) in
|
||||
|
||||
result += section.elements.count
|
||||
}
|
||||
}
|
||||
|
||||
var allItemIDs: [NSManagedObjectID] {
|
||||
|
||||
return self.sections.lazy.flatMap({ $0.elements }).map({ $0.differenceIdentifier })
|
||||
|
||||
Reference in New Issue
Block a user