अफसोस की बात है, ऐप्पल ने केवल बॉक्स के बाहर स्विफ्ट में तीन संग्रह डेटास्ट्रक्चर में बनाया है। ये ऐरे, शब्दकोश और सेट है। सौभाग्य से, ऐप्पल ने प्रोटोकॉल का एक बहुत ही व्यापक और शक्तिशाली पदानुक्रम पेश किया है जो आपके स्वयं के संग्रह वर्गों को आसानी से और सुंदर ढंग से परिभाषित करने का समर्थन करता है।
इसलिए, यदि आपको अपने समाधान की अतिरिक्त जगह- (और शायद समय-) जटिलता पर कोई फर्क नहीं पड़ता है, तो हो सकता है कि आप अपने स्वयं के स्विफ्ट संग्रह वर्ग/संरचना को चुनने का विकल्प चुन सकें जो उस क्रम को संरक्षित करता है जिसमें ऑर्डर को संरक्षित किया जाता है कुंजी को इंडेक्स से संबंधित और इसके विपरीत, तत्वों को इसमें जोड़ा गया था। अपना खुद का संग्रह डेटास्ट्रक्चर बनाने के बारे में अधिक जानकारी के लिए प्रलेखन देखें: https://developer.apple.com/documentation/swift/collection।
अस्वीकरण::
मैं तुम्हें एक छोटे से कुछ तुम जा पाने के लिए दे देंगे इस कोड का परीक्षण नहीं किया गया है, और मैं एल्गोरिथम जटिलता के मामलों पर विचार की एक महान सौदा खर्च नहीं किया है। कृपया अपने समाधान की आवश्यकताओं को निर्धारित करें और स्वयं को जांचें कि क्या निम्न कोड का पालन करता है।
public struct KeepOrderDictionary<Key, Value> where Key : Hashable
{
public private(set) var values: [Value]
fileprivate var keyToIndexMap: [Key:Int]
fileprivate var indexToKeyMap: [Int:Key]
public init()
{
self.values = [Value]()
self.keyToIndexMap = [Key:Int]()
self.indexToKeyMap = [Int:Key]()
}
public var count: Int
{ return values.count}
public mutating func add(key: Key, _ value: Value)
{
if let index = keyToIndexMap[key]
{ values[index] = value}
else
{
values.append(value)
keyToIndexMap[key] = values.count - 1
indexToKeyMap[values.count - 1] = key
}
}
public mutating func add(index: Int, _ value: Value) -> Bool
{
if let key = indexToKeyMap[index]
{
add(key: key, value)
return true
}
return false
}
public func get(key: Key) -> (Key, Value)?
{
if let index = keyToIndexMap[key]
{ return (key, values[index])}
return nil
}
public func get(index: Int) -> (Key, Value)?
{
if let key = indexToKeyMap[index]
{ return (key, values[index])}
return nil
}
public mutating func removeValue(forKey key: Key) -> Bool
{
guard let index = keyToIndexMap[key] else
{ return false}
values.remove(at: index)
keyToIndexMap.removeValue(forKey: key)
indexToKeyMap.removeValue(forKey: index)
return true
}
public mutating func removeValue(at index: Int) -> Bool
{
guard let key = indexToKeyMap[index] else
{ return false}
values.remove(at: index)
keyToIndexMap.removeValue(forKey: key)
indexToKeyMap.removeValue(forKey: index)
return true
}
}
extension KeepOrderDictionary
{
public subscript(key: Key) -> Value?
{
get
{ return get(key: key)?.1}
set
{
if let newValue = newValue
{ add(key: key, newValue)}
else
{ let _ = removeValue(forKey: key)}
}
}
public subscript(index: Int) -> Value?
{
get
{ return get(index: index)?.1}
set
{
if let newValue = newValue
{ let _ = add(index: index, newValue)}
}
}
}
extension KeepOrderDictionary : ExpressibleByDictionaryLiteral
{
public init(dictionaryLiteral elements: (Key, Value)...)
{
self.init()
for entry in elements
{ add(key: entry.0, entry.1)}
}
}
extension KeepOrderDictionary : Sequence
{
public typealias Iterator = IndexingIterator<[(key: Key, value: Value)]>
public func makeIterator() -> KeepOrderDictionary.Iterator
{
var content = [(key: Key, value: Value)]()
for i in 0 ..< count
{
if let value: Value = self[i], let key: Key = indexToKeyMap[i]
{ content.append((key: key, value: value))}
}
return content.makeIterator()
}
}
स्रोत
2017-06-06 14:40:04
[प्रलेखन] (https://developer.apple.com/library/ios/documentation/General/Reference/SwiftStandardLibraryReference/Dictionary.html) स्पष्ट रूप से: एक शब्दकोश एक सामान्य प्रकार है कि एक का प्रबंध करती है * * अनियंत्रित ** कुंजी-मूल्य जोड़े का संग्रह।- http://stackoverflow.com/questions/26546488/dictionary-printing-results-backwards भी देखें। –
आपको अपना खुद का प्रकार बनाना होगा जिसमें शब्दकोश और क्रम हो। ऐसा करने के लिए कोई प्रकार निर्मित नहीं है। – Kirsteins
बमर, यह एक बड़ी सीमा है। मैं कुंजी या मूल्यों से सॉर्ट नहीं करना चाहता, मैं बस उन्हें घोषित किए गए क्रम में दिखाना चाहता हूं। – TruMan1