mirror of
https://github.com/zhigang1992/GitHawk.git
synced 2026-05-25 10:23:34 +08:00
33 lines
694 B
Swift
33 lines
694 B
Swift
//
|
|
// WidthCache.swift
|
|
// Freetime
|
|
//
|
|
// Created by Ryan Nystrom on 10/8/17.
|
|
// Copyright © 2017 Ryan Nystrom. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
final class WidthCache<KeyType: Hashable, DataType> {
|
|
|
|
// note: not thread safe
|
|
private var cache = [String: DataType]()
|
|
|
|
// MARK: Public API
|
|
|
|
func data(key: KeyType, width: CGFloat) -> DataType? {
|
|
return cache[mergedKey(key: key, width: width)]
|
|
}
|
|
|
|
func set(data: DataType, key: KeyType, width: CGFloat) {
|
|
cache[mergedKey(key: key, width: width)] = data
|
|
}
|
|
|
|
// MARK: Private API
|
|
|
|
func mergedKey(key: KeyType, width: CGFloat) -> String {
|
|
return "\(key.hashValue)\(width)"
|
|
}
|
|
|
|
}
|