Insert a sequential Dictionary (such as Java’s LinkedHashMap) in swift?

A standard swift class is a dictionary, but keep the type insertion order like @ L_ 301_ 1@? If not, how will it be implemented?

Solution

I don't know one. It's an interesting problem to solve (it has been put in my standard library). In most cases, it's just a problem of maintaining the keys of a dictionary and array side by side However, for (key, value) and OD The standard operation of keys in keys will be iterated in insertion order rather than semi - random

// 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
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>