Skip to content

Commit

Permalink
Fix capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
DandyLyons committed Sep 17, 2024
1 parent 6aa864b commit 21508a5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

extension POCOrderedSetImplementation {
#if DEBUG
@usableFromInline @inline(never)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import OrderedCollections

public struct POCOrderedSetImplementation<Left: Hashable, Right: Hashable> {
Expand All @@ -16,10 +15,12 @@ public struct POCOrderedSetImplementation<Left: Hashable, Right: Hashable> {
_rtl.firstIndex(of: rightValue)
}

@available(*, deprecated)
@inlinable public var capacity: Int {
print("OrderedSet does not have built in capacity variable")
return 0
/// A view of the capacity of the ordered view
///
/// `POCOrderedSetImplementation` is backed by two `OrderedSet`, which does not have a capacity property.
/// However it does have an `elements` array. `capacityView` returns the `capactity` of the `elements` array.
@inlinable public var capacityView: Int {
return _ltr.elements.capacity
}

@inlinable public mutating func reserveCapacity(_ minimumCapacity: Int) {
Expand Down Expand Up @@ -102,7 +103,9 @@ public struct POCOrderedSetImplementation<Left: Hashable, Right: Hashable> {

guard leftInserted == true, rightInserted == true,
atLeftIndex == atRightIndex else {
fatalError("error occured while inserting right value: \(newRightValue) by left: \(leftValue) through subscript")
fatalError("""
error occured while inserting right value: \(newRightValue) by left: \(leftValue) through subscript
""")
}
return
}
Expand Down Expand Up @@ -145,7 +148,9 @@ public struct POCOrderedSetImplementation<Left: Hashable, Right: Hashable> {

guard leftInserted == true, rightInserted == true,
atLeftIndex == atRightIndex else {
fatalError("error occured while inserting left value: \(newLeftValue) by right: \(rightValue) through subscript")
fatalError("""
error occured while inserting left value: \(newLeftValue) by right: \(rightValue) through subscript
""")
}
return
}
Expand Down Expand Up @@ -226,14 +231,15 @@ extension OrderedSet {
let index = self.firstIndex(of: oldValue)
return (replaced: false, index: index ?? -1)
}
guard let index = self.firstIndex(of: oldValue) else {
// self does not contain old value (old value is not present in self)

if self.contains(oldValue) {
// remove old value
// append new value
return (replaced: true, index: -1)
} else {
return (replaced: false, index: -1)

}

// remove old value
// append new value
return (replaced: true, index: -1)
}
}

5 changes: 3 additions & 2 deletions Tests/BijectiveDictionaryTests/BijectiveDictionaryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import Testing
#expect(fromInit.count == 0)
}

@Test func createWithCapacity() {
let dict = BijectiveDictionary<String, Int>(minimumCapacity: 10)
@Test(arguments: [10, 100, 1000, 10_000, 100_000, 1_000_000])
func createWithCapacity(minimumCapacity: Int) {
let dict = BijectiveDictionary<String, Int>(minimumCapacity: minimumCapacity)
#expect(dict.capacity >= 10)
}

Expand Down
9 changes: 4 additions & 5 deletions Tests/BijectiveDictionaryTests/POCTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ import Testing
#expect(fromInit.count == 0)
}

@Test func _createWithCapacity() {
let dict = POCOrderedSetImplementation<String, Int>(minimumCapacity: 10)
@Test(arguments: [10, 100, 1000, 10_000, 100_000, 1_000_000])
func _createWithCapacity(minimumCapacity: Int) {
let dict = POCOrderedSetImplementation<String, Int>(minimumCapacity: minimumCapacity)

withKnownIssue {
#expect(dict.capacity >= 10) // OrderedSet doesn't have a built-in capacity property
}
#expect(dict.capacityView >= 10)
}

@Test func _fromUniqueLeftRightPairs() {
Expand Down

0 comments on commit 21508a5

Please sign in to comment.