插入顺序字典(如Java的LinkedHashMap)在Swift?

前端之家收集整理的这篇文章主要介绍了插入顺序字典(如Java的LinkedHashMap)在Swift?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有一个标准的 swift类是一个Dictionary,但是保持键入插入顺序像 Java’s LinkedHashMap?如果没有,将如何实施?

解决方法

不知道一个,这是一个有趣的问题要解决(已经把它放在我的标准库中)大多数情况下,这只是一个维护一个字典和数组的键并排的问题.但是,对于od中的(key,value)和od.keys中的键的标准操作将以插入顺序而不是半随机的方式进行迭代.
// OrderedDictionary behaves like a Dictionary except that it maintains
//  the insertion order of the keys,so iteration order matches insertion
//  order.
struct OrderedDictionary<KeyType:Hashable,ValueType> {
    private var _dictionary:Dictionary<KeyType,ValueType>
    private var _keys:Array<KeyType>

    init() {
        _dictionary = [:]
        _keys = []
    }

    init(minimumCapacity:Int) {
        _dictionary = Dictionary<KeyType,ValueType>(minimumCapacity:minimumCapacity)
        _keys = Array<KeyType>()
    }

    init(_ dictionary:Dictionary<KeyType,ValueType>) {
        _dictionary = dictionary
        _keys = map(dictionary.keys) { $0 }
    }

    subscript(key:KeyType) -> ValueType? {
        get {
            return _dictionary[key]
        }
        set {
            if newValue == nil {
                self.removeValueForKey(key)
            }
            else {
                self.updateValue(newValue!,forKey: key)
            }
        }
    }

    mutating func updateValue(value:ValueType,forKey key:KeyType) -> ValueType? {
        let oldValue = _dictionary.updateValue(value,forKey: key)
        if oldValue == nil {
            _keys.append(key)
        }
        return oldValue
    }

    mutating func removeValueForKey(key:KeyType) {
        _keys = _keys.filter { $0 != key }
        _dictionary.removeValueForKey(key)
    }

    mutating func removeAll(keepCapacity:Int) {
        _keys = []
        _dictionary = Dictionary<KeyType,ValueType>(minimumCapacity: keepCapacity)
    }

    var count: Int { get { return _dictionary.count } }

    // keys isn't lazy evaluated because it's just an array anyway
    var keys:[KeyType] { get { return _keys } }

    // values is lazy evaluated because of the dictionary lookup and creating a new array
    var values:GeneratorOf<ValueType> {
        get {
            var index = 0
            return GeneratorOf<ValueType> {
                if index >= self._keys.count {
                    return nil
                }
                else {
                    let key = self._keys[index]
                    index++
                    return self._dictionary[key]
                }
            }
        }
    }
}

extension OrderedDictionary : SequenceType {
    func generate() -> GeneratorOf<(KeyType,ValueType)> {
        var index = 0
        return GeneratorOf<(KeyType,ValueType)> {
            if index >= self._keys.count {
                return nil
            }
            else {
                let key = self._keys[index]
                index++
                return (key,self._dictionary[key]!)
            }
        }
    }
}

func ==<Key: Equatable,Value: Equatable>(lhs: OrderedDictionary<Key,Value>,rhs: OrderedDictionary<Key,Value>) -> Bool {
    return lhs._keys == rhs._keys && lhs._dictionary == rhs._dictionary
}

func !=<Key: Equatable,Value>) -> Bool {
    return lhs._keys != rhs._keys || lhs._dictionary != rhs._dictionary
}
原文链接:/java/121348.html

猜你在找的Java相关文章