Skip to content

Commit

Permalink
Reassign indexes after deleting element
Browse files Browse the repository at this point in the history
  • Loading branch information
sendyhalim committed Jan 9, 2018
1 parent bbcbc57 commit 04502dc
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Sources/OrderedSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,15 @@ public struct OrderedSet<T: Hashable>: ExpressibleByArrayLiteral {
mutating private func remove(element: T, index: Int) -> T {
indexByElement[element] = nil

return elements.remove(at: index)
let removedElement: T = elements.remove(at: index)

// Now we need to update indexByElement because if we're deleting in the middle
// then all the elements after the deleted element will be shifted to the left
for i in index..<elements.count {
indexByElement[elements[i]] = i
}

return removedElement
}
}

Expand Down

0 comments on commit 04502dc

Please sign in to comment.