Writing for OS/X or the iPhone? Using NSDictionary to store and access key-value pairs? Ever tried to enumerate them all only to find they come back in some random order? As NSDictionary uses a hash table to map keys to values, it maintains no sort order whatsoever across its entries. The SortedDictionary class and its mutable companion, MutableSortedDictionary implement dictionaries that maintain a strong sort order across their keys. SortedDictionary and MutableSortedDictionary deliver high performance using an implementation of a self-balancing binary tree.
The SortedDictionary and MutableSortedDictionary classes implement the interfaces of NSDictionary and NSMutableDictionary, respectively, and can serve as drop-in replacements. They support the NSCoding, NSCopying and NSMutableCopying protocols. Additionally, the sorted dictionary classes add methods allowing you to enumerate entries in both their forward and reverse key order, without having to sort and without any additional computational cost.
Add the SortedDictionary folder to your project. Add all the files in the SortedDictionary folder, including its Internal and External folders, into your project. When you build your project, the sorted dictionaries classes will be compiled into your application. To create a sorted dictionary in code, include the appropriate header file:
#import "SortedDictionary.h"
- (void) someMethod {
SortedDictionary *dict = [MutableSortedDictionary dictionary];
[dict setValue: @"red" forKey: @"apple"];
[dict setValue: @"yellow" forKey: @"banana"];
[dict setValue: @"orange" forKey: @"orange"];
NSString *color = [dict objectForKey: @"apple"];
}
Add the compiled frameworks to your project:
In XCode, open Targets, then right-click (or ctrl-click) your target, and select Add>Existing Frameworks...
The Target Info window opens. Click the + button below the Linked Libraries list. Select Add Other...
MIT License
Copyright (c) 2009 Oren Trutner
Modified by Yuriy Shevchuk, struktur AG, on 8/30/2013.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.