Compare commits

...

32 Commits

Author SHA1 Message Date
Lukas Kubanek 4065061d25 Bumped version 2016-01-06 23:51:04 +01:00
Marius Bergmann 60ffb98742 Set iOS deployment target to 8.0
All tests also pass on 8.0. This makes it possible to use the project in
an 8.x application.
2016-01-05 16:56:09 +01:00
Lukas Kubanek d70db906df Fix in the README 2015-12-18 10:19:39 +01:00
Lukas Kubanek 30bb351de2 Updated to Xcode 7.2 2015-12-15 12:20:05 +01:00
Lukas Kubanek 0022833380 Fixed a typo 2015-12-10 10:11:04 +01:00
Lukas Kubanek 1219d6dd8a Updated README and playground 2015-12-06 14:48:50 +01:00
Lukas Kubanek c5e6b63a9c Added support for sorting
Closes #5
2015-12-06 14:30:23 +01:00
Lukas Kubanek a2d0ef2c1b Cleaned up tests 2015-12-06 14:16:42 +01:00
Lukas Kubanek 61729a772b Updated framework search headers 2015-12-06 13:56:39 +01:00
Lukas Kubanek 5e51f91e07 The backing store properties are now initialized in-place 2015-12-04 13:58:55 +01:00
Lukas Kubanek 5acd33d10a Removed the dependency on Nimble for tests 2015-11-27 15:13:04 +01:00
Lukas Kubanek 1a4623ceff And hopefully the last change 2015-11-15 18:45:01 +01:00
Lukas Kubanek 8016abd95b Synxed with an older version of synx and fixed Carthage 2015-11-15 17:52:18 +01:00
Lukas Kubanek e58469f8e4 Synxed the project structure and updated dependencies
- Added Carthage to .gitignore
- Updated Nimble to v3.0
- Added a script for updating the dependencies
2015-11-15 17:01:36 +01:00
Lukas Kubanek 100e077a0d Added a convenience method for inserting new element 2015-11-04 13:51:17 +01:00
Lukas Kubanek 72ccef23af Updated installation section in README 2015-10-21 11:50:19 +02:00
Lukas Kubanek 1a9ac85931 Added properties orderedKeys & orderedValues
Closes #1
2015-10-14 20:48:41 +02:00
Lukas Kubanek 9074b8abe8 Updated readme 2015-09-11 10:24:58 +02:00
Lukas Kubanek 7f58e9ae8d Minor refactoring 2015-09-11 10:23:01 +02:00
Lukas Kubanek feffcd9041 Improved fatal error message for insertion using negative index 2015-09-11 10:22:53 +02:00
Lukas Kubanek e963b30ff8 Added support for index-based insertion 2015-09-11 10:17:28 +02:00
Lukas Kubanek 23725866ab Improved fatal error messages 2015-09-11 09:35:42 +02:00
Lukas Kubanek 9aa12bb2aa Updated readme 2015-09-10 21:58:39 +02:00
Lukas Kubanek 4bcf559767 Updated the Swift/Xcode requirement 2015-09-10 19:16:48 +02:00
Lukas Kubanek c318c55cab Added support for == operator and initialization from pairs 2015-09-10 19:10:39 +02:00
Lukas Kubanek b00cb645f5 Ignored own builds 2015-09-10 19:10:13 +02:00
Lukas Kubanek c2bfb83d40 Typo 2015-08-30 18:02:26 +02:00
Lukas Kubanek 9c2c3f0c21 Added method containsKey(:) 2015-08-30 12:50:52 +02:00
Lukas Kubanek e8240aef93 Added Travis badge 2015-08-30 11:53:28 +02:00
Lukas Kubanek 049f05acae Put Nimble builds into the repository 2015-08-30 11:45:57 +02:00
Lukas Kubanek 7ad09edbc0 Added Travis CI configuration 2015-08-30 11:37:04 +02:00
Lukas Kubanek 39f90292f6 Typo 2015-08-30 11:21:30 +02:00
12 changed files with 468 additions and 242 deletions
+13
View File
@@ -0,0 +1,13 @@
language: objective-c
branches:
only:
- master
xcode_project: OrderedDictionary.xcodeproj
xcode_scheme: OrderedDictionary-iOS
osx_image: xcode7
xcode_sdk: iphonesimulator
script:
- xcodebuild clean build test -project OrderedDictionary.xcodeproj -scheme OrderedDictionary-iOS -destination "platform=iOS Simulator,name=iPhone 6,OS=9.0"
-1
View File
@@ -1 +0,0 @@
github "Quick/Nimble" "e84623d953d811f74a2afcda4fa0c8121e7dad7d"
-1
View File
@@ -1 +0,0 @@
github "Quick/Nimble" "e84623d953d811f74a2afcda4fa0c8121e7dad7d"
+23 -69
View File
@@ -8,17 +8,13 @@
/* Begin PBXBuildFile section */
803F9CD51B92324C002F728F /* OrderedDictionary.h in Headers */ = {isa = PBXBuildFile; fileRef = 803F9CD21B92324C002F728F /* OrderedDictionary.h */; settings = {ATTRIBUTES = (Public, ); }; };
803F9CD81B9232EE002F728F /* OrderedDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 803F9CD71B9232EE002F728F /* OrderedDictionary.swift */; settings = {ASSET_TAGS = (); }; };
803F9CF41B925B1C002F728F /* OrderedDictionary.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 803F9CC61B923207002F728F /* OrderedDictionary.framework */; settings = {ASSET_TAGS = (); }; };
803F9CFD1B925B4C002F728F /* OrderedDictionaryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 803F9CFC1B925B4C002F728F /* OrderedDictionaryTests.swift */; settings = {ASSET_TAGS = (); }; };
803F9D001B925BBF002F728F /* Nimble.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 803F9CE91B925AE5002F728F /* Nimble.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
803F9D011B925BE9002F728F /* Nimble.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 803F9CE91B925AE5002F728F /* Nimble.framework */; };
803F9CD81B9232EE002F728F /* OrderedDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 803F9CD71B9232EE002F728F /* OrderedDictionary.swift */; };
803F9CF41B925B1C002F728F /* OrderedDictionary.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 803F9CC61B923207002F728F /* OrderedDictionary.framework */; };
803F9CFD1B925B4C002F728F /* OrderedDictionaryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 803F9CFC1B925B4C002F728F /* OrderedDictionaryTests.swift */; };
8060976B1B92F2D200DB20CF /* OrderedDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 803F9CD71B9232EE002F728F /* OrderedDictionary.swift */; };
8060976C1B92F2D900DB20CF /* OrderedDictionary.h in Headers */ = {isa = PBXBuildFile; fileRef = 803F9CD21B92324C002F728F /* OrderedDictionary.h */; settings = {ATTRIBUTES = (Public, ); }; };
806097761B92F44F00DB20CF /* OrderedDictionary.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 806097631B92F20400DB20CF /* OrderedDictionary.framework */; settings = {ASSET_TAGS = (); }; };
806097761B92F44F00DB20CF /* OrderedDictionary.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 806097631B92F20400DB20CF /* OrderedDictionary.framework */; };
8060977C1B92F4A000DB20CF /* OrderedDictionaryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 803F9CFC1B925B4C002F728F /* OrderedDictionaryTests.swift */; };
806097801B92F4F000DB20CF /* Nimble.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8060977F1B92F4F000DB20CF /* Nimble.framework */; settings = {ASSET_TAGS = (); }; };
806097821B92F4FC00DB20CF /* Nimble.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 8060977F1B92F4F000DB20CF /* Nimble.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -45,7 +41,6 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
803F9D001B925BBF002F728F /* Nimble.framework in CopyFiles */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -55,7 +50,6 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
806097821B92F4FC00DB20CF /* Nimble.framework in CopyFiles */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -66,13 +60,11 @@
803F9CD21B92324C002F728F /* OrderedDictionary.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OrderedDictionary.h; sourceTree = "<group>"; };
803F9CD41B92324C002F728F /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
803F9CD71B9232EE002F728F /* OrderedDictionary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OrderedDictionary.swift; sourceTree = "<group>"; };
803F9CE91B925AE5002F728F /* Nimble.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Nimble.framework; path = Carthage/Build/Mac/Nimble.framework; sourceTree = "<group>"; };
803F9CEF1B925B1C002F728F /* OrderedDictionary-OSX-Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "OrderedDictionary-OSX-Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
803F9CFC1B925B4C002F728F /* OrderedDictionaryTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OrderedDictionaryTests.swift; sourceTree = "<group>"; };
803F9CFE1B925B56002F728F /* Info-Tests.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = "Info-Tests.plist"; path = "Supporting Files/Info-Tests.plist"; sourceTree = SOURCE_ROOT; };
803F9CFE1B925B56002F728F /* Info-Tests.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Info-Tests.plist"; sourceTree = "<group>"; };
806097631B92F20400DB20CF /* OrderedDictionary.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = OrderedDictionary.framework; sourceTree = BUILT_PRODUCTS_DIR; };
806097711B92F44F00DB20CF /* OrderedDictionary-iOS-Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "OrderedDictionary-iOS-Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
8060977F1B92F4F000DB20CF /* Nimble.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Nimble.framework; path = Carthage/Build/iOS/Nimble.framework; sourceTree = "<group>"; };
80F94F181B926A60000A96E7 /* OrderedDictionary.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = OrderedDictionary.playground; sourceTree = "<group>"; };
/* End PBXFileReference section */
@@ -89,7 +81,6 @@
buildActionMask = 2147483647;
files = (
803F9CF41B925B1C002F728F /* OrderedDictionary.framework in Frameworks */,
803F9D011B925BE9002F728F /* Nimble.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -105,7 +96,6 @@
buildActionMask = 2147483647;
files = (
806097761B92F44F00DB20CF /* OrderedDictionary.framework in Frameworks */,
806097801B92F4F000DB20CF /* Nimble.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -116,10 +106,9 @@
isa = PBXGroup;
children = (
803F9CD11B92324C002F728F /* Sources */,
80F94F171B926A60000A96E7 /* Playgrounds */,
803F9CFB1B925B4C002F728F /* Tests */,
80F94F171B926A60000A96E7 /* Playgrounds */,
803F9CD31B92324C002F728F /* Supporting Files */,
803F9CE81B9259F3002F728F /* Frameworks */,
803F9CC71B923207002F728F /* Products */,
);
sourceTree = "<group>";
@@ -147,21 +136,12 @@
803F9CD31B92324C002F728F /* Supporting Files */ = {
isa = PBXGroup;
children = (
803F9CD41B92324C002F728F /* Info.plist */,
803F9CFE1B925B56002F728F /* Info-Tests.plist */,
803F9CD41B92324C002F728F /* Info.plist */,
);
path = "Supporting Files";
sourceTree = "<group>";
};
803F9CE81B9259F3002F728F /* Frameworks */ = {
isa = PBXGroup;
children = (
8060977D1B92F4D900DB20CF /* OSX */,
8060977E1B92F4DF00DB20CF /* iOS */,
);
name = Frameworks;
sourceTree = "<group>";
};
803F9CFB1B925B4C002F728F /* Tests */ = {
isa = PBXGroup;
children = (
@@ -170,22 +150,6 @@
path = Tests;
sourceTree = "<group>";
};
8060977D1B92F4D900DB20CF /* OSX */ = {
isa = PBXGroup;
children = (
803F9CE91B925AE5002F728F /* Nimble.framework */,
);
name = OSX;
sourceTree = "<group>";
};
8060977E1B92F4DF00DB20CF /* iOS */ = {
isa = PBXGroup;
children = (
8060977F1B92F4F000DB20CF /* Nimble.framework */,
);
name = iOS;
sourceTree = "<group>";
};
80F94F171B926A60000A96E7 /* Playgrounds */ = {
isa = PBXGroup;
children = (
@@ -450,6 +414,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
@@ -490,6 +455,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
@@ -507,14 +473,12 @@
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Carthage/Build/Mac",
);
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
FRAMEWORK_VERSION = A;
INFOPLIST_FILE = "Supporting Files/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.10;
PRODUCT_BUNDLE_IDENTIFIER = com.lukaskubanek.OrderedDictionary;
PRODUCT_NAME = OrderedDictionary;
SKIP_INSTALL = YES;
@@ -531,14 +495,12 @@
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Carthage/Build/Mac",
);
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
FRAMEWORK_VERSION = A;
INFOPLIST_FILE = "Supporting Files/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.10;
PRODUCT_BUNDLE_IDENTIFIER = com.lukaskubanek.OrderedDictionary;
PRODUCT_NAME = OrderedDictionary;
SKIP_INSTALL = YES;
@@ -549,10 +511,7 @@
isa = XCBuildConfiguration;
buildSettings = {
COMBINE_HIDPI_IMAGES = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Carthage/Build/Mac",
);
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = "Supporting Files/Info-Tests.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.lukaskubanek.OrderedDictionaryTests;
@@ -564,10 +523,7 @@
isa = XCBuildConfiguration;
buildSettings = {
COMBINE_HIDPI_IMAGES = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Carthage/Build/Mac",
);
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = "Supporting Files/Info-Tests.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.lukaskubanek.OrderedDictionaryTests;
@@ -578,13 +534,14 @@
806097691B92F20400DB20CF /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_IDENTITY = "iPhone Developer";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = "Supporting Files/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.lukaskubanek.OrderedDictionary;
PRODUCT_NAME = OrderedDictionary;
@@ -597,13 +554,14 @@
8060976A1B92F20400DB20CF /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_IDENTITY = "iPhone Developer";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = "Supporting Files/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.lukaskubanek.OrderedDictionary;
PRODUCT_NAME = OrderedDictionary;
@@ -617,10 +575,7 @@
8060977A1B92F44F00DB20CF /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Carthage/Build/iOS",
);
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = "Supporting Files/Info-Tests.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
@@ -633,10 +588,7 @@
8060977B1B92F44F00DB20CF /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Carthage/Build/iOS",
);
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = "Supporting Files/Info-Tests.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
@@ -684,6 +636,7 @@
8060976A1B92F20400DB20CF /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
806097791B92F44F00DB20CF /* Build configuration list for PBXNativeTarget "OrderedDictionary-iOS-Tests" */ = {
isa = XCConfigurationList;
@@ -692,6 +645,7 @@
8060977B1B92F44F00DB20CF /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0700"
LastUpgradeVersion = "0720"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0700"
LastUpgradeVersion = "0720"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
@@ -63,3 +63,25 @@ print(orderedDictionary.indexForKey("F")) // => Optional(1)
print(orderedDictionary.indexForKey("E")) // => Optional(2)
print(orderedDictionary.indexForKey("A")) // => nil
print(orderedDictionary.indexForKey("C")) // => nil
////
// Sorting
////
var sortingOrderedDictionary: OrderedDictionary<String, Int> = [
("E", 4),
("G", 3),
("A", 3),
("D", 1),
("B", 4)
]
sortingOrderedDictionary.sortInPlace { (item1, item2) in
if item1.1 == item2.1 {
return item1.0 < item2.0
} else {
return item1.1 < item2.1
}
}
print(sortingOrderedDictionary) // => [D: 1, A: 3, G: 3, B: 4, E: 4]
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='osx' requires-full-environment='true'>
<playground version='5.0' target-platform='osx'>
<timeline fileName='timeline.xctimeline'/>
</playground>
+68 -40
View File
@@ -1,40 +1,39 @@
# OrderedDictionary
[![][image-1]][1] [![][image-2]][2] ![][image-3] [![][image-4]][3] [![][image-5]][4]
[![][image-1]][1] [![][image-2]][2] [![][image-3]][3] ![][image-4] [![][image-5]][4] [![][image-6]][5]
This is a lightweight implementation of an ordered dictionary data structure in Swift packed into a µframework.
`OrderedDictioary` is an immutable generic collection which combines the features of `Dictionary` and `Array`. Like `Dictionary` it stores key-value pairs and maps the keys to values. Additionally these pairs are sorted by zero-based integer index like in `Array`. The `OrderedDictionary` provides similar APIs to the collections from the Swift standard library and allows accessing the content by keys or indexes.
`OrderedDictionary` is an immutable generic collection which combines the features of `Dictionary` and `Array`. Like `Dictionary` it stores key-value pairs and maps the keys to values. Additionally these pairs are sorted by zero-based integer index like in `Array`. The `OrderedDictionary` provides similar APIs to the collections from the Swift standard library, allows accessing the content by keys or indexes and sorting.
Internally `OrderedDictionary` uses a backing store composed of an instance of `Dictionary` for storing the key-value pairs and an instance of `Array` for managing the ordered keys.
## Requirements
- Swift 2.0
- Xcode 7.0 beta 6+
- Swift 2.0+
- Xcode 7.0+
- iOS 8.0+ / OS X 10.10+
## Installation
The easiest way to integrate `OrderedDictionary` to your project is to use [Carthage][5]. Add following line to your `Cartfile`:
### Carthage
```
github "lukaskubanek/OrderedDictionary" ~> 0.1
```
The easiest way to integrate this framework in your project is to use [Carthage](https://github.com/Carthage/Carthage/).
Then drag the `OrderedDictionary.xcodeproj` or the `OrderedDictionary.framework` into your project/workspace and link against the `OrderedDictionary.framework`.
1. Add `github "lukaskubanek/OrderedDictionary" ~> 0.5` to your `Cartfile`.
2. Run `carthage bootstrap`.
3. Drag either the `OrderedDictionary.xcodeproj` or the `OrderedDictionary.framework` into your project/workspace and link your target against the `OrderedDictionary.framework`.
4. Make sure the framework [gets copied](https://github.com/Carthage/Carthage#adding-frameworks-to-an-application) to your application bundle.
Finally make sure the framework gets copied into your application bundle.
### Submodules
Another option for integrating this framework is to use [Git submodules](http://git-scm.com/book/en/v2/Git-Tools-Submodules).
## Example Usage
Initialization:
```swift
import OrderedDictionary
////
// Initialize an ordered dictionary using an array of key-value pairs
////
var orderedDictionary: OrderedDictionary<String, Int> = [
("A", 1),
("B", 2),
@@ -43,11 +42,11 @@ var orderedDictionary: OrderedDictionary<String, Int> = [
]
print(orderedDictionary) // => [A: 1, B: 2, C: 3, D: 4]
```
////
// Loop through the contents
////
Looping through the contents:
```swift
for (index, (key, value)) in orderedDictionary.enumerate() {
print("\(index): (\(key): \(value))")
}
@@ -56,11 +55,11 @@ for (index, (key, value)) in orderedDictionary.enumerate() {
// => 1: (B: 2)
// => 2: (C: 3)
// => 3: (D: 4)
```
////
// Modify the values by setting the value for key
////
Modifying the values by setting the value for key:
```swift
orderedDictionary["A"] = 100
orderedDictionary["E"] = 5
orderedDictionary["B"] = nil
@@ -73,11 +72,11 @@ print(orderedDictionary["B"]) // => nil
print(orderedDictionary["C"]) // => Optional(3)
print(orderedDictionary["D"]) // => Optional(42)
print(orderedDictionary["E"]) // => Optional(5)
```
////
// Modify the values by setting the element for index
////
Modifying the values by setting the element for index:
```swift
orderedDictionary[2] = ("F", 235)
orderedDictionary.updateElement(("K", 12), atIndex: 1)
orderedDictionary.removeAtIndex(0)
@@ -96,25 +95,54 @@ print(orderedDictionary.indexForKey("A")) // => nil
print(orderedDictionary.indexForKey("C")) // => nil
```
Sorting:
```swift
var sortingOrderedDictionary: OrderedDictionary<String, Int> = [
("E", 4),
("G", 3),
("A", 3),
("D", 1),
("B", 4)
]
sortingOrderedDictionary.sortInPlace { (item1, item2) in
if item1.1 == item2.1 {
return item1.0 < item2.0
} else {
return item1.1 < item2.1
}
}
print(sortingOrderedDictionary) // => [D: 1, A: 3, G: 3, B: 4, E: 4]
```
## Author
Lukas Kubanek // [lukaskubanek.com][6] // [@kubanekl][7]
Lukas Kubanek // [lukaskubanek.com][11] // [@kubanekl][12]
## License
OrderedDictionary is release under the [MIT License][8].
`OrderedDictionary` is released under the [MIT License][13].
[1]: https://github.com/lukaskubanek/OrderedDictionary/releases
[2]: https://developer.apple.com/swift/
[3]: https://github.com/Carthage/Carthage
[4]: LICENSE.md
[5]: https://github.com/Carthage/Carthage/
[6]: http://lukaskubanek.com
[7]: https://twitter.com/kubanekl
[8]: LICENSE.md
[1]: https://travis-ci.org/lukaskubanek/OrderedDictionary
[2]: https://github.com/lukaskubanek/OrderedDictionary/releases
[3]: https://developer.apple.com/swift/
[4]: https://github.com/Carthage/Carthage
[5]: LICENSE.md
[6]: https://github.com/Carthage/Carthage/
[7]: https://developer.apple.com/swift/
[8]: https://github.com/lukaskubanek/OrderedDictionary/releases
[9]: https://developer.apple.com/swift/
[10]: https://github.com/Carthage/Carthage
[11]: http://lukaskubanek.com
[12]: https://twitter.com/kubanekl
[13]: LICENSE.md
[image-1]: https://img.shields.io/github/release/lukaskubanek/OrderedDictionary.svg?style=flat-square
[image-2]: https://img.shields.io/badge/Swift-2.0_(7b6)-orange.svg?style=flat-square
[image-3]: https://img.shields.io/badge/Platform-OS_X%20&_iOS-yellowgreen.svg?style=flat-square
[image-4]: https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat-square "Carthage compatible"
[image-5]: https://img.shields.io/badge/license-MIT-lightgrey.svg?style=flat-square "License"
[image-1]: https://img.shields.io/travis/lukaskubanek/OrderedDictionary.svg?style=flat-square
[image-2]: https://img.shields.io/github/release/lukaskubanek/OrderedDictionary.svg?style=flat-square
[image-3]: https://img.shields.io/badge/Swift-2.0-orange.svg?style=flat-square
[image-4]: https://img.shields.io/badge/platform-OS_X%20&_iOS-yellowgreen.svg?style=flat-square
[image-5]: https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat-square "Carthage compatible"
[image-6]: https://img.shields.io/badge/license-MIT-lightgrey.svg?style=flat-square "License"
+136 -49
View File
@@ -6,35 +6,51 @@
// Copyright © 2015 Lukas Kubanek. All rights reserved.
//
/// The `OrderedDictionary` is a collection which combines the features of `Dictionary` and `Array`.
/// It maps keys to values and additionally sorts the key-value pairs by zero-based integer index.
public struct OrderedDictionary<Key : Hashable, Value>: CollectionType, ArrayLiteralConvertible, CustomStringConvertible {
public struct OrderedDictionary<Key: Hashable, Value>: MutableCollectionType, ArrayLiteralConvertible, CustomStringConvertible {
// ======================================================= //
// MARK: - Type Aliases
// ======================================================= //
public typealias Element = (Key, Value)
public typealias Index = Int
// ======================================================= //
// MARK: - Initialization
// ======================================================= //
public init() {
self.orderedKeys = []
self.keysToValues = [:]
}
public init() {}
public init(arrayLiteral elements: Element...) {
self.init()
public init(elements: [Element]) {
for element in elements {
self[element.0] = element.1
}
}
// MARK: - Type Aliases
public init(arrayLiteral elements: Element...) {
self.init(elements: elements)
}
public typealias Element = (Key, Value)
public typealias Index = Int
// ======================================================= //
// MARK: - Accessing Keys & Values
// ======================================================= //
public var orderedKeys: [Key] {
return _orderedKeys
}
public var orderedValues: [Value] {
return _orderedKeys.flatMap { _keysToValues[$0] }
}
// ======================================================= //
// MARK: - Managing Content Using Keys
// ======================================================= //
public subscript(key: Key) -> Value? {
get {
return keysToValues[key]
return _keysToValues[key]
}
set(newValue) {
if let newValue = newValue {
@@ -45,23 +61,36 @@ public struct OrderedDictionary<Key : Hashable, Value>: CollectionType, ArrayLit
}
}
public func containsKey(key: Key) -> Bool {
return _orderedKeys.contains(key)
}
public mutating func updateValue(value: Value, forKey key: Key) -> Value? {
if orderedKeys.contains(key) {
guard let currentValue = keysToValues[key] else { fatalError("Inconsistency error occured in OrderedDictionary.") }
keysToValues[key] = value
if _orderedKeys.contains(key) {
guard let currentValue = _keysToValues[key] else {
fatalError("Inconsistency error occured in OrderedDictionary")
}
_keysToValues[key] = value
return currentValue
} else {
orderedKeys.append(key)
keysToValues[key] = value
_orderedKeys.append(key)
_keysToValues[key] = value
return nil
}
}
public mutating func removeValueForKey(key: Key) -> Value? {
if let index = orderedKeys.indexOf(key) {
orderedKeys.removeAtIndex(index)
guard let currentValue = keysToValues[key] else { fatalError("Inconsistency error occured in OrderedDictionary.") }
keysToValues[key] = nil
if let index = _orderedKeys.indexOf(key) {
guard let currentValue = _keysToValues[key] else {
fatalError("Inconsistency error occured in OrderedDictionary")
}
_orderedKeys.removeAtIndex(index)
_keysToValues[key] = nil
return currentValue
} else {
return nil
@@ -69,19 +98,21 @@ public struct OrderedDictionary<Key : Hashable, Value>: CollectionType, ArrayLit
}
public mutating func removeAll(keepCapacity keepCapacity: Bool = true) {
orderedKeys.removeAll(keepCapacity: keepCapacity)
keysToValues.removeAll(keepCapacity: keepCapacity)
_orderedKeys.removeAll(keepCapacity: keepCapacity)
_keysToValues.removeAll(keepCapacity: keepCapacity)
}
// ======================================================= //
// MARK: - Managing Content Using Indexes
// ======================================================= //
public subscript(index: Index) -> Element {
get {
if let element = elementAtIndex(index) {
return element
} else {
fatalError("Index out of bounds in OrderedDictionary.")
guard let element = elementAtIndex(index) else {
fatalError("OrderedDictionary index out of range")
}
return element
}
set(newValue) {
updateElement(newValue, atIndex: index)
@@ -89,38 +120,73 @@ public struct OrderedDictionary<Key : Hashable, Value>: CollectionType, ArrayLit
}
public func indexForKey(key: Key) -> Index? {
return orderedKeys.indexOf(key)
return _orderedKeys.indexOf(key)
}
public func elementAtIndex(index: Index) -> Element? {
guard orderedKeys.indices.contains(index) else { return nil }
guard _orderedKeys.indices.contains(index) else { return nil }
let key = orderedKeys[index]
guard let value = self.keysToValues[key] else { fatalError("Inconsistency error occured in OrderedDictionary.") }
let key = _orderedKeys[index]
guard let value = self._keysToValues[key] else {
fatalError("Inconsistency error occured in OrderedDictionary")
}
return (key, value)
}
public mutating func insertElementWithKey(key: Key, value: Value, atIndex index: Index) -> Value? {
return insertElement((key, value), atIndex: index)
}
public mutating func insertElement(newElement: Element, atIndex index: Index) -> Value? {
guard index >= 0 else {
fatalError("Negative OrderedDictionary index is out of range")
}
guard index <= count else {
fatalError("OrderedDictionary index out of range")
}
let (key, value) = (newElement.0, newElement.1)
let adjustedIndex: Int
let currentValue: Value?
if let currentIndex = _orderedKeys.indexOf(key) {
currentValue = _keysToValues[key]
adjustedIndex = (currentIndex < index - 1) ? index - 1 : index
_orderedKeys.removeAtIndex(currentIndex)
_keysToValues[key] = nil
} else {
currentValue = nil
adjustedIndex = index
}
_orderedKeys.insert(key, atIndex: adjustedIndex)
_keysToValues[key] = value
return currentValue
}
public mutating func updateElement(element: Element, atIndex index: Index) -> Element? {
// TODO: Handle index out of range
let currentElement = elementAtIndex(index)
if currentElement != nil {
keysToValues.removeValueForKey(element.0)
guard let currentElement = elementAtIndex(index) else {
fatalError("OrderedDictionary index out of range")
}
let (newKey, newValue) = (element.0, element.1)
orderedKeys[index] = newKey
keysToValues[newKey] = newValue
_orderedKeys[index] = newKey
_keysToValues[newKey] = newValue
return currentElement
}
public mutating func removeAtIndex(index: Index) -> Element? {
if let element = elementAtIndex(index) {
orderedKeys.removeAtIndex(index)
keysToValues.removeValueForKey(element.0)
_orderedKeys.removeAtIndex(index)
_keysToValues.removeValueForKey(element.0)
return element
} else {
@@ -128,19 +194,28 @@ public struct OrderedDictionary<Key : Hashable, Value>: CollectionType, ArrayLit
}
}
// ======================================================= //
// MARK: - Description
// ======================================================= //
public var description: String {
let content = map({ "\($0.0): \($0.1)" }).joinWithSeparator(", ")
return "[\(content)]"
}
// ======================================================= //
// MARK: - Backing Store
// ======================================================= //
private var orderedKeys: [Key]
private var keysToValues: [Key: Value]
/// The backing store for the ordered keys.
private var _orderedKeys = [Key]()
/// The backing store for the mapping of keys to values.
private var _keysToValues = [Key: Value]()
// ======================================================= //
// MARK: - SequenceType & Indexable Conformance
// ======================================================= //
public func generate() -> AnyGenerator<Element> {
var nextIndex = 0
@@ -149,8 +224,12 @@ public struct OrderedDictionary<Key : Hashable, Value>: CollectionType, ArrayLit
return anyGenerator {
guard nextIndex < lastIndex else { return nil }
let nextKey = self.orderedKeys[nextIndex]
guard let nextValue = self.keysToValues[nextKey] else { fatalError("Inconsistency error occured in OrderedDictionary.") }
let nextKey = self._orderedKeys[nextIndex]
guard let nextValue = self._keysToValues[nextKey] else {
fatalError("Inconsistency error occured in OrderedDictionary")
}
let element = (nextKey, nextValue)
nextIndex++
@@ -159,8 +238,16 @@ public struct OrderedDictionary<Key : Hashable, Value>: CollectionType, ArrayLit
}
}
public var startIndex: Index { return orderedKeys.startIndex }
public var startIndex: Index {
return _orderedKeys.startIndex
}
public var endIndex: Index { return orderedKeys.endIndex }
public var endIndex: Index {
return _orderedKeys.endIndex
}
}
public func == <Key: Equatable, Value: Equatable>(lhs: OrderedDictionary<Key, Value>, rhs: OrderedDictionary<Key, Value>) -> Bool {
return lhs._orderedKeys == rhs._orderedKeys && lhs._keysToValues == rhs._keysToValues
}
+1 -1
View File
@@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.1</string>
<string>0.5</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
+202 -78
View File
@@ -7,7 +7,6 @@
//
import XCTest
import Nimble
import OrderedDictionary
class OrderedDictionaryTests: XCTestCase {
@@ -22,139 +21,264 @@ class OrderedDictionaryTests: XCTestCase {
]
}
// ======================================================= //
// MARK: - Content
// ======================================================= //
func testInitializedContent() {
expect(self.orderedDictionary.count) == 3
XCTAssertEqual(orderedDictionary.count, 3)
expect(self.orderedDictionary["A"]) == 1
expect(self.orderedDictionary.indexForKey("A")) == 0
XCTAssertEqual(orderedDictionary["A"], 1)
XCTAssertEqual(orderedDictionary.indexForKey("A"), 0)
XCTAssertTrue(orderedDictionary.containsKey("A"))
expect(self.orderedDictionary["B"]) == 2
expect(self.orderedDictionary.indexForKey("B")) == 1
XCTAssertEqual(orderedDictionary["B"], 2)
XCTAssertEqual(orderedDictionary.indexForKey("B"), 1)
XCTAssertTrue(orderedDictionary.containsKey("B"))
expect(self.orderedDictionary["C"]) == 3
expect(self.orderedDictionary.indexForKey("C")) == 2
XCTAssertEqual(orderedDictionary["C"], 3)
XCTAssertEqual(orderedDictionary.indexForKey("C"), 2)
XCTAssertTrue(orderedDictionary.containsKey("C"))
}
func testInitializationUsingPairs() {
let elements = [
("A", 1),
("B", 2),
("C", 3)
]
XCTAssertTrue(OrderedDictionary(elements: elements) == orderedDictionary)
}
func testElementsGenerator() {
for entry in self.orderedDictionary.enumerate() {
expect(self.orderedDictionary[entry.index].0) == entry.element.0
expect(self.orderedDictionary[entry.index].1) == entry.element.1
for entry in orderedDictionary.enumerate() {
XCTAssertEqual(orderedDictionary[entry.index].0, entry.element.0)
XCTAssertEqual(orderedDictionary[entry.index].1, entry.element.1)
}
}
// MARK: - Key-based Modifications
func testKeyBasedSubscript() {
self.orderedDictionary["A"] = 5
self.orderedDictionary["D"] = 10
self.orderedDictionary["B"] = nil
expect(self.orderedDictionary.count) == 3
expect(self.orderedDictionary["A"]) == 5
expect(self.orderedDictionary.indexForKey("A")) == 0
expect(self.orderedDictionary["B"]).to(beNil())
expect(self.orderedDictionary.indexForKey("B")).to(beNil())
expect(self.orderedDictionary["C"]) == 3
expect(self.orderedDictionary.indexForKey("C")) == 1
expect(self.orderedDictionary["D"]) == 10
expect(self.orderedDictionary.indexForKey("D")) == 2
func testOrderedKeysAndValues() {
XCTAssertEqual(orderedDictionary.orderedKeys, ["A", "B", "C"])
XCTAssertEqual(orderedDictionary.orderedValues, [1, 2, 3])
}
// ======================================================= //
// MARK: - Key-based Modifications
// ======================================================= //
func testKeyBasedSubscript() {
orderedDictionary["A"] = 5
orderedDictionary["D"] = 10
orderedDictionary["B"] = nil
XCTAssertEqual(orderedDictionary.count, 3)
XCTAssertEqual(orderedDictionary["A"], 5)
XCTAssertEqual(orderedDictionary.indexForKey("A"), 0)
XCTAssertTrue(orderedDictionary.containsKey("A"))
XCTAssertNil(orderedDictionary["B"])
XCTAssertNil(orderedDictionary.indexForKey("B"))
XCTAssertFalse(orderedDictionary.containsKey("B"))
XCTAssertEqual(orderedDictionary["C"], 3)
XCTAssertEqual(orderedDictionary.indexForKey("C"), 1)
XCTAssertTrue(orderedDictionary.containsKey("C"))
XCTAssertEqual(orderedDictionary["D"], 10)
XCTAssertEqual(orderedDictionary.indexForKey("D"), 2)
XCTAssertTrue(orderedDictionary.containsKey("D"))
}
// ======================================================= //
// MARK: - Index-based Modifications
// ======================================================= //
func testIndexBasedSubscriptForRetrievingValues() {
let elementAtIndex0 = self.orderedDictionary[0]
expect(elementAtIndex0.0) == "A"
expect(elementAtIndex0.1) == 1
let elementAtIndex0 = orderedDictionary[0]
XCTAssertEqual(elementAtIndex0.0, "A")
XCTAssertEqual(elementAtIndex0.1, 1)
let elementAtIndex1 = self.orderedDictionary[1]
expect(elementAtIndex1.0) == "B"
expect(elementAtIndex1.1) == 2
let elementAtIndex1 = orderedDictionary[1]
XCTAssertEqual(elementAtIndex1.0, "B")
XCTAssertEqual(elementAtIndex1.1, 2)
let elementAtIndex2 = self.orderedDictionary[2]
expect(elementAtIndex2.0) == "C"
expect(elementAtIndex2.1) == 3
let elementAtIndex2 = orderedDictionary[2]
XCTAssertEqual(elementAtIndex2.0, "C")
XCTAssertEqual(elementAtIndex2.1, 3)
}
func testIndexBasedSubscriptForSettingValues() {
self.orderedDictionary[0] = ("F", 10)
self.orderedDictionary[1] = ("B", 5)
orderedDictionary[0] = ("F", 10)
orderedDictionary[1] = ("B", 5)
let elementAtIndex0 = self.orderedDictionary[0]
expect(elementAtIndex0.0) == "F"
expect(elementAtIndex0.1) == 10
let elementAtIndex0 = orderedDictionary[0]
XCTAssertEqual(elementAtIndex0.0, "F")
XCTAssertEqual(elementAtIndex0.1, 10)
let elementAtIndex1 = self.orderedDictionary[1]
expect(elementAtIndex1.0) == "B"
expect(elementAtIndex1.1) == 5
let elementAtIndex1 = orderedDictionary[1]
XCTAssertEqual(elementAtIndex1.0, "B")
XCTAssertEqual(elementAtIndex1.1, 5)
let elementAtIndex2 = self.orderedDictionary[2]
expect(elementAtIndex2.0) == "C"
expect(elementAtIndex2.1) == 3
let elementAtIndex2 = orderedDictionary[2]
XCTAssertEqual(elementAtIndex2.0, "C")
XCTAssertEqual(elementAtIndex2.1, 3)
}
func testRetrievingElementAtNonExistentIndex() {
expect(self.orderedDictionary.elementAtIndex(10)).to(beNil())
XCTAssertNil(orderedDictionary.elementAtIndex(10))
}
func testIndexBasedInsertionsOfElementsWithDistinctKeys() {
orderedDictionary.insertElement(("T", 15), atIndex: 0)
orderedDictionary.insertElement(("U", 16), atIndex: 2)
orderedDictionary.insertElement(("V", 17), atIndex: 5)
orderedDictionary.insertElement(("W", 18), atIndex: 2)
let XCTAssertEqualedOrderedDictionary: OrderedDictionary<String, Int> = [
("T", 15),
("A", 1),
("W", 18),
("U", 16),
("B", 2),
("C", 3),
("V", 17)
]
XCTAssertTrue(orderedDictionary == XCTAssertEqualedOrderedDictionary)
}
func testIndexBasedInsertionOfElementWithSameKeyBeforeItsCurrentIndex() {
let previousValue = orderedDictionary.insertElement(("B", 5), atIndex: 0)
let expectedOrderedDictionary: OrderedDictionary<String, Int> = [
("B", 5),
("A", 1),
("C", 3)
]
XCTAssertEqual(orderedDictionary.count, 3)
XCTAssertTrue(orderedDictionary == expectedOrderedDictionary)
XCTAssertEqual(previousValue, 2)
}
func testIndexBasedInsertionOfElementWithSameKeyAtItsCurrentIndex() {
let previousValue = orderedDictionary.insertElement(("B", 5), atIndex: 1)
let expectedOrderedDictionary: OrderedDictionary<String, Int> = [
("A", 1),
("B", 5),
("C", 3)
]
XCTAssertEqual(orderedDictionary.count, 3)
XCTAssertTrue(orderedDictionary == expectedOrderedDictionary)
XCTAssertEqual(previousValue, 2)
}
func testIndexBasedInsertionOfElementWithSameKeyAfterItsCurrentIndex() {
let previousValue = orderedDictionary.insertElement(("B", 5), atIndex: 3)
let expectedOrderedDictionary: OrderedDictionary<String, Int> = [
("A", 1),
("C", 3),
("B", 5)
]
XCTAssertEqual(orderedDictionary.count, 3)
XCTAssertTrue(orderedDictionary == expectedOrderedDictionary)
XCTAssertEqual(previousValue, 2)
}
// ======================================================= //
// MARK: - Removal
// ======================================================= //
func testRemoveAll() {
self.orderedDictionary.removeAll()
orderedDictionary.removeAll()
expect(self.orderedDictionary.count) == 0
XCTAssertEqual(orderedDictionary.count, 0)
}
func testRemovalForKey() {
let removedValue1 = self.orderedDictionary.removeValueForKey("A")
let removedValue2 = self.orderedDictionary.removeValueForKey("K")
let removedValue1 = orderedDictionary.removeValueForKey("A")
let removedValue2 = orderedDictionary.removeValueForKey("K")
expect(removedValue1) == 1
expect(removedValue2).to(beNil())
XCTAssertEqual(removedValue1, 1)
XCTAssertNil(removedValue2)
expect(self.orderedDictionary.count) == 2
XCTAssertEqual(orderedDictionary.count, 2)
expect(self.orderedDictionary["A"]).to(beNil())
expect(self.orderedDictionary.indexForKey("A")).to(beNil())
XCTAssertNil(orderedDictionary["A"])
XCTAssertNil(orderedDictionary.indexForKey("A"))
expect(self.orderedDictionary["B"]) == 2
expect(self.orderedDictionary.indexForKey("B")) == 0
XCTAssertEqual(orderedDictionary["B"], 2)
XCTAssertEqual(orderedDictionary.indexForKey("B"), 0)
expect(self.orderedDictionary["C"]) == 3
expect(self.orderedDictionary.indexForKey("C")) == 1
XCTAssertEqual(orderedDictionary["C"], 3)
XCTAssertEqual(orderedDictionary.indexForKey("C"), 1)
}
func testRemovalAtIndex() {
let removedElement1 = self.orderedDictionary.removeAtIndex(1)
let removedElement2 = self.orderedDictionary.removeAtIndex(0)
let removedElement3 = self.orderedDictionary.removeAtIndex(5)
let removedElement1 = orderedDictionary.removeAtIndex(1)
let removedElement2 = orderedDictionary.removeAtIndex(0)
let removedElement3 = orderedDictionary.removeAtIndex(5)
expect(removedElement1?.0) == "B"
expect(removedElement1?.1) == 2
XCTAssertEqual(removedElement1?.0, "B")
XCTAssertEqual(removedElement1?.1, 2)
expect(removedElement2?.0) == "A"
expect(removedElement2?.1) == 1
XCTAssertEqual(removedElement2?.0, "A")
XCTAssertEqual(removedElement2?.1, 1)
expect(removedElement3).to(beNil())
XCTAssertNil(removedElement3)
expect(self.orderedDictionary.count) == 1
XCTAssertEqual(orderedDictionary.count, 1)
let elementAtIndex0 = self.orderedDictionary[0]
expect(elementAtIndex0.0) == "C"
expect(elementAtIndex0.1) == 3
let elementAtIndex0 = orderedDictionary[0]
XCTAssertEqual(elementAtIndex0.0, "C")
XCTAssertEqual(elementAtIndex0.1, 3)
}
// ======================================================= //
// MARK: - Sorting
// ======================================================= //
func testSortingInPlace() {
var testOrderedDictionary: OrderedDictionary<String, Int> = [
("E", 4),
("G", 3),
("A", 3),
("D", 1),
("B", 4)
]
testOrderedDictionary.sortInPlace { (item1, item2) in
if item1.1 == item2.1 {
return item1.0 < item2.0
} else {
return item1.1 < item2.1
}
}
let expectedOrderedDictionary: OrderedDictionary<String, Int> = [
("D", 1),
("A", 3),
("G", 3),
("B", 4),
("E", 4)
]
XCTAssertTrue(testOrderedDictionary == expectedOrderedDictionary)
}
// ======================================================= //
// MARK: - Description
// ======================================================= //
func testDescription() {
expect(self.orderedDictionary.description) == "[A: 1, B: 2, C: 3]"
XCTAssertEqual(orderedDictionary.description, "[A: 1, B: 2, C: 3]")
}
}