Skip to content

Commit

Permalink
Structuring, bugs, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdremov committed May 20, 2021
1 parent 0b33838 commit 8596335
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 24 deletions.
11 changes: 5 additions & 6 deletions Sources/FastList/FastList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class FastList<T>: CustomStringConvertible, Collection, Sequence {
var indNow = storage[0].next;
var counter = 0;
while(indNow != 0 && counter < size) {
out += "\(String(describing: storage[Int(indNow)].value))"
out += "\(String(describing: storage[Int(indNow)].value!))"
if (counter != size - 1){
out += ", "
}
Expand Down Expand Up @@ -156,15 +156,11 @@ public class FastList<T>: CustomStringConvertible, Collection, Sequence {
return try insertAfter(pos: afterPos, value: value)
}

@discardableResult public func pushBack(value: T) -> UInt {
return try! insertAfter(pos: storage[0].prev, value: value);
}

@discardableResult public func append(value: T) -> UInt {
return try! insertAfter(pos: storage[0].prev, value: value);
}

@discardableResult public func pushFront(value: T) -> UInt {
@discardableResult public func appendFront(value: T) -> UInt {
optimized = false
return try! insertAfter(pos: 0, value: value);
}
Expand Down Expand Up @@ -295,6 +291,9 @@ public class FastList<T>: CustomStringConvertible, Collection, Sequence {
}

public func optimize() {
if (optimized){
return
}
let newStorage = UnsafeMutablePointer<ListNode>.allocate(capacity: Int(capacity))
newStorage.initialize(repeating: ListNode(), count: Int(capacity))

Expand Down
74 changes: 56 additions & 18 deletions Tests/FastListTests/FastListTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
let list = FastList<Int>(capacity: 0)

for i in 0...1024 {
list.pushBack(value: i)
list.append(value: i)
XCTAssertEqual(list.isOptimized(), true)
}

Expand All @@ -42,7 +42,7 @@
let list = FastList<Int>(capacity: 0)

for i in 0...1024 {
list.pushFront(value: i)
list.appendFront(value: i)
}
XCTAssertEqual(list.isOptimized(), false)

Expand All @@ -57,7 +57,7 @@
var savedInd:[(UInt, Int)] = []

for i in 0...1024 {
savedInd.append((list.pushBack(value: i), i))
savedInd.append((list.append(value: i), i))
}

for i in 0..<savedInd.count {
Expand All @@ -71,7 +71,7 @@
var savedInd:[(UInt, Int)] = []

for i in 0...1024 {
savedInd.append((list.pushFront(value: i), i))
savedInd.append((list.appendFront(value: i), i))
}

for i in 0..<savedInd.count {
Expand All @@ -83,10 +83,10 @@
var list:FastList? = FastList<Leaker>(capacity: 0)

for _ in 0...1024 {
list!.pushBack(value: Leaker())
list!.append(value: Leaker())
}
for _ in 0...1024 {
list!.pushFront(value: Leaker())
list!.appendFront(value: Leaker())
}

list = nil
Expand All @@ -99,11 +99,11 @@


for _ in 0...1024 {
list!.pushBack(value: Leaker())
list!.append(value: Leaker())
}

for _ in 0...1024 {
list!.pushFront(value: Leaker())
list!.appendFront(value: Leaker())
}

try! list!.remove(physic: 2)
Expand All @@ -119,11 +119,11 @@


for _ in 0...1024 {
list!.pushBack(value: Leaker())
list!.append(value: Leaker())
}

for _ in 0...1024 {
list!.pushFront(value: Leaker())
list!.appendFront(value: Leaker())
}

list = nil
Expand All @@ -136,19 +136,19 @@


for _ in 0...1024 {
list!.pushBack(value: Leaker())
list!.append(value: Leaker())
}


for _ in 0...1024 {
list!.pushFront(value: Leaker())
list!.appendFront(value: Leaker())
}

try! list!.remove(physic: 2)
try! list!.remove(physic: 20)

for _ in 0...1024 {
list!.pushFront(value: Leaker())
list!.appendFront(value: Leaker())
}
list = nil
XCTAssertEqual(Leaker.counter, 0)
Expand Down Expand Up @@ -189,7 +189,7 @@
let list = FastList<Int>()

for i in 0...testSize {
list.pushBack(value: i)
list.append(value: i)
}

XCTAssertEqual(list.isOptimized(), true)
Expand Down Expand Up @@ -261,7 +261,7 @@
list.reserveCapacity(testSize)

for i in 0...testSize {
list.pushFront(value: i)
list.appendFront(value: i)
}
}

Expand Down Expand Up @@ -291,7 +291,7 @@
let list = FastList<Int>()

for i in 0...testSize {
list.pushBack(value: i)
list.append(value: i)
}

var count = 0
Expand All @@ -315,7 +315,7 @@
let list = FastList<Int>()

for i in 0...testSize {
list.pushFront(value: i)
list.appendFront(value: i)
}

XCTAssertFalse(list.isOptimized())
Expand All @@ -341,7 +341,7 @@
let list = FastList<Int>()

for i in 0...testSize {
list.pushBack(value: i)
list.append(value: i)
}

XCTAssertTrue(list.isOptimized())
Expand Down Expand Up @@ -378,4 +378,42 @@
XCTAssertEqual(list[i], i + add)
}
}

func testExample() {
let list = FastList<Int>()

for i in 0...5 {
list.append(value: i * i)
}

let hundredId = list.append(value: 100) // access id to 100

try! list.set(physic: hundredId, value: 101) // O(1) always

for i in 0...5 {
list.appendFront(value: i * i * i) // breaks optimization
}

print(list)
for i in list {
print(i!)
}

for i in 0..<list.count {
print(list[i]!) // O(n) subscript
}

list.optimize()
print(list) // the same list, optimized representation

for i in 0..<list.count {
print(list[i]!) // O(1) subscript
}

while(!list.isEmpty) {
try! list.remove(logic: 0)
}

print(list)
}
}

0 comments on commit 8596335

Please sign in to comment.