-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [SS-004] HashMap초기 구현 * [SS-004] 정렬리스트 추출 구현 * [SS-004] HashMap 삽입삭제 구현 * [SS-004] testASCAndDESCList * [SS-004] ASC, DESC 최적화 리스트크기를 지정하여 최적화 * [SS-004] HashMap에 적용 * [SS-004] HashMap멀티쓰레드 환경 테스트 코드 추가 * [SS-004] Docs, HashMap Readme작성
- Loading branch information
Showing
10 changed files
with
551 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+7.01 KB
(110%)
...j/project.xcworkspace/xcuserdata/choejun-yeong.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// HashMap.swift | ||
// SwiftStructures | ||
// | ||
// Created by choijunios on 10/13/24. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Thread safe hash map | ||
public class HashMap<Key, Value> where Key: Hashable & Comparable { | ||
|
||
typealias KeyStore = RBTree<Key> | ||
|
||
private var dictionary: [Key: Value] = .init() | ||
private let keyStore: KeyStore = .init() | ||
|
||
private let lock: NSLock = .init() | ||
|
||
public subscript(key: Key) -> Value? { | ||
get { | ||
defer { | ||
lock.unlock() | ||
} | ||
lock.lock() | ||
|
||
return dictionary[key] | ||
} | ||
set(newValue) { | ||
defer { | ||
lock.unlock() | ||
} | ||
lock.lock() | ||
|
||
if dictionary[key] == nil { | ||
// key isn't exists in dictionary | ||
try! keyStore.append(key) | ||
} | ||
|
||
dictionary[key] = newValue | ||
} | ||
} | ||
|
||
public func remove(_ key: Key) { | ||
defer { | ||
lock.unlock() | ||
} | ||
lock.lock() | ||
|
||
do { | ||
try keyStore.remove(key) | ||
} catch { | ||
print("\(HashMap.self): " + error.localizedDescription) | ||
} | ||
|
||
dictionary.removeValue(forKey: key) | ||
} | ||
} | ||
|
||
|
||
// MARK: sorted value list | ||
public extension HashMap { | ||
|
||
func ascendingValues(_ count: Int) -> [Value] { | ||
defer { | ||
lock.unlock() | ||
} | ||
lock.lock() | ||
|
||
let pairCount = dictionary.count | ||
let keyCount = count > pairCount ? pairCount : count | ||
|
||
let keys = keyStore.sortedList(type: .ASC, count: keyCount) | ||
|
||
return keys.compactMap { key in | ||
dictionary[key] | ||
} | ||
} | ||
|
||
func descendingValues(_ count: Int) -> [Value] { | ||
defer { | ||
lock.unlock() | ||
} | ||
lock.lock() | ||
|
||
let pairCount = dictionary.count | ||
let keyCount = count > pairCount ? pairCount : count | ||
|
||
let keys = keyStore.sortedList(type: .DESC, count: keyCount) | ||
|
||
return keys.compactMap { key in | ||
dictionary[key] | ||
} | ||
} | ||
} |
Oops, something went wrong.