├── .gitignore ├── LICENSE ├── LongestCommonSubsequence.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata ├── LongestCommonSubsequence ├── Base.lproj │ ├── Main_iPad.storyboard │ └── Main_iPhone.storyboard ├── Images.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── LaunchImage.launchimage │ │ └── Contents.json ├── LongestCommonSubsequence-Info.plist ├── LongestCommonSubsequence-Prefix.pch ├── NSArray+LongestCommonSubsequence.h ├── NSArray+LongestCommonSubsequence.m ├── SKAppDelegate.h ├── SKAppDelegate.m ├── SKViewController.h ├── SKViewController.m ├── en.lproj │ └── InfoPlist.strings └── main.m ├── LongestCommonSubsequenceTests ├── LongCommonSubsequenceInTableView.m ├── LongestCommonSubsequenceTests-Info.plist ├── LongestCommonSubsequenceTests.m └── en.lproj │ └── InfoPlist.strings └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | .DS_Store 3 | */build/* 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | profile 14 | *.moved-aside 15 | DerivedData 16 | .idea/ 17 | *.hmap 18 | *.xccheckout 19 | 20 | #CocoaPods 21 | Pods 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Soroush Khanlou 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /LongestCommonSubsequence.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 1A3F15BB182BD42B00C3D5AE /* LongCommonSubsequenceInTableView.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A3F15BA182BD42B00C3D5AE /* LongCommonSubsequenceInTableView.m */; }; 11 | 1AC04D6F182A7D550079D681 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1AC04D6E182A7D550079D681 /* Foundation.framework */; }; 12 | 1AC04D71182A7D550079D681 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1AC04D70182A7D550079D681 /* CoreGraphics.framework */; }; 13 | 1AC04D73182A7D550079D681 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1AC04D72182A7D550079D681 /* UIKit.framework */; }; 14 | 1AC04D79182A7D550079D681 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 1AC04D77182A7D550079D681 /* InfoPlist.strings */; }; 15 | 1AC04D7B182A7D550079D681 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 1AC04D7A182A7D550079D681 /* main.m */; }; 16 | 1AC04D7F182A7D550079D681 /* SKAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1AC04D7E182A7D550079D681 /* SKAppDelegate.m */; }; 17 | 1AC04D82182A7D550079D681 /* Main_iPhone.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1AC04D80182A7D550079D681 /* Main_iPhone.storyboard */; }; 18 | 1AC04D85182A7D550079D681 /* Main_iPad.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1AC04D83182A7D550079D681 /* Main_iPad.storyboard */; }; 19 | 1AC04D88182A7D550079D681 /* SKViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1AC04D87182A7D550079D681 /* SKViewController.m */; }; 20 | 1AC04D8A182A7D550079D681 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1AC04D89182A7D550079D681 /* Images.xcassets */; }; 21 | 1AC04D91182A7D550079D681 /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1AC04D90182A7D550079D681 /* XCTest.framework */; }; 22 | 1AC04D92182A7D550079D681 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1AC04D6E182A7D550079D681 /* Foundation.framework */; }; 23 | 1AC04D93182A7D550079D681 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1AC04D72182A7D550079D681 /* UIKit.framework */; }; 24 | 1AC04D9B182A7D550079D681 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 1AC04D99182A7D550079D681 /* InfoPlist.strings */; }; 25 | 1AC04D9D182A7D550079D681 /* LongestCommonSubsequenceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1AC04D9C182A7D550079D681 /* LongestCommonSubsequenceTests.m */; }; 26 | 1AC04DA8182A7D9D0079D681 /* NSArray+LongestCommonSubsequence.m in Sources */ = {isa = PBXBuildFile; fileRef = 1AC04DA7182A7D9D0079D681 /* NSArray+LongestCommonSubsequence.m */; }; 27 | /* End PBXBuildFile section */ 28 | 29 | /* Begin PBXContainerItemProxy section */ 30 | 1AC04D94182A7D550079D681 /* PBXContainerItemProxy */ = { 31 | isa = PBXContainerItemProxy; 32 | containerPortal = 1AC04D63182A7D550079D681 /* Project object */; 33 | proxyType = 1; 34 | remoteGlobalIDString = 1AC04D6A182A7D550079D681; 35 | remoteInfo = LongestCommonSubsequence; 36 | }; 37 | /* End PBXContainerItemProxy section */ 38 | 39 | /* Begin PBXFileReference section */ 40 | 1A3F15BA182BD42B00C3D5AE /* LongCommonSubsequenceInTableView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LongCommonSubsequenceInTableView.m; sourceTree = ""; }; 41 | 1AC04D6B182A7D550079D681 /* LongestCommonSubsequence.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LongestCommonSubsequence.app; sourceTree = BUILT_PRODUCTS_DIR; }; 42 | 1AC04D6E182A7D550079D681 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 43 | 1AC04D70182A7D550079D681 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; }; 44 | 1AC04D72182A7D550079D681 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; 45 | 1AC04D76182A7D550079D681 /* LongestCommonSubsequence-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "LongestCommonSubsequence-Info.plist"; sourceTree = ""; }; 46 | 1AC04D78182A7D550079D681 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; 47 | 1AC04D7A182A7D550079D681 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 48 | 1AC04D7C182A7D550079D681 /* LongestCommonSubsequence-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "LongestCommonSubsequence-Prefix.pch"; sourceTree = ""; }; 49 | 1AC04D7D182A7D550079D681 /* SKAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SKAppDelegate.h; sourceTree = ""; }; 50 | 1AC04D7E182A7D550079D681 /* SKAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SKAppDelegate.m; sourceTree = ""; }; 51 | 1AC04D81182A7D550079D681 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main_iPhone.storyboard; sourceTree = ""; }; 52 | 1AC04D84182A7D550079D681 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main_iPad.storyboard; sourceTree = ""; }; 53 | 1AC04D86182A7D550079D681 /* SKViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SKViewController.h; sourceTree = ""; }; 54 | 1AC04D87182A7D550079D681 /* SKViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SKViewController.m; sourceTree = ""; }; 55 | 1AC04D89182A7D550079D681 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 56 | 1AC04D8F182A7D550079D681 /* LongestCommonSubsequenceTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = LongestCommonSubsequenceTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 57 | 1AC04D90182A7D550079D681 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; }; 58 | 1AC04D98182A7D550079D681 /* LongestCommonSubsequenceTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "LongestCommonSubsequenceTests-Info.plist"; sourceTree = ""; }; 59 | 1AC04D9A182A7D550079D681 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; 60 | 1AC04D9C182A7D550079D681 /* LongestCommonSubsequenceTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LongestCommonSubsequenceTests.m; sourceTree = ""; }; 61 | 1AC04DA6182A7D9D0079D681 /* NSArray+LongestCommonSubsequence.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+LongestCommonSubsequence.h"; sourceTree = ""; }; 62 | 1AC04DA7182A7D9D0079D681 /* NSArray+LongestCommonSubsequence.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+LongestCommonSubsequence.m"; sourceTree = ""; }; 63 | /* End PBXFileReference section */ 64 | 65 | /* Begin PBXFrameworksBuildPhase section */ 66 | 1AC04D68182A7D550079D681 /* Frameworks */ = { 67 | isa = PBXFrameworksBuildPhase; 68 | buildActionMask = 2147483647; 69 | files = ( 70 | 1AC04D71182A7D550079D681 /* CoreGraphics.framework in Frameworks */, 71 | 1AC04D73182A7D550079D681 /* UIKit.framework in Frameworks */, 72 | 1AC04D6F182A7D550079D681 /* Foundation.framework in Frameworks */, 73 | ); 74 | runOnlyForDeploymentPostprocessing = 0; 75 | }; 76 | 1AC04D8C182A7D550079D681 /* Frameworks */ = { 77 | isa = PBXFrameworksBuildPhase; 78 | buildActionMask = 2147483647; 79 | files = ( 80 | 1AC04D91182A7D550079D681 /* XCTest.framework in Frameworks */, 81 | 1AC04D93182A7D550079D681 /* UIKit.framework in Frameworks */, 82 | 1AC04D92182A7D550079D681 /* Foundation.framework in Frameworks */, 83 | ); 84 | runOnlyForDeploymentPostprocessing = 0; 85 | }; 86 | /* End PBXFrameworksBuildPhase section */ 87 | 88 | /* Begin PBXGroup section */ 89 | 1AC04D62182A7D550079D681 = { 90 | isa = PBXGroup; 91 | children = ( 92 | 1AC04D74182A7D550079D681 /* LongestCommonSubsequence */, 93 | 1AC04D96182A7D550079D681 /* LongestCommonSubsequenceTests */, 94 | 1AC04D6D182A7D550079D681 /* Frameworks */, 95 | 1AC04D6C182A7D550079D681 /* Products */, 96 | ); 97 | sourceTree = ""; 98 | usesTabs = 0; 99 | }; 100 | 1AC04D6C182A7D550079D681 /* Products */ = { 101 | isa = PBXGroup; 102 | children = ( 103 | 1AC04D6B182A7D550079D681 /* LongestCommonSubsequence.app */, 104 | 1AC04D8F182A7D550079D681 /* LongestCommonSubsequenceTests.xctest */, 105 | ); 106 | name = Products; 107 | sourceTree = ""; 108 | }; 109 | 1AC04D6D182A7D550079D681 /* Frameworks */ = { 110 | isa = PBXGroup; 111 | children = ( 112 | 1AC04D6E182A7D550079D681 /* Foundation.framework */, 113 | 1AC04D70182A7D550079D681 /* CoreGraphics.framework */, 114 | 1AC04D72182A7D550079D681 /* UIKit.framework */, 115 | 1AC04D90182A7D550079D681 /* XCTest.framework */, 116 | ); 117 | name = Frameworks; 118 | sourceTree = ""; 119 | }; 120 | 1AC04D74182A7D550079D681 /* LongestCommonSubsequence */ = { 121 | isa = PBXGroup; 122 | children = ( 123 | 1AC04D7D182A7D550079D681 /* SKAppDelegate.h */, 124 | 1AC04D7E182A7D550079D681 /* SKAppDelegate.m */, 125 | 1AC04D80182A7D550079D681 /* Main_iPhone.storyboard */, 126 | 1AC04D83182A7D550079D681 /* Main_iPad.storyboard */, 127 | 1AC04D86182A7D550079D681 /* SKViewController.h */, 128 | 1AC04D87182A7D550079D681 /* SKViewController.m */, 129 | 1AC04DA6182A7D9D0079D681 /* NSArray+LongestCommonSubsequence.h */, 130 | 1AC04DA7182A7D9D0079D681 /* NSArray+LongestCommonSubsequence.m */, 131 | 1AC04D89182A7D550079D681 /* Images.xcassets */, 132 | 1AC04D75182A7D550079D681 /* Supporting Files */, 133 | ); 134 | path = LongestCommonSubsequence; 135 | sourceTree = ""; 136 | }; 137 | 1AC04D75182A7D550079D681 /* Supporting Files */ = { 138 | isa = PBXGroup; 139 | children = ( 140 | 1AC04D76182A7D550079D681 /* LongestCommonSubsequence-Info.plist */, 141 | 1AC04D77182A7D550079D681 /* InfoPlist.strings */, 142 | 1AC04D7A182A7D550079D681 /* main.m */, 143 | 1AC04D7C182A7D550079D681 /* LongestCommonSubsequence-Prefix.pch */, 144 | ); 145 | name = "Supporting Files"; 146 | sourceTree = ""; 147 | }; 148 | 1AC04D96182A7D550079D681 /* LongestCommonSubsequenceTests */ = { 149 | isa = PBXGroup; 150 | children = ( 151 | 1AC04D9C182A7D550079D681 /* LongestCommonSubsequenceTests.m */, 152 | 1A3F15BA182BD42B00C3D5AE /* LongCommonSubsequenceInTableView.m */, 153 | 1AC04D97182A7D550079D681 /* Supporting Files */, 154 | ); 155 | path = LongestCommonSubsequenceTests; 156 | sourceTree = ""; 157 | }; 158 | 1AC04D97182A7D550079D681 /* Supporting Files */ = { 159 | isa = PBXGroup; 160 | children = ( 161 | 1AC04D98182A7D550079D681 /* LongestCommonSubsequenceTests-Info.plist */, 162 | 1AC04D99182A7D550079D681 /* InfoPlist.strings */, 163 | ); 164 | name = "Supporting Files"; 165 | sourceTree = ""; 166 | }; 167 | /* End PBXGroup section */ 168 | 169 | /* Begin PBXNativeTarget section */ 170 | 1AC04D6A182A7D550079D681 /* LongestCommonSubsequence */ = { 171 | isa = PBXNativeTarget; 172 | buildConfigurationList = 1AC04DA0182A7D550079D681 /* Build configuration list for PBXNativeTarget "LongestCommonSubsequence" */; 173 | buildPhases = ( 174 | 1AC04D67182A7D550079D681 /* Sources */, 175 | 1AC04D68182A7D550079D681 /* Frameworks */, 176 | 1AC04D69182A7D550079D681 /* Resources */, 177 | ); 178 | buildRules = ( 179 | ); 180 | dependencies = ( 181 | ); 182 | name = LongestCommonSubsequence; 183 | productName = LongestCommonSubsequence; 184 | productReference = 1AC04D6B182A7D550079D681 /* LongestCommonSubsequence.app */; 185 | productType = "com.apple.product-type.application"; 186 | }; 187 | 1AC04D8E182A7D550079D681 /* LongestCommonSubsequenceTests */ = { 188 | isa = PBXNativeTarget; 189 | buildConfigurationList = 1AC04DA3182A7D550079D681 /* Build configuration list for PBXNativeTarget "LongestCommonSubsequenceTests" */; 190 | buildPhases = ( 191 | 1AC04D8B182A7D550079D681 /* Sources */, 192 | 1AC04D8C182A7D550079D681 /* Frameworks */, 193 | 1AC04D8D182A7D550079D681 /* Resources */, 194 | ); 195 | buildRules = ( 196 | ); 197 | dependencies = ( 198 | 1AC04D95182A7D550079D681 /* PBXTargetDependency */, 199 | ); 200 | name = LongestCommonSubsequenceTests; 201 | productName = LongestCommonSubsequenceTests; 202 | productReference = 1AC04D8F182A7D550079D681 /* LongestCommonSubsequenceTests.xctest */; 203 | productType = "com.apple.product-type.bundle.unit-test"; 204 | }; 205 | /* End PBXNativeTarget section */ 206 | 207 | /* Begin PBXProject section */ 208 | 1AC04D63182A7D550079D681 /* Project object */ = { 209 | isa = PBXProject; 210 | attributes = { 211 | CLASSPREFIX = SK; 212 | LastUpgradeCheck = 0500; 213 | TargetAttributes = { 214 | 1AC04D8E182A7D550079D681 = { 215 | TestTargetID = 1AC04D6A182A7D550079D681; 216 | }; 217 | }; 218 | }; 219 | buildConfigurationList = 1AC04D66182A7D550079D681 /* Build configuration list for PBXProject "LongestCommonSubsequence" */; 220 | compatibilityVersion = "Xcode 3.2"; 221 | developmentRegion = English; 222 | hasScannedForEncodings = 0; 223 | knownRegions = ( 224 | en, 225 | Base, 226 | ); 227 | mainGroup = 1AC04D62182A7D550079D681; 228 | productRefGroup = 1AC04D6C182A7D550079D681 /* Products */; 229 | projectDirPath = ""; 230 | projectRoot = ""; 231 | targets = ( 232 | 1AC04D6A182A7D550079D681 /* LongestCommonSubsequence */, 233 | 1AC04D8E182A7D550079D681 /* LongestCommonSubsequenceTests */, 234 | ); 235 | }; 236 | /* End PBXProject section */ 237 | 238 | /* Begin PBXResourcesBuildPhase section */ 239 | 1AC04D69182A7D550079D681 /* Resources */ = { 240 | isa = PBXResourcesBuildPhase; 241 | buildActionMask = 2147483647; 242 | files = ( 243 | 1AC04D85182A7D550079D681 /* Main_iPad.storyboard in Resources */, 244 | 1AC04D8A182A7D550079D681 /* Images.xcassets in Resources */, 245 | 1AC04D82182A7D550079D681 /* Main_iPhone.storyboard in Resources */, 246 | 1AC04D79182A7D550079D681 /* InfoPlist.strings in Resources */, 247 | ); 248 | runOnlyForDeploymentPostprocessing = 0; 249 | }; 250 | 1AC04D8D182A7D550079D681 /* Resources */ = { 251 | isa = PBXResourcesBuildPhase; 252 | buildActionMask = 2147483647; 253 | files = ( 254 | 1AC04D9B182A7D550079D681 /* InfoPlist.strings in Resources */, 255 | ); 256 | runOnlyForDeploymentPostprocessing = 0; 257 | }; 258 | /* End PBXResourcesBuildPhase section */ 259 | 260 | /* Begin PBXSourcesBuildPhase section */ 261 | 1AC04D67182A7D550079D681 /* Sources */ = { 262 | isa = PBXSourcesBuildPhase; 263 | buildActionMask = 2147483647; 264 | files = ( 265 | 1AC04DA8182A7D9D0079D681 /* NSArray+LongestCommonSubsequence.m in Sources */, 266 | 1AC04D7F182A7D550079D681 /* SKAppDelegate.m in Sources */, 267 | 1AC04D88182A7D550079D681 /* SKViewController.m in Sources */, 268 | 1AC04D7B182A7D550079D681 /* main.m in Sources */, 269 | ); 270 | runOnlyForDeploymentPostprocessing = 0; 271 | }; 272 | 1AC04D8B182A7D550079D681 /* Sources */ = { 273 | isa = PBXSourcesBuildPhase; 274 | buildActionMask = 2147483647; 275 | files = ( 276 | 1AC04D9D182A7D550079D681 /* LongestCommonSubsequenceTests.m in Sources */, 277 | 1A3F15BB182BD42B00C3D5AE /* LongCommonSubsequenceInTableView.m in Sources */, 278 | ); 279 | runOnlyForDeploymentPostprocessing = 0; 280 | }; 281 | /* End PBXSourcesBuildPhase section */ 282 | 283 | /* Begin PBXTargetDependency section */ 284 | 1AC04D95182A7D550079D681 /* PBXTargetDependency */ = { 285 | isa = PBXTargetDependency; 286 | target = 1AC04D6A182A7D550079D681 /* LongestCommonSubsequence */; 287 | targetProxy = 1AC04D94182A7D550079D681 /* PBXContainerItemProxy */; 288 | }; 289 | /* End PBXTargetDependency section */ 290 | 291 | /* Begin PBXVariantGroup section */ 292 | 1AC04D77182A7D550079D681 /* InfoPlist.strings */ = { 293 | isa = PBXVariantGroup; 294 | children = ( 295 | 1AC04D78182A7D550079D681 /* en */, 296 | ); 297 | name = InfoPlist.strings; 298 | sourceTree = ""; 299 | }; 300 | 1AC04D80182A7D550079D681 /* Main_iPhone.storyboard */ = { 301 | isa = PBXVariantGroup; 302 | children = ( 303 | 1AC04D81182A7D550079D681 /* Base */, 304 | ); 305 | name = Main_iPhone.storyboard; 306 | sourceTree = ""; 307 | }; 308 | 1AC04D83182A7D550079D681 /* Main_iPad.storyboard */ = { 309 | isa = PBXVariantGroup; 310 | children = ( 311 | 1AC04D84182A7D550079D681 /* Base */, 312 | ); 313 | name = Main_iPad.storyboard; 314 | sourceTree = ""; 315 | }; 316 | 1AC04D99182A7D550079D681 /* InfoPlist.strings */ = { 317 | isa = PBXVariantGroup; 318 | children = ( 319 | 1AC04D9A182A7D550079D681 /* en */, 320 | ); 321 | name = InfoPlist.strings; 322 | sourceTree = ""; 323 | }; 324 | /* End PBXVariantGroup section */ 325 | 326 | /* Begin XCBuildConfiguration section */ 327 | 1AC04D9E182A7D550079D681 /* Debug */ = { 328 | isa = XCBuildConfiguration; 329 | buildSettings = { 330 | ALWAYS_SEARCH_USER_PATHS = NO; 331 | ARCHS = "$(ARCHS_STANDARD_INCLUDING_64_BIT)"; 332 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 333 | CLANG_CXX_LIBRARY = "libc++"; 334 | CLANG_ENABLE_MODULES = YES; 335 | CLANG_ENABLE_OBJC_ARC = YES; 336 | CLANG_WARN_BOOL_CONVERSION = YES; 337 | CLANG_WARN_CONSTANT_CONVERSION = YES; 338 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 339 | CLANG_WARN_EMPTY_BODY = YES; 340 | CLANG_WARN_ENUM_CONVERSION = YES; 341 | CLANG_WARN_INT_CONVERSION = YES; 342 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 343 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 344 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 345 | COPY_PHASE_STRIP = NO; 346 | GCC_C_LANGUAGE_STANDARD = gnu99; 347 | GCC_DYNAMIC_NO_PIC = NO; 348 | GCC_OPTIMIZATION_LEVEL = 0; 349 | GCC_PREPROCESSOR_DEFINITIONS = ( 350 | "DEBUG=1", 351 | "$(inherited)", 352 | ); 353 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 354 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 355 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 356 | GCC_WARN_UNDECLARED_SELECTOR = YES; 357 | GCC_WARN_UNINITIALIZED_AUTOS = YES; 358 | GCC_WARN_UNUSED_FUNCTION = YES; 359 | GCC_WARN_UNUSED_VARIABLE = YES; 360 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 361 | ONLY_ACTIVE_ARCH = YES; 362 | SDKROOT = iphoneos; 363 | TARGETED_DEVICE_FAMILY = "1,2"; 364 | }; 365 | name = Debug; 366 | }; 367 | 1AC04D9F182A7D550079D681 /* Release */ = { 368 | isa = XCBuildConfiguration; 369 | buildSettings = { 370 | ALWAYS_SEARCH_USER_PATHS = NO; 371 | ARCHS = "$(ARCHS_STANDARD_INCLUDING_64_BIT)"; 372 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 373 | CLANG_CXX_LIBRARY = "libc++"; 374 | CLANG_ENABLE_MODULES = YES; 375 | CLANG_ENABLE_OBJC_ARC = YES; 376 | CLANG_WARN_BOOL_CONVERSION = YES; 377 | CLANG_WARN_CONSTANT_CONVERSION = YES; 378 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 379 | CLANG_WARN_EMPTY_BODY = YES; 380 | CLANG_WARN_ENUM_CONVERSION = YES; 381 | CLANG_WARN_INT_CONVERSION = YES; 382 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 383 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 384 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 385 | COPY_PHASE_STRIP = YES; 386 | ENABLE_NS_ASSERTIONS = NO; 387 | GCC_C_LANGUAGE_STANDARD = gnu99; 388 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 389 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 390 | GCC_WARN_UNDECLARED_SELECTOR = YES; 391 | GCC_WARN_UNINITIALIZED_AUTOS = YES; 392 | GCC_WARN_UNUSED_FUNCTION = YES; 393 | GCC_WARN_UNUSED_VARIABLE = YES; 394 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 395 | SDKROOT = iphoneos; 396 | TARGETED_DEVICE_FAMILY = "1,2"; 397 | VALIDATE_PRODUCT = YES; 398 | }; 399 | name = Release; 400 | }; 401 | 1AC04DA1182A7D550079D681 /* Debug */ = { 402 | isa = XCBuildConfiguration; 403 | buildSettings = { 404 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 405 | ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; 406 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 407 | GCC_PREFIX_HEADER = "LongestCommonSubsequence/LongestCommonSubsequence-Prefix.pch"; 408 | INFOPLIST_FILE = "LongestCommonSubsequence/LongestCommonSubsequence-Info.plist"; 409 | PRODUCT_NAME = "$(TARGET_NAME)"; 410 | WRAPPER_EXTENSION = app; 411 | }; 412 | name = Debug; 413 | }; 414 | 1AC04DA2182A7D550079D681 /* Release */ = { 415 | isa = XCBuildConfiguration; 416 | buildSettings = { 417 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 418 | ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; 419 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 420 | GCC_PREFIX_HEADER = "LongestCommonSubsequence/LongestCommonSubsequence-Prefix.pch"; 421 | INFOPLIST_FILE = "LongestCommonSubsequence/LongestCommonSubsequence-Info.plist"; 422 | PRODUCT_NAME = "$(TARGET_NAME)"; 423 | WRAPPER_EXTENSION = app; 424 | }; 425 | name = Release; 426 | }; 427 | 1AC04DA4182A7D550079D681 /* Debug */ = { 428 | isa = XCBuildConfiguration; 429 | buildSettings = { 430 | ARCHS = "$(ARCHS_STANDARD_INCLUDING_64_BIT)"; 431 | BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/LongestCommonSubsequence.app/LongestCommonSubsequence"; 432 | FRAMEWORK_SEARCH_PATHS = ( 433 | "$(SDKROOT)/Developer/Library/Frameworks", 434 | "$(inherited)", 435 | "$(DEVELOPER_FRAMEWORKS_DIR)", 436 | ); 437 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 438 | GCC_PREFIX_HEADER = "LongestCommonSubsequence/LongestCommonSubsequence-Prefix.pch"; 439 | GCC_PREPROCESSOR_DEFINITIONS = ( 440 | "DEBUG=1", 441 | "$(inherited)", 442 | ); 443 | INFOPLIST_FILE = "LongestCommonSubsequenceTests/LongestCommonSubsequenceTests-Info.plist"; 444 | PRODUCT_NAME = "$(TARGET_NAME)"; 445 | TEST_HOST = "$(BUNDLE_LOADER)"; 446 | WRAPPER_EXTENSION = xctest; 447 | }; 448 | name = Debug; 449 | }; 450 | 1AC04DA5182A7D550079D681 /* Release */ = { 451 | isa = XCBuildConfiguration; 452 | buildSettings = { 453 | ARCHS = "$(ARCHS_STANDARD_INCLUDING_64_BIT)"; 454 | BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/LongestCommonSubsequence.app/LongestCommonSubsequence"; 455 | FRAMEWORK_SEARCH_PATHS = ( 456 | "$(SDKROOT)/Developer/Library/Frameworks", 457 | "$(inherited)", 458 | "$(DEVELOPER_FRAMEWORKS_DIR)", 459 | ); 460 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 461 | GCC_PREFIX_HEADER = "LongestCommonSubsequence/LongestCommonSubsequence-Prefix.pch"; 462 | INFOPLIST_FILE = "LongestCommonSubsequenceTests/LongestCommonSubsequenceTests-Info.plist"; 463 | PRODUCT_NAME = "$(TARGET_NAME)"; 464 | TEST_HOST = "$(BUNDLE_LOADER)"; 465 | WRAPPER_EXTENSION = xctest; 466 | }; 467 | name = Release; 468 | }; 469 | /* End XCBuildConfiguration section */ 470 | 471 | /* Begin XCConfigurationList section */ 472 | 1AC04D66182A7D550079D681 /* Build configuration list for PBXProject "LongestCommonSubsequence" */ = { 473 | isa = XCConfigurationList; 474 | buildConfigurations = ( 475 | 1AC04D9E182A7D550079D681 /* Debug */, 476 | 1AC04D9F182A7D550079D681 /* Release */, 477 | ); 478 | defaultConfigurationIsVisible = 0; 479 | defaultConfigurationName = Release; 480 | }; 481 | 1AC04DA0182A7D550079D681 /* Build configuration list for PBXNativeTarget "LongestCommonSubsequence" */ = { 482 | isa = XCConfigurationList; 483 | buildConfigurations = ( 484 | 1AC04DA1182A7D550079D681 /* Debug */, 485 | 1AC04DA2182A7D550079D681 /* Release */, 486 | ); 487 | defaultConfigurationIsVisible = 0; 488 | defaultConfigurationName = Release; 489 | }; 490 | 1AC04DA3182A7D550079D681 /* Build configuration list for PBXNativeTarget "LongestCommonSubsequenceTests" */ = { 491 | isa = XCConfigurationList; 492 | buildConfigurations = ( 493 | 1AC04DA4182A7D550079D681 /* Debug */, 494 | 1AC04DA5182A7D550079D681 /* Release */, 495 | ); 496 | defaultConfigurationIsVisible = 0; 497 | defaultConfigurationName = Release; 498 | }; 499 | /* End XCConfigurationList section */ 500 | }; 501 | rootObject = 1AC04D63182A7D550079D681 /* Project object */; 502 | } 503 | -------------------------------------------------------------------------------- /LongestCommonSubsequence.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /LongestCommonSubsequence/Base.lproj/Main_iPad.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /LongestCommonSubsequence/Base.lproj/Main_iPhone.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /LongestCommonSubsequence/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "40x40", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "60x60", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "ipad", 20 | "size" : "29x29", 21 | "scale" : "1x" 22 | }, 23 | { 24 | "idiom" : "ipad", 25 | "size" : "29x29", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "ipad", 30 | "size" : "40x40", 31 | "scale" : "1x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "40x40", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "76x76", 41 | "scale" : "1x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "76x76", 46 | "scale" : "2x" 47 | } 48 | ], 49 | "info" : { 50 | "version" : 1, 51 | "author" : "xcode" 52 | } 53 | } -------------------------------------------------------------------------------- /LongestCommonSubsequence/Images.xcassets/LaunchImage.launchimage/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "orientation" : "portrait", 5 | "idiom" : "iphone", 6 | "extent" : "full-screen", 7 | "minimum-system-version" : "7.0", 8 | "scale" : "2x" 9 | }, 10 | { 11 | "orientation" : "portrait", 12 | "idiom" : "iphone", 13 | "subtype" : "retina4", 14 | "extent" : "full-screen", 15 | "minimum-system-version" : "7.0", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "orientation" : "portrait", 20 | "idiom" : "ipad", 21 | "extent" : "full-screen", 22 | "minimum-system-version" : "7.0", 23 | "scale" : "1x" 24 | }, 25 | { 26 | "orientation" : "landscape", 27 | "idiom" : "ipad", 28 | "extent" : "full-screen", 29 | "minimum-system-version" : "7.0", 30 | "scale" : "1x" 31 | }, 32 | { 33 | "orientation" : "portrait", 34 | "idiom" : "ipad", 35 | "extent" : "full-screen", 36 | "minimum-system-version" : "7.0", 37 | "scale" : "2x" 38 | }, 39 | { 40 | "orientation" : "landscape", 41 | "idiom" : "ipad", 42 | "extent" : "full-screen", 43 | "minimum-system-version" : "7.0", 44 | "scale" : "2x" 45 | } 46 | ], 47 | "info" : { 48 | "version" : 1, 49 | "author" : "xcode" 50 | } 51 | } -------------------------------------------------------------------------------- /LongestCommonSubsequence/LongestCommonSubsequence-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleDisplayName 8 | ${PRODUCT_NAME} 9 | CFBundleExecutable 10 | ${EXECUTABLE_NAME} 11 | CFBundleIdentifier 12 | com.khanlou.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1.0 25 | LSRequiresIPhoneOS 26 | 27 | UIMainStoryboardFile 28 | Main_iPhone 29 | UIMainStoryboardFile~ipad 30 | Main_iPad 31 | UIRequiredDeviceCapabilities 32 | 33 | armv7 34 | 35 | UISupportedInterfaceOrientations 36 | 37 | UIInterfaceOrientationPortrait 38 | UIInterfaceOrientationLandscapeLeft 39 | UIInterfaceOrientationLandscapeRight 40 | 41 | UISupportedInterfaceOrientations~ipad 42 | 43 | UIInterfaceOrientationPortrait 44 | UIInterfaceOrientationPortraitUpsideDown 45 | UIInterfaceOrientationLandscapeLeft 46 | UIInterfaceOrientationLandscapeRight 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /LongestCommonSubsequence/LongestCommonSubsequence-Prefix.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Prefix header 3 | // 4 | // The contents of this file are implicitly included at the beginning of every source file. 5 | // 6 | 7 | #import 8 | 9 | #ifndef __IPHONE_5_0 10 | #warning "This project uses features only available in iOS SDK 5.0 and later." 11 | #endif 12 | 13 | #ifdef __OBJC__ 14 | #import 15 | #import 16 | #endif 17 | -------------------------------------------------------------------------------- /LongestCommonSubsequence/NSArray+LongestCommonSubsequence.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+LongestCommonSubsequence.h 3 | // LongestCommonSubsequence 4 | // 5 | // Created by Soroush Khanlou on 11/6/13. 6 | // 7 | // 8 | 9 | #import 10 | 11 | @interface NSArray (LongestCommonSubsequence) 12 | 13 | - (NSIndexSet*) indexesOfCommonElementsWithArray:(NSArray*)array; 14 | - (NSIndexSet*) indexesOfCommonElementsWithArray:(NSArray*)array addedIndexes:(NSIndexSet**)addedIndexes removedIndexes:(NSIndexSet**)removedIndexes; 15 | 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /LongestCommonSubsequence/NSArray+LongestCommonSubsequence.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+LongestCommonSubsequence.m 3 | // LongestCommonSubsequence 4 | // 5 | // Created by Soroush Khanlou on 11/6/13. 6 | // 7 | // 8 | 9 | #import "NSArray+LongestCommonSubsequence.h" 10 | 11 | @implementation NSArray (LongestCommonSubsequence) 12 | 13 | - (NSIndexSet*) indexesOfCommonElementsWithArray:(NSArray*)array { 14 | return [self indexesOfCommonElementsWithArray:array addedIndexes:nil removedIndexes:nil]; 15 | } 16 | 17 | - (NSIndexSet*) indexesOfCommonElementsWithArray:(NSArray*)array addedIndexes:(NSIndexSet**)addedIndexes removedIndexes:(NSIndexSet**)removedIndexes { 18 | 19 | const NSInteger firstDimension = self.count + 1; 20 | 21 | NSInteger **lengths = calloc(firstDimension, sizeof(NSInteger*)); 22 | 23 | if(lengths == NULL) { 24 | [[self class] raiseMallocExceptionWithBytes: firstDimension * sizeof(NSInteger*) * CHAR_BIT / 8]; 25 | } 26 | 27 | const NSInteger secondDimension = array.count + 1; 28 | 29 | for(NSInteger i = 0; i < firstDimension; i++) { 30 | lengths[i] = calloc(secondDimension, sizeof(NSInteger)); 31 | 32 | if(lengths[i] == NULL) { 33 | [[self class] raiseMallocExceptionWithBytes: secondDimension * sizeof(NSInteger) * CHAR_BIT / 8]; 34 | } 35 | } 36 | 37 | for (NSInteger i = self.count; i >= 0; i--) { 38 | for (NSInteger j = array.count; j >= 0; j--) { 39 | 40 | if (i == self.count || j == array.count) { 41 | lengths[i][j] = 0; 42 | } else if ([self[i] isEqual:array[j]]) { 43 | lengths[i][j] = 1 + lengths[i+1][j+1]; 44 | } else { 45 | lengths[i][j] = MAX(lengths[i+1][j], lengths[i][j+1]); 46 | } 47 | } 48 | } 49 | 50 | NSMutableIndexSet *commonIndexes = [NSMutableIndexSet indexSet]; 51 | 52 | for (NSInteger i = 0, j = 0 ; i < self.count && j < array.count; ) { 53 | if ([self[i] isEqual:array[j]]) { 54 | [commonIndexes addIndex:i]; 55 | i++; j++; 56 | } else if (lengths[i+1][j] >= lengths[i][j+1]) { 57 | i++; 58 | } else { 59 | j++; 60 | } 61 | } 62 | 63 | for(NSInteger i = 0; i < firstDimension; i++) { 64 | free(lengths[i]); 65 | } 66 | 67 | free(lengths); 68 | 69 | if (removedIndexes) { 70 | NSMutableIndexSet *_removedIndexes = [NSMutableIndexSet indexSet]; 71 | 72 | for (NSInteger i = 0; i < self.count; i++) { 73 | if (![commonIndexes containsIndex:i]) { 74 | [_removedIndexes addIndex:i]; 75 | } 76 | } 77 | *removedIndexes = _removedIndexes; 78 | } 79 | 80 | 81 | if (addedIndexes) { 82 | NSArray *commonObjects = [self objectsAtIndexes:commonIndexes]; 83 | 84 | NSMutableIndexSet *_addedIndexes = [NSMutableIndexSet indexSet]; 85 | for (NSInteger i = 0, j = 0; i < commonObjects.count || j < array.count; ) { 86 | if (i < commonObjects.count && j < array.count && [commonObjects[i] isEqual:array[j]]) { 87 | i++; 88 | j++; 89 | } else { 90 | [_addedIndexes addIndex:j]; 91 | j++; 92 | } 93 | } 94 | 95 | *addedIndexes = _addedIndexes; 96 | } 97 | return commonIndexes; 98 | 99 | } 100 | 101 | + (void) raiseMallocExceptionWithBytes: (long long) bytes { 102 | NSString* const bytesString = [NSByteCountFormatter stringFromByteCount: bytes countStyle: NSByteCountFormatterCountStyleMemory]; 103 | 104 | NSString* const format = [NSString stringWithFormat: @"Unable to allocate %@.", bytesString]; 105 | 106 | [NSException raise: NSMallocException format: @"%@", format]; 107 | } 108 | 109 | @end 110 | -------------------------------------------------------------------------------- /LongestCommonSubsequence/SKAppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // SKAppDelegate.h 3 | // LongestCommonSubsequence 4 | // 5 | // Created by Soroush Khanlou on 11/6/13. 6 | // 7 | // 8 | 9 | #import 10 | 11 | @interface SKAppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /LongestCommonSubsequence/SKAppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // SKAppDelegate.m 3 | // LongestCommonSubsequence 4 | // 5 | // Created by Soroush Khanlou on 11/6/13. 6 | // 7 | // 8 | 9 | #import "SKAppDelegate.h" 10 | 11 | @implementation SKAppDelegate 12 | 13 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 14 | { 15 | // Override point for customization after application launch. 16 | return YES; 17 | } 18 | 19 | - (void)applicationWillResignActive:(UIApplication *)application 20 | { 21 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 22 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 23 | } 24 | 25 | - (void)applicationDidEnterBackground:(UIApplication *)application 26 | { 27 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 28 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 29 | } 30 | 31 | - (void)applicationWillEnterForeground:(UIApplication *)application 32 | { 33 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | - (void)applicationDidBecomeActive:(UIApplication *)application 37 | { 38 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 39 | } 40 | 41 | - (void)applicationWillTerminate:(UIApplication *)application 42 | { 43 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 44 | } 45 | 46 | @end 47 | -------------------------------------------------------------------------------- /LongestCommonSubsequence/SKViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // SKViewController.h 3 | // LongestCommonSubsequence 4 | // 5 | // Created by Soroush Khanlou on 11/6/13. 6 | // 7 | // 8 | 9 | #import 10 | 11 | @interface SKViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /LongestCommonSubsequence/SKViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // SKViewController.m 3 | // LongestCommonSubsequence 4 | // 5 | // Created by Soroush Khanlou on 11/6/13. 6 | // 7 | // 8 | 9 | #import "SKViewController.h" 10 | #import "NSArray+LongestCommonSubsequence.h" 11 | 12 | @interface SKViewController () 13 | 14 | @property (nonatomic, strong) UITableView *tableView; 15 | @property (nonatomic, strong) NSArray *sourceData; 16 | 17 | 18 | @end 19 | 20 | @implementation SKViewController 21 | 22 | - (void)viewDidLoad 23 | { 24 | [super viewDidLoad]; 25 | 26 | 27 | self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds]; 28 | _tableView.delegate = self; 29 | _tableView.dataSource = self; 30 | [self.view addSubview:_tableView]; 31 | 32 | [self resetDataSource:nil]; 33 | [_tableView reloadData]; 34 | 35 | self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Reset" style:UIBarButtonItemStyleBordered target:self action:@selector(resetDataSource:)]; 36 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Switch" style:UIBarButtonItemStyleBordered target:self action:@selector(changeDataSource:)]; 37 | } 38 | 39 | - (void) resetDataSource:(id)sender { 40 | [self switchToNewDataSource: @[@"a", @"b", @"c", @"d", @"e"]]; 41 | } 42 | 43 | - (void) changeDataSource:(id)sender { 44 | UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Change Data Source To" 45 | delegate:self 46 | cancelButtonTitle:@"Cancel" 47 | destructiveButtonTitle:nil 48 | otherButtonTitles: 49 | @"a b c d e", 50 | @"1 a b c d e", 51 | @"a b c d e f", 52 | @"a b c d e f g", 53 | @"a b c c.5 d e", 54 | @"b c d e", 55 | @"b c d", 56 | @"", 57 | nil]; 58 | 59 | [sheet showInView:self.view]; 60 | } 61 | 62 | - (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 63 | if (buttonIndex == actionSheet.cancelButtonIndex) { 64 | return; 65 | } 66 | NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex]; 67 | NSArray *components = [title componentsSeparatedByString:@" "]; 68 | [self switchToNewDataSource:components]; 69 | } 70 | 71 | - (void) switchToNewDataSource:(NSArray*)newSource { 72 | NSIndexSet *addedIndexes, *removedIndexes; 73 | 74 | [_sourceData indexesOfCommonElementsWithArray:newSource addedIndexes:&addedIndexes removedIndexes:&removedIndexes]; 75 | 76 | self.sourceData = [newSource mutableCopy]; 77 | 78 | NSMutableArray *indexPathsToAdd = [NSMutableArray array], *indexPathsToDelete = [NSMutableArray array]; 79 | 80 | 81 | [addedIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { 82 | [indexPathsToAdd addObject:[NSIndexPath indexPathForRow:idx inSection:0]]; 83 | }]; 84 | [removedIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { 85 | [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:idx inSection:0]]; 86 | }]; 87 | 88 | [_tableView beginUpdates]; 89 | [_tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationAutomatic]; 90 | [_tableView insertRowsAtIndexPaths:indexPathsToAdd withRowAnimation:UITableViewRowAnimationAutomatic]; 91 | [_tableView endUpdates]; 92 | } 93 | 94 | 95 | #pragma mark - UITableViewDataSource 96 | 97 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 98 | return 1; 99 | } 100 | 101 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 102 | return _sourceData.count; 103 | } 104 | 105 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 106 | static NSString *CellIdentifier = @"Cell"; 107 | 108 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 109 | if (!cell) { 110 | cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 111 | } 112 | 113 | [self configureCell:cell forRowAtIndexPath:indexPath]; 114 | 115 | return cell; 116 | } 117 | 118 | - (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 119 | cell.textLabel.text = [_sourceData objectAtIndex:indexPath.row]; 120 | } 121 | 122 | @end 123 | -------------------------------------------------------------------------------- /LongestCommonSubsequence/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /LongestCommonSubsequence/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // LongestCommonSubsequence 4 | // 5 | // Created by Soroush Khanlou on 11/6/13. 6 | // 7 | // 8 | 9 | #import 10 | 11 | #import "SKAppDelegate.h" 12 | 13 | int main(int argc, char * argv[]) 14 | { 15 | @autoreleasepool { 16 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([SKAppDelegate class])); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LongestCommonSubsequenceTests/LongCommonSubsequenceInTableView.m: -------------------------------------------------------------------------------- 1 | // 2 | // LongCommonSubsequenceInTableView.m 3 | // LongestCommonSubsequence 4 | // 5 | // Created by Soroush Khanlou on 11/7/13. 6 | // 7 | // 8 | 9 | #import 10 | #import "NSArray+LongestCommonSubsequence.h" 11 | 12 | 13 | @interface LongCommonSubsequenceInTableView : XCTestCase 14 | 15 | @property (nonatomic, strong) NSArray *sourceData; 16 | @property (nonatomic, strong) UITableView *tableView; 17 | 18 | @end 19 | 20 | @implementation LongCommonSubsequenceInTableView 21 | 22 | - (void)setUp 23 | { 24 | [super setUp]; 25 | 26 | 27 | self.tableView = [[UITableView alloc] initWithFrame:UIApplication.sharedApplication.keyWindow.bounds]; 28 | _tableView.delegate = self; 29 | _tableView.dataSource = self; 30 | 31 | [UIApplication.sharedApplication.keyWindow addSubview:_tableView]; 32 | [self resetTableView]; 33 | } 34 | 35 | - (void)tearDown 36 | { 37 | self.tableView = nil; 38 | [super tearDown]; 39 | } 40 | 41 | - (void) resetTableView { 42 | self.sourceData = @[@"a", @"b", @"c", @"d", @"e"]; 43 | [_tableView reloadData]; 44 | [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1f]]; 45 | } 46 | 47 | - (void)testRemovingOneItem 48 | { 49 | [self resetTableView]; 50 | 51 | [self switchToNewDataSource:@[@"a", @"b", @"d", @"e"]]; 52 | } 53 | 54 | - (void)testRemovingFirstItem 55 | { 56 | [self resetTableView]; 57 | 58 | [self switchToNewDataSource:@[@"b", @"c", @"d", @"e"]]; 59 | } 60 | 61 | - (void)testRemovingTwoItems 62 | { 63 | [self resetTableView]; 64 | 65 | [self switchToNewDataSource:@[@"a", @"b", @"d"]]; 66 | 67 | } 68 | 69 | - (void) testRemovingAllItems 70 | { 71 | [self resetTableView]; 72 | [self switchToNewDataSource:@[]]; 73 | } 74 | 75 | - (void)testAddingOneItem 76 | { 77 | [self resetTableView]; 78 | 79 | [self switchToNewDataSource:@[@"a", @"b", @"c", @"d", @"e", @"f"]]; 80 | 81 | } 82 | 83 | - (void)testAddingFirstItem 84 | { 85 | [self resetTableView]; 86 | 87 | [self switchToNewDataSource:@[@"1", @"a", @"b", @"c", @"d", @"e"]]; 88 | 89 | } 90 | 91 | - (void)testAddingTwoItems 92 | { 93 | [self resetTableView]; 94 | 95 | [self switchToNewDataSource:@[@"a", @"b", @"c", @"d", @"e", @"f", @"g"]]; 96 | 97 | } 98 | 99 | - (void)testAddingTwoNonAdjacentItems 100 | { 101 | [self resetTableView]; 102 | 103 | [self switchToNewDataSource:@[@"a", @"a.1", @"b", @"c", @"c.2", @"d", @"e", @"e.3"]]; 104 | } 105 | 106 | - (void)testReordering 107 | { 108 | [self resetTableView]; 109 | 110 | [self switchToNewDataSource:@[@"b", @"e", @"d", @"c", @"a"]]; 111 | } 112 | 113 | 114 | - (void)testSwitchingOneItem 115 | { 116 | [self resetTableView]; 117 | 118 | [self switchToNewDataSource:@[@"a", @"b", @"c", @"d", @"f"]]; 119 | 120 | } 121 | 122 | 123 | 124 | - (void) switchToNewDataSource:(NSArray*)newSource { 125 | NSIndexSet *addedIndexes, *removedIndexes; 126 | 127 | [_sourceData indexesOfCommonElementsWithArray:newSource addedIndexes:&addedIndexes removedIndexes:&removedIndexes]; 128 | 129 | self.sourceData = newSource; 130 | 131 | NSMutableArray *indexPathsToAdd = [NSMutableArray array], *indexPathsToDelete = [NSMutableArray array]; 132 | 133 | [addedIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { 134 | [indexPathsToAdd addObject:[NSIndexPath indexPathForRow:idx inSection:0]]; 135 | }]; 136 | [removedIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { 137 | [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:idx inSection:0]]; 138 | }]; 139 | 140 | [_tableView beginUpdates]; 141 | [_tableView insertRowsAtIndexPaths:indexPathsToAdd withRowAnimation:UITableViewRowAnimationNone]; 142 | [_tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationNone]; 143 | [_tableView endUpdates]; 144 | 145 | [self verifyCells]; 146 | } 147 | 148 | - (void) verifyCells 149 | { 150 | [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1f]]; 151 | 152 | XCTAssert([_tableView numberOfRowsInSection:0] == self.sourceData.count, @"number of rows in table view should equal source data count"); 153 | for (NSUInteger i = 0; i < self.sourceData.count; i++) { 154 | NSString* cellLabel = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]].textLabel.text; 155 | XCTAssert([cellLabel isEqualToString:self.sourceData[i]], @"result cells should equal expected"); 156 | } 157 | } 158 | 159 | #pragma mark - UITableViewDataSource 160 | 161 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 162 | return 1; 163 | } 164 | 165 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 166 | return _sourceData.count; 167 | } 168 | 169 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 170 | static NSString *CellIdentifier = @"Cell"; 171 | 172 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 173 | if (!cell) { 174 | cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 175 | } 176 | 177 | [self configureCell:cell forRowAtIndexPath:indexPath]; 178 | 179 | return cell; 180 | } 181 | 182 | - (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 183 | cell.textLabel.text = [_sourceData objectAtIndex:indexPath.row]; 184 | } 185 | 186 | @end 187 | -------------------------------------------------------------------------------- /LongestCommonSubsequenceTests/LongestCommonSubsequenceTests-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | com.khanlou.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundlePackageType 14 | BNDL 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleSignature 18 | ???? 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /LongestCommonSubsequenceTests/LongestCommonSubsequenceTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // LongestCommonSubsequenceTests.m 3 | // LongestCommonSubsequenceTests 4 | // 5 | // Created by Soroush Khanlou on 11/6/13. 6 | // 7 | // 8 | 9 | #import 10 | #import "NSArray+LongestCommonSubsequence.h" 11 | 12 | @interface LongestCommonSubsequenceTests : XCTestCase 13 | 14 | @end 15 | 16 | @implementation LongestCommonSubsequenceTests 17 | 18 | - (void)testSimpleCase 19 | { 20 | NSArray *first = @[@"a", @"b", @"c", @"d", @"e"]; 21 | NSArray *second = @[@"m", @"a", @"b", @"f"]; 22 | 23 | [self compareArray:first toArray:second expectingMatches:2]; 24 | } 25 | 26 | - (void)testWithInitialObjectsMatching 27 | { 28 | NSArray *first = @[@"a", @"b", @"c", @"d", @"e"]; 29 | NSArray *second = @[@"a", @"b", @"f"]; 30 | 31 | [self compareArray:first toArray:second expectingMatches:2]; 32 | } 33 | 34 | - (void)testAllObjectsMatching 35 | { 36 | NSArray *first = @[@"a", @"b", @"c", @"d", @"e"]; 37 | NSArray *second = @[@"a", @"b", @"c", @"d", @"e"]; 38 | 39 | [self compareArray:first toArray:second expectingMatches:5]; 40 | } 41 | 42 | - (void)testRemoveAll 43 | { 44 | NSArray *first = @[@"a", @"b", @"c", @"d", @"e"]; 45 | NSArray *second = @[]; 46 | 47 | [self compareArray:first toArray:second expectingMatches:0]; 48 | } 49 | 50 | - (void)testAddAll 51 | { 52 | NSArray *first = @[]; 53 | NSArray *second = @[@"a", @"b", @"c", @"d", @"e"]; 54 | 55 | [self compareArray:first toArray:second expectingMatches:0]; 56 | } 57 | 58 | 59 | - (void)testAllObjectsMatchingWithDifferentLengths 60 | { 61 | NSArray *first = @[@"a", @"b", @"c", @"d", @"e"]; 62 | NSArray *second = @[@"a", @"b", @"c", @"d"]; 63 | 64 | [self compareArray:first toArray:second expectingMatches:4]; 65 | } 66 | 67 | - (void) testCommutativeProperty 68 | { 69 | NSArray *first = @[@"a", @"b", @"c", @"d", @"e"]; 70 | NSArray *second = @[@"a", @"b", @"c", @"d"]; 71 | 72 | [self compareArray:first toArray:second expectingMatches:4]; 73 | [self compareArray:second toArray:first expectingMatches:4]; 74 | } 75 | 76 | 77 | - (void)testObjectsAtEnd 78 | { 79 | NSArray *first = @[@"a", @"b", @"c", @"d", @"e"]; 80 | NSArray *second = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g"]; 81 | 82 | [self compareArray:first toArray:second expectingMatches:5]; 83 | } 84 | 85 | - (void)testFullMethod 86 | { 87 | NSArray *first = @[@"a", @"b", @"c", @"d", @"e"]; 88 | NSArray *second = @[@"m", @"a", @"b", @"f"]; 89 | 90 | NSIndexSet *addedIndexes, *removedIndexes; 91 | 92 | NSIndexSet *commonIndexes = [first indexesOfCommonElementsWithArray:second addedIndexes:&addedIndexes removedIndexes:&removedIndexes]; 93 | 94 | XCTAssert(commonIndexes.count == 2, @"should match 2 items"); 95 | 96 | NSArray *expectedResult = @[@"a", @"b"]; 97 | XCTAssertEqualObjects(expectedResult, [first objectsAtIndexes:commonIndexes], @"indexes should map to the objects that we expect"); 98 | 99 | // removed indexes ∪ common indexes = all first indexes 100 | NSIndexSet *allFirstIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 5)]; 101 | NSMutableIndexSet *removedPlusCommon = [removedIndexes mutableCopy]; 102 | [removedPlusCommon addIndexes:commonIndexes]; 103 | XCTAssertEqualObjects(allFirstIndexes, removedPlusCommon); 104 | 105 | // added indexes = (0, 3) 106 | NSMutableIndexSet *expectedAddedIndexes = [NSMutableIndexSet indexSet]; 107 | [expectedAddedIndexes addIndex:0]; 108 | [expectedAddedIndexes addIndex:3]; 109 | XCTAssertEqualObjects(addedIndexes, expectedAddedIndexes); 110 | 111 | // removed indexes = (2-4) 112 | NSMutableIndexSet *expectedRemovedIndexes = [NSMutableIndexSet indexSet]; 113 | [expectedRemovedIndexes addIndexesInRange:NSMakeRange(2, 3)]; 114 | XCTAssertEqualObjects(removedIndexes, expectedRemovedIndexes); 115 | 116 | XCTAssertEqual(second.count, first.count + addedIndexes.count - removedIndexes.count, @"number of items in the new array should equal the first array's items + the added items - the deleted items"); 117 | 118 | NSArray *commonObjects = [first objectsAtIndexes:commonIndexes]; 119 | 120 | 121 | 122 | NSMutableArray *firstMinusRemovedIndexes = [first mutableCopy]; 123 | [firstMinusRemovedIndexes removeObjectsAtIndexes:removedIndexes]; 124 | 125 | XCTAssertEqualObjects(commonObjects, firstMinusRemovedIndexes, @"the common objects should be the first array minus the objects at the removed indexes"); 126 | 127 | // first - removed + added = second 128 | NSMutableArray *firstMinusRemovedPlusAdded = [first mutableCopy]; 129 | [firstMinusRemovedPlusAdded removeObjectsAtIndexes:removedIndexes]; 130 | NSArray *addedObjects = [second objectsAtIndexes:addedIndexes]; 131 | [firstMinusRemovedPlusAdded insertObjects:addedObjects atIndexes:addedIndexes]; 132 | XCTAssertEqualObjects(firstMinusRemovedPlusAdded, second); 133 | } 134 | 135 | - (void) compareArray:(NSArray*)firstArray toArray:(NSArray*)secondArray expectingMatches:(NSInteger)matches { 136 | NSIndexSet *addedIndexes, *removedIndexes; 137 | 138 | NSIndexSet *commonIndexes = [firstArray indexesOfCommonElementsWithArray:secondArray addedIndexes:&addedIndexes removedIndexes:&removedIndexes]; 139 | 140 | XCTAssert(commonIndexes.count == matches, @"common index mismatch"); 141 | 142 | 143 | XCTAssertEqual(secondArray.count, firstArray.count + addedIndexes.count - removedIndexes.count, @"number of items in the new array should equal the first array's items + the added items - the deleted items"); 144 | } 145 | 146 | @end 147 | -------------------------------------------------------------------------------- /LongestCommonSubsequenceTests/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NSArray and Longest Common Subsequence 2 | 3 | This is a category on NSArray that finds the indexes of the longest common subsequence with another array. 4 | 5 | `UITableView` has built-in methods for adding and removing cells with animations, but it has no way of peeking into your data source to understand how it has changed. Understanding which elements in a list were removed, which stayed the same, and which were added, seems like a thing computers would be really good at. It turns out, it is. 6 | 7 | The technique is called the **Longest Common Subsequence**. I wrote a category on `NSArray` to calculate this. For the actual algorithm, I used a bottom-up dynamic programming solution, with results cached in a C matrix. It isn't terribly complex, and I shamelessly stole my implementation from [this page](http://www.ics.uci.edu/~eppstein/161/960229.html). You can also read a more in depth explanation [on Wikipedia](http://en.wikipedia.org/wiki/Longest_common_subsequence_problem). 8 | 9 | The algorithm operates in `O(mn)` time, where m and n are the lengths of the two arrays. Determining the equality of objects is done through the `isEquals:` method, so be sure that your implementation of does represent equality for your objects. 10 | 11 | Building the table of the lengths of common subsequences is the first step. Once we have our table of subsequence lengths, we can backtrack through it to get the indexes of the common objects. This technique also documented in a few places, including both of the links above. After that, we use the `commonIndexes` to find the `addedIndexes` and the `removedIndexes`. 12 | 13 | ### Usage 14 | 15 | If all you need is the common indexes, there is a convenience method for you: 16 | 17 | - (NSIndexSet*) indexesOfCommonElementsWithArray:(NSArray*)array; 18 | 19 | Once you have the `NSIndexSet` you can call `-[NSArray objectsAtIndexes:]` to easily get access to the objects themselves. 20 | 21 | Of course, this is Cocoa, and we want to be able to use this information with UIKit, namely, `UITableView` and `UICollectionView`. To get indexes for use with the `- insertRowsAtIndexPaths:withRowAnimation:` and `-deleteRowsAtIndexPaths:withRowAnimation:` methods, use the second method provided. It has two `inout` parameters that return the extra data that is needed. 22 | 23 | - (NSIndexSet*) indexesOfCommonElementsWithArray:(NSArray*)array addedIndexes:(NSIndexSet**)addedIndexes removedIndexes:(NSIndexSet**)removedIndexes; 24 | 25 | ### General Case Implementation 26 | 27 | You can see that a sample implementation (for any general set of data!) is pretty simple. 28 | 29 | NSIndexSet *addedIndexes, *removedIndexes; 30 | 31 | [oldData indexesOfCommonElementsWithArray:newData addedIndexes:&addedIndexes removedIndexes:&removedIndexes]; 32 | 33 | self.dataSource = newData; 34 | 35 | NSMutableArray *indexPathsToAdd = [NSMutableArray array], *indexPathsToDelete = [NSMutableArray array]; 36 | 37 | [addedIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { 38 | [indexPathsToAdd addObject:[NSIndexPath indexPathForRow:idx inSection:0]]; 39 | }]; 40 | [removedIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { 41 | [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:idx inSection:0]]; 42 | }]; 43 | 44 | [_tableView beginUpdates]; 45 | [_tableView insertRowsAtIndexPaths:indexPathsToAdd withRowAnimation:UITableViewRowAnimationAutomatic]; 46 | [_tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationAutomatic]; 47 | [_tableView endUpdates]; 48 | 49 | ### Note about the addedIndexes 50 | 51 | The key to finding the right indexes for adding was (as always) [in the Apple documentation](https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/ManageInsertDeleteRow/ManageInsertDeleteRow.html#//apple_ref/doc/uid/TP40007451-CH10-SW1): 52 | 53 | > [`UITableView`] defers any insertions of rows or sections until after it has handled the deletions of rows or sections. 54 | 55 | This means the deleted objects have to be found first. Once they've been removed, we have only the common objects left. We can then compare the new array to the list of common objects, and find the indexes that the new tableview rows will need to be added at. 56 | --------------------------------------------------------------------------------