From 8008a24c7a6a631191dcc57191456a9084658867 Mon Sep 17 00:00:00 2001 From: Robert Thompson Date: Thu, 18 Feb 2016 17:46:11 -0500 Subject: [PATCH 0001/1536] Initial commit --- Treap/Treap.swift | 160 ++++ Treap/Treap/Treap.xcodeproj/project.pbxproj | 400 ++++++++++ Treap/Treap/Treap/AppDelegate.swift | 27 + .../AppIcon.appiconset/Contents.json | 58 ++ Treap/Treap/Treap/Base.lproj/MainMenu.xib | 681 ++++++++++++++++++ Treap/Treap/Treap/Info.plist | 34 + Treap/Treap/TreapTests/Info.plist | 24 + Treap/Treap/TreapTests/TreapTests.swift | 57 ++ Treap/TreapCollectionType.swift | 81 +++ Treap/TreapMergeSplit.swift | 113 +++ 10 files changed, 1635 insertions(+) create mode 100644 Treap/Treap.swift create mode 100644 Treap/Treap/Treap.xcodeproj/project.pbxproj create mode 100644 Treap/Treap/Treap/AppDelegate.swift create mode 100644 Treap/Treap/Treap/Assets.xcassets/AppIcon.appiconset/Contents.json create mode 100644 Treap/Treap/Treap/Base.lproj/MainMenu.xib create mode 100644 Treap/Treap/Treap/Info.plist create mode 100644 Treap/Treap/TreapTests/Info.plist create mode 100644 Treap/Treap/TreapTests/TreapTests.swift create mode 100644 Treap/TreapCollectionType.swift create mode 100644 Treap/TreapMergeSplit.swift diff --git a/Treap/Treap.swift b/Treap/Treap.swift new file mode 100644 index 000000000..108eec913 --- /dev/null +++ b/Treap/Treap.swift @@ -0,0 +1,160 @@ +// +// Treap.swift +// TreapExample +// +// Created by Robert Thompson on 7/27/15. +// Copyright © 2015 Robert Thompson. All rights reserved. +// + +import Foundation + +public indirect enum Treap { + case Empty + case Node(key: Key, val: Element, p: Int, left: Treap, right: Treap) + + public init() { + self = .Empty + } + + internal func get(key: Key) -> Element? { + switch self { + case .Empty: + return nil + case let .Node(treeKey, val, _, _, _) where treeKey == key: + return val + case let .Node(treeKey, _, _, left, _) where key < treeKey: + return left.get(key) + case let .Node(treeKey, _, _, _, right) where key > treeKey: + return right.get(key) + default: + return nil + } + } + + public func contains(key: Key) -> Bool { + switch self { + case .Empty: + return false + case let .Node(treeKey, _, _, _, _) where treeKey == key: + return true + case let .Node(treeKey, _, _, left, _) where key < treeKey: + return left.contains(key) + case let .Node(treeKey, _, _, _, right) where key > treeKey: + return right.contains(key) + default: + return false + } + } + + public var depth: Int { + get { + switch self { + case .Empty: + return 0 + case let .Node(_, _, _, left, .Empty): + return 1 + left.depth + case let .Node(_, _, _, .Empty, right): + return 1 + right.depth + case let .Node(_, _, _, left, right): + let leftDepth = left.depth + let rightDepth = right.depth + return 1 + max(leftDepth, rightDepth) + } + + } + } + + public var count: Int { + get { + return Treap.countHelper(self) + } + } + + private static func countHelper(treap: Treap) -> Int + { + if case let .Node(_, _, _, left, right) = treap + { + return countHelper(left) + 1 + countHelper(right) + } + + return 0 + } +} + +internal func leftRotate(tree: Treap) -> Treap { + if case let .Node(key, val, p, .Node(leftKey, leftVal, leftP, leftLeft, leftRight), right) = tree { + return .Node(key: leftKey, val: leftVal, p: leftP, left: leftLeft, right: Treap.Node(key: key, val: val, p: p, left: leftRight, right: right)) + } + else + { + return .Empty + } +} + +internal func rightRotate(tree: Treap) -> Treap { + if case let .Node(key, val, p, left, .Node(rightKey, rightVal, rightP, rightLeft, rightRight)) = tree { + return .Node(key: rightKey, val: rightVal, p: rightP, left: Treap.Node(key: key, val: val, p: p, left: left, right: rightLeft), right: rightRight) + } + else + { + return .Empty + } +} + +public extension Treap { + internal func set(key: Key, val: Element, p: Int = Int(arc4random())) -> Treap { + switch self { + case .Empty: + return .Node(key: key, val: val, p: p, left: .Empty, right: .Empty) + case let .Node(nodeKey, nodeVal, nodeP, left, right) where key != nodeKey: + return insertAndBalance(nodeKey, nodeVal, nodeP, left, right, key, val, p) + case let .Node(nodeKey, _, nodeP, left, right) where key == nodeKey: + return .Node(key: key, val: val, p: nodeP, left: left, right: right) + default: // should never happen + return .Empty + } + + } + + private func insertAndBalance(nodeKey: Key, _ nodeVal: Element, _ nodeP: Int, _ left: Treap, _ right: Treap, _ key: Key, _ val: Element, _ p: Int) -> Treap { + let newChild: Treap + let newNode: Treap + let rotate: (Treap) -> Treap + if key < nodeKey { + newChild = left.set(key, val: val, p: p) + newNode = .Node(key: nodeKey, val: nodeVal, p: nodeP, left: newChild, right: right) + rotate = leftRotate + } + else if key > nodeKey { + newChild = right.set(key, val: val, p: p) + newNode = .Node(key: nodeKey, val: nodeVal, p: nodeP, left: left, right: newChild) + rotate = rightRotate + } + else { + // It should be impossible to reach here + newChild = .Empty + newNode = .Empty + return newNode + } + + if case let .Node(_, _, newChildP, _, _) = newChild where newChildP < nodeP { + return rotate(newNode) + } + else { + return newNode + } + } + + internal func delete(key: Key) throws -> Treap { + switch self { + case .Empty: + throw NSError(domain: "com.wta.treap.errorDomain", code: -1, userInfo: nil) + case let .Node(nodeKey, val, p, left, right) where key < nodeKey: + return try Treap.Node(key: nodeKey, val: val, p: p, left: left.delete(key), right: right) + case let .Node(nodeKey, val, p, left, right) where key > nodeKey: + return try Treap.Node(key: nodeKey, val: val, p: p, left: left, right: right.delete(key)) + case let .Node(_, _, _, left, right): + return merge(left, right: right) + } + } +} diff --git a/Treap/Treap/Treap.xcodeproj/project.pbxproj b/Treap/Treap/Treap.xcodeproj/project.pbxproj new file mode 100644 index 000000000..319a22f37 --- /dev/null +++ b/Treap/Treap/Treap.xcodeproj/project.pbxproj @@ -0,0 +1,400 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + E1E34DC71C7670240023AF4D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1E34DC61C7670240023AF4D /* AppDelegate.swift */; }; + E1E34DC91C7670240023AF4D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E1E34DC81C7670240023AF4D /* Assets.xcassets */; }; + E1E34DCC1C7670240023AF4D /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = E1E34DCA1C7670240023AF4D /* MainMenu.xib */; }; + E1E34DD71C7670250023AF4D /* TreapTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1E34DD61C7670250023AF4D /* TreapTests.swift */; }; + E1E34DE31C7670350023AF4D /* Treap.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1E34DE11C7670350023AF4D /* Treap.swift */; }; + E1E34DE41C7670350023AF4D /* TreapMergeSplit.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1E34DE21C7670350023AF4D /* TreapMergeSplit.swift */; }; + E1E34DEA1C7671200023AF4D /* TreapCollectionType.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1E34DE91C7671200023AF4D /* TreapCollectionType.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXContainerItemProxy section */ + E1E34DD31C7670250023AF4D /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = E1E34DBB1C7670240023AF4D /* Project object */; + proxyType = 1; + remoteGlobalIDString = E1E34DC21C7670240023AF4D; + remoteInfo = Treap; + }; +/* End PBXContainerItemProxy section */ + +/* Begin PBXFileReference section */ + E1E34DC31C7670240023AF4D /* Treap.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Treap.app; sourceTree = BUILT_PRODUCTS_DIR; }; + E1E34DC61C7670240023AF4D /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + E1E34DC81C7670240023AF4D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + E1E34DCB1C7670240023AF4D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = ""; }; + E1E34DCD1C7670240023AF4D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + E1E34DD21C7670250023AF4D /* TreapTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TreapTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; + E1E34DD61C7670250023AF4D /* TreapTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TreapTests.swift; sourceTree = ""; }; + E1E34DD81C7670250023AF4D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + E1E34DE11C7670350023AF4D /* Treap.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Treap.swift; path = ../../Treap.swift; sourceTree = ""; }; + E1E34DE21C7670350023AF4D /* TreapMergeSplit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TreapMergeSplit.swift; path = ../../TreapMergeSplit.swift; sourceTree = ""; }; + E1E34DE91C7671200023AF4D /* TreapCollectionType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TreapCollectionType.swift; path = ../../TreapCollectionType.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + E1E34DC01C7670240023AF4D /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + E1E34DCF1C7670250023AF4D /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + E1E34DBA1C7670240023AF4D = { + isa = PBXGroup; + children = ( + E1E34DC51C7670240023AF4D /* Treap */, + E1E34DD51C7670250023AF4D /* TreapTests */, + E1E34DC41C7670240023AF4D /* Products */, + ); + sourceTree = ""; + }; + E1E34DC41C7670240023AF4D /* Products */ = { + isa = PBXGroup; + children = ( + E1E34DC31C7670240023AF4D /* Treap.app */, + E1E34DD21C7670250023AF4D /* TreapTests.xctest */, + ); + name = Products; + sourceTree = ""; + }; + E1E34DC51C7670240023AF4D /* Treap */ = { + isa = PBXGroup; + children = ( + E1E34DC61C7670240023AF4D /* AppDelegate.swift */, + E1E34DC81C7670240023AF4D /* Assets.xcassets */, + E1E34DCA1C7670240023AF4D /* MainMenu.xib */, + E1E34DE11C7670350023AF4D /* Treap.swift */, + E1E34DE21C7670350023AF4D /* TreapMergeSplit.swift */, + E1E34DE91C7671200023AF4D /* TreapCollectionType.swift */, + E1E34DCD1C7670240023AF4D /* Info.plist */, + ); + path = Treap; + sourceTree = ""; + }; + E1E34DD51C7670250023AF4D /* TreapTests */ = { + isa = PBXGroup; + children = ( + E1E34DD61C7670250023AF4D /* TreapTests.swift */, + E1E34DD81C7670250023AF4D /* Info.plist */, + ); + path = TreapTests; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + E1E34DC21C7670240023AF4D /* Treap */ = { + isa = PBXNativeTarget; + buildConfigurationList = E1E34DDB1C7670250023AF4D /* Build configuration list for PBXNativeTarget "Treap" */; + buildPhases = ( + E1E34DBF1C7670240023AF4D /* Sources */, + E1E34DC01C7670240023AF4D /* Frameworks */, + E1E34DC11C7670240023AF4D /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Treap; + productName = Treap; + productReference = E1E34DC31C7670240023AF4D /* Treap.app */; + productType = "com.apple.product-type.application"; + }; + E1E34DD11C7670250023AF4D /* TreapTests */ = { + isa = PBXNativeTarget; + buildConfigurationList = E1E34DDE1C7670250023AF4D /* Build configuration list for PBXNativeTarget "TreapTests" */; + buildPhases = ( + E1E34DCE1C7670250023AF4D /* Sources */, + E1E34DCF1C7670250023AF4D /* Frameworks */, + E1E34DD01C7670250023AF4D /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + E1E34DD41C7670250023AF4D /* PBXTargetDependency */, + ); + name = TreapTests; + productName = TreapTests; + productReference = E1E34DD21C7670250023AF4D /* TreapTests.xctest */; + productType = "com.apple.product-type.bundle.unit-test"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + E1E34DBB1C7670240023AF4D /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0720; + LastUpgradeCheck = 0720; + ORGANIZATIONNAME = "WillowTree, Inc."; + TargetAttributes = { + E1E34DC21C7670240023AF4D = { + CreatedOnToolsVersion = 7.2.1; + }; + E1E34DD11C7670250023AF4D = { + CreatedOnToolsVersion = 7.2.1; + TestTargetID = E1E34DC21C7670240023AF4D; + }; + }; + }; + buildConfigurationList = E1E34DBE1C7670240023AF4D /* Build configuration list for PBXProject "Treap" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = E1E34DBA1C7670240023AF4D; + productRefGroup = E1E34DC41C7670240023AF4D /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + E1E34DC21C7670240023AF4D /* Treap */, + E1E34DD11C7670250023AF4D /* TreapTests */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + E1E34DC11C7670240023AF4D /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + E1E34DC91C7670240023AF4D /* Assets.xcassets in Resources */, + E1E34DCC1C7670240023AF4D /* MainMenu.xib in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + E1E34DD01C7670250023AF4D /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + E1E34DBF1C7670240023AF4D /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + E1E34DEA1C7671200023AF4D /* TreapCollectionType.swift in Sources */, + E1E34DE31C7670350023AF4D /* Treap.swift in Sources */, + E1E34DC71C7670240023AF4D /* AppDelegate.swift in Sources */, + E1E34DE41C7670350023AF4D /* TreapMergeSplit.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + E1E34DCE1C7670250023AF4D /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + E1E34DD71C7670250023AF4D /* TreapTests.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXTargetDependency section */ + E1E34DD41C7670250023AF4D /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = E1E34DC21C7670240023AF4D /* Treap */; + targetProxy = E1E34DD31C7670250023AF4D /* PBXContainerItemProxy */; + }; +/* End PBXTargetDependency section */ + +/* Begin PBXVariantGroup section */ + E1E34DCA1C7670240023AF4D /* MainMenu.xib */ = { + isa = PBXVariantGroup; + children = ( + E1E34DCB1C7670240023AF4D /* Base */, + ); + name = MainMenu.xib; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + E1E34DD91C7670250023AF4D /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + E1E34DDA1C7670250023AF4D /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + E1E34DDC1C7670250023AF4D /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + COMBINE_HIDPI_IMAGES = YES; + INFOPLIST_FILE = Treap/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.willowtree.Treap; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + E1E34DDD1C7670250023AF4D /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + COMBINE_HIDPI_IMAGES = YES; + INFOPLIST_FILE = Treap/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.willowtree.Treap; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; + E1E34DDF1C7670250023AF4D /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + BUNDLE_LOADER = "$(TEST_HOST)"; + COMBINE_HIDPI_IMAGES = YES; + INFOPLIST_FILE = TreapTests/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.willowtree.TreapTests; + PRODUCT_NAME = "$(TARGET_NAME)"; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Treap.app/Contents/MacOS/Treap"; + }; + name = Debug; + }; + E1E34DE01C7670250023AF4D /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + BUNDLE_LOADER = "$(TEST_HOST)"; + COMBINE_HIDPI_IMAGES = YES; + INFOPLIST_FILE = TreapTests/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.willowtree.TreapTests; + PRODUCT_NAME = "$(TARGET_NAME)"; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Treap.app/Contents/MacOS/Treap"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + E1E34DBE1C7670240023AF4D /* Build configuration list for PBXProject "Treap" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + E1E34DD91C7670250023AF4D /* Debug */, + E1E34DDA1C7670250023AF4D /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + E1E34DDB1C7670250023AF4D /* Build configuration list for PBXNativeTarget "Treap" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + E1E34DDC1C7670250023AF4D /* Debug */, + E1E34DDD1C7670250023AF4D /* Release */, + ); + defaultConfigurationIsVisible = 0; + }; + E1E34DDE1C7670250023AF4D /* Build configuration list for PBXNativeTarget "TreapTests" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + E1E34DDF1C7670250023AF4D /* Debug */, + E1E34DE01C7670250023AF4D /* Release */, + ); + defaultConfigurationIsVisible = 0; + }; +/* End XCConfigurationList section */ + }; + rootObject = E1E34DBB1C7670240023AF4D /* Project object */; +} diff --git a/Treap/Treap/Treap/AppDelegate.swift b/Treap/Treap/Treap/AppDelegate.swift new file mode 100644 index 000000000..a156c1cb4 --- /dev/null +++ b/Treap/Treap/Treap/AppDelegate.swift @@ -0,0 +1,27 @@ +// +// AppDelegate.swift +// Treap +// +// Created by Robert Thompson on 2/18/16. +// Copyright © 2016 WillowTree, Inc. All rights reserved. +// + +import Cocoa + +@NSApplicationMain +class AppDelegate: NSObject, NSApplicationDelegate { + + @IBOutlet weak var window: NSWindow! + + + func applicationDidFinishLaunching(aNotification: NSNotification) { + // Insert code here to initialize your application + } + + func applicationWillTerminate(aNotification: NSNotification) { + // Insert code here to tear down your application + } + + +} + diff --git a/Treap/Treap/Treap/Assets.xcassets/AppIcon.appiconset/Contents.json b/Treap/Treap/Treap/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 000000000..2db2b1c7c --- /dev/null +++ b/Treap/Treap/Treap/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,58 @@ +{ + "images" : [ + { + "idiom" : "mac", + "size" : "16x16", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "16x16", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "32x32", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "32x32", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "128x128", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "128x128", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "256x256", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "256x256", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "512x512", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "512x512", + "scale" : "2x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/Treap/Treap/Treap/Base.lproj/MainMenu.xib b/Treap/Treap/Treap/Base.lproj/MainMenu.xib new file mode 100644 index 000000000..8955ad822 --- /dev/null +++ b/Treap/Treap/Treap/Base.lproj/MainMenu.xib @@ -0,0 +1,681 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Treap/Treap/Treap/Info.plist b/Treap/Treap/Treap/Info.plist new file mode 100644 index 000000000..63294f2da --- /dev/null +++ b/Treap/Treap/Treap/Info.plist @@ -0,0 +1,34 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIconFile + + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + LSMinimumSystemVersion + $(MACOSX_DEPLOYMENT_TARGET) + NSHumanReadableCopyright + Copyright © 2016 WillowTree, Inc. All rights reserved. + NSMainNibFile + MainMenu + NSPrincipalClass + NSApplication + + diff --git a/Treap/Treap/TreapTests/Info.plist b/Treap/Treap/TreapTests/Info.plist new file mode 100644 index 000000000..ba72822e8 --- /dev/null +++ b/Treap/Treap/TreapTests/Info.plist @@ -0,0 +1,24 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + BNDL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + + diff --git a/Treap/Treap/TreapTests/TreapTests.swift b/Treap/Treap/TreapTests/TreapTests.swift new file mode 100644 index 000000000..06421ad78 --- /dev/null +++ b/Treap/Treap/TreapTests/TreapTests.swift @@ -0,0 +1,57 @@ +// +// TreapTests.swift +// TreapTests +// +// Created by Robert Thompson on 2/18/16. +// Copyright © 2016 WillowTree, Inc. All rights reserved. +// + +import XCTest +@testable import Treap + +class TreapTests: XCTestCase { + + override func setUp() { + super.setUp() + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + super.tearDown() + } + + func testSanity() { + var treap = Treap.Empty + treap = treap.set(5, val: "a").set(7, val: "b") + XCTAssert(treap.get(5) == "a") + XCTAssert(treap.get(7) == "b") + treap = treap.set(2, val: "c") + XCTAssert(treap.get(2) == "c") + treap = treap.set(2, val: "d") + XCTAssert(treap.get(2) == "d") + treap = try! treap.delete(5) + XCTAssert(!treap.contains(5)) + XCTAssert(treap.contains(7)) + } + + func testFairlyBalanced() { + var treap = Treap.Empty + for i in 0..<1000 { + treap = treap.set(i, val: nil) + } + let depth = treap.depth + XCTAssert(depth < 30, "treap.depth was \(depth)") + } + + func testFairlyBalancedCollection() { + var treap = Treap() + for i in 0..<1000 + { + treap[i] = Optional.None + } + let depth = treap.depth + XCTAssert(depth > 0 && depth < 30) + } + +} diff --git a/Treap/TreapCollectionType.swift b/Treap/TreapCollectionType.swift new file mode 100644 index 000000000..9753b6207 --- /dev/null +++ b/Treap/TreapCollectionType.swift @@ -0,0 +1,81 @@ +// +// TreapCollectionType.swift +// Treap +// +// Created by Robert Thompson on 2/18/16. +// Copyright © 2016 WillowTree, Inc. All rights reserved. +// + +import Foundation + +extension Treap: CollectionType { + public typealias Index = TreapIndex + + public subscript(index: TreapIndex) -> Element { + get { + guard let result = self.get(index.keys[index.keyIndex]) else { + fatalError("Invalid index!") + } + + return result + } + + mutating set { + let key = index.keys[index.keyIndex] + self = self.set(key, val: newValue) + } + } + + public subscript(key: Key) -> Element? { + get { + return self.get(key) + } + + mutating set { + guard let value = newValue else { + let _ = try? self.delete(key) + return + } + + self = self.set(key, val: value) + } + } + + public var startIndex: TreapIndex { + return TreapIndex(keys: keys, keyIndex: 0) + } + + public var endIndex: TreapIndex { + let keys = self.keys + return TreapIndex(keys: keys, keyIndex: keys.count) + } + + private var keys: [Key] { + var results: [Key] = [] + if case let .Node(key, _, _, left, right) = self { + results.appendContentsOf(left.keys) + results.append(key) + results.appendContentsOf(right.keys) + } + + return results + } +} + +public struct TreapIndex: ForwardIndexType { + private let keys: [Key] + private let keyIndex: Int + + public func successor() -> TreapIndex { + return TreapIndex(keys: keys, keyIndex: keyIndex + 1) + } + + private init(keys: [Key] = [], keyIndex: Int = 0) { + self.keys = keys + self.keyIndex = keyIndex + } +} + +public func ==(lhs: TreapIndex, rhs: TreapIndex) -> Bool { + return lhs.keys == rhs.keys && lhs.keyIndex == rhs.keyIndex +} diff --git a/Treap/TreapMergeSplit.swift b/Treap/TreapMergeSplit.swift new file mode 100644 index 000000000..eed6d3d94 --- /dev/null +++ b/Treap/TreapMergeSplit.swift @@ -0,0 +1,113 @@ +// +// TreapMergeSplit.swift +// TreapExample +// +// Created by Robert Thompson on 7/27/15. +// Copyright © 2015 Robert Thompson. All rights reserved. +// + +import Foundation +public extension Treap { + internal func split(key: Key) -> (left: Treap, right: Treap) + { + var current = self + let val: Element + if let newVal = self.get(key) + { + current = try! current.delete(key) + val = newVal + } + else if case let .Node(_, newVal, _, _, _) = self + { + val = newVal + } + else + { + fatalError("No values in treap") + } + + switch self { + case .Node: + if case let .Node(_, _, _, left, right) = current.set(key, val: val, p: -1) + { + return (left: left, right: right) + } + else + { + return (left: .Empty, right: .Empty) + } + default: + return (left: .Empty, right: .Empty) + } + } + + internal var leastKey: Key? { + switch self { + case .Empty: + return nil + case let .Node(key, _, _, .Empty, _): + return key + case let .Node(_, _, _, left, _): + return left.leastKey + } + } + + internal var mostKey: Key? { + switch self { + case .Empty: + return nil + case let .Node(key, _, _, _, .Empty): + return key + case let .Node(_, _, _, _, right): + return right.mostKey + } + } +} + +internal func merge(left: Treap, right: Treap) -> Treap { + switch (left, right) { + case (.Empty, _): + return right + case (_, .Empty): + return left + + case let (.Node(leftKey, leftVal, leftP, leftLeft, leftRight), .Node(rightKey, rightVal, rightP, rightLeft, rightRight)): + if leftP < rightP { + return .Node(key: leftKey, val: leftVal, p: leftP, left: leftLeft, right: merge(leftRight, right: right)) + } + else + { + return .Node(key: rightKey, val: rightVal, p: rightP, left: merge(rightLeft, right: left), right: rightRight) + } + default: + break + } + return .Empty +} + +extension Treap: CustomStringConvertible +{ + public var description: String { + get { + return Treap.descHelper(self, indent: 0) + } + } + + private static func descHelper(treap: Treap, indent: Int) -> String + { + if case let .Node(key, value, priority, left, right) = treap { + var result = "" + let tabs = String(count: indent, repeatedValue: Character("\t")) + + result += descHelper(left, indent: indent + 1) + result += "\n" + tabs + "\(key), \(value), \(priority)\n" + result += descHelper(right, indent: indent + 1) + + return result + } + else + { + return "" + } + } +} \ No newline at end of file From 0598dc1d9105322489522d79d67a64574e8538fc Mon Sep 17 00:00:00 2001 From: Robert Thompson Date: Thu, 18 Feb 2016 17:59:35 -0500 Subject: [PATCH 0002/1536] May as well be BidirectionalIndexType --- Treap/TreapCollectionType.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Treap/TreapCollectionType.swift b/Treap/TreapCollectionType.swift index 9753b6207..a4f37c0b7 100644 --- a/Treap/TreapCollectionType.swift +++ b/Treap/TreapCollectionType.swift @@ -62,7 +62,7 @@ extension Treap: CollectionType { } } -public struct TreapIndex: ForwardIndexType { +public struct TreapIndex: BidirectionalIndexType { private let keys: [Key] private let keyIndex: Int @@ -70,6 +70,10 @@ public struct TreapIndex: ForwardIndexType { return TreapIndex(keys: keys, keyIndex: keyIndex + 1) } + public func predecessor() -> TreapIndex { + return TreapIndex(keys: keys, keyIndex: keyIndex - 1) + } + private init(keys: [Key] = [], keyIndex: Int = 0) { self.keys = keys self.keyIndex = keyIndex From d88f10e94866e1cd6088d6236fcadbf379b51603 Mon Sep 17 00:00:00 2001 From: Robert Thompson Date: Thu, 18 Feb 2016 18:01:22 -0500 Subject: [PATCH 0003/1536] Honestly it's MutableCollectionType --- Treap/TreapCollectionType.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Treap/TreapCollectionType.swift b/Treap/TreapCollectionType.swift index a4f37c0b7..f624bf265 100644 --- a/Treap/TreapCollectionType.swift +++ b/Treap/TreapCollectionType.swift @@ -8,7 +8,7 @@ import Foundation -extension Treap: CollectionType { +extension Treap: MutableCollectionType { public typealias Index = TreapIndex public subscript(index: TreapIndex) -> Element { From 3ed00a3d2dd102b0e68cef456920c7f83416d5b1 Mon Sep 17 00:00:00 2001 From: Robert Thompson Date: Fri, 19 Feb 2016 11:02:24 -0500 Subject: [PATCH 0004/1536] Fix license --- Treap/Treap.swift | 20 ++++++++++++++++++-- Treap/Treap/Treap/AppDelegate.swift | 19 ++++++++++++++++++- Treap/Treap/TreapTests/TreapTests.swift | 20 ++++++++++++++++++-- Treap/TreapCollectionType.swift | 20 ++++++++++++++++++-- Treap/TreapMergeSplit.swift | 20 ++++++++++++++++++-- 5 files changed, 90 insertions(+), 9 deletions(-) diff --git a/Treap/Treap.swift b/Treap/Treap.swift index 108eec913..5413a17c2 100644 --- a/Treap/Treap.swift +++ b/Treap/Treap.swift @@ -3,8 +3,24 @@ // TreapExample // // Created by Robert Thompson on 7/27/15. -// Copyright © 2015 Robert Thompson. All rights reserved. -// +// Copyright © 2016 Robert Thompson +/* Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE.*/ import Foundation diff --git a/Treap/Treap/Treap/AppDelegate.swift b/Treap/Treap/Treap/AppDelegate.swift index a156c1cb4..1813d840b 100644 --- a/Treap/Treap/Treap/AppDelegate.swift +++ b/Treap/Treap/Treap/AppDelegate.swift @@ -3,7 +3,24 @@ // Treap // // Created by Robert Thompson on 2/18/16. -// Copyright © 2016 WillowTree, Inc. All rights reserved. +// Copyright © 2016 Robert Thompson +/* Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE.*/ // import Cocoa diff --git a/Treap/Treap/TreapTests/TreapTests.swift b/Treap/Treap/TreapTests/TreapTests.swift index 06421ad78..496e2f940 100644 --- a/Treap/Treap/TreapTests/TreapTests.swift +++ b/Treap/Treap/TreapTests/TreapTests.swift @@ -3,8 +3,24 @@ // TreapTests // // Created by Robert Thompson on 2/18/16. -// Copyright © 2016 WillowTree, Inc. All rights reserved. -// +// Copyright © 2016 Robert Thompson +/* Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE.*/ import XCTest @testable import Treap diff --git a/Treap/TreapCollectionType.swift b/Treap/TreapCollectionType.swift index f624bf265..36d02eaac 100644 --- a/Treap/TreapCollectionType.swift +++ b/Treap/TreapCollectionType.swift @@ -3,8 +3,24 @@ // Treap // // Created by Robert Thompson on 2/18/16. -// Copyright © 2016 WillowTree, Inc. All rights reserved. -// +// Copyright © 2016 Robert Thompson +/* Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE.*/ import Foundation diff --git a/Treap/TreapMergeSplit.swift b/Treap/TreapMergeSplit.swift index eed6d3d94..51929893b 100644 --- a/Treap/TreapMergeSplit.swift +++ b/Treap/TreapMergeSplit.swift @@ -3,8 +3,24 @@ // TreapExample // // Created by Robert Thompson on 7/27/15. -// Copyright © 2015 Robert Thompson. All rights reserved. -// +// Copyright © 2016 Robert Thompson +/* Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE.*/ import Foundation public extension Treap { From d344becbfbfb054269148cc23261306c337c246a Mon Sep 17 00:00:00 2001 From: Matteo Piombo Date: Sun, 13 Mar 2016 10:15:58 +0100 Subject: [PATCH 0005/1536] Introduce Associated Labels to centroids. This could help showing how to use KMeans as a classifier. Some changes to KMeans properties and function. Updated Tests accordingly. Demo the usage of labels in one small test. --- K-Means/KMeans.swift | 58 ++++++++++++------- K-Means/Tests/KMeansTests.swift | 45 ++++++++------ K-Means/Tests/Tests.xcodeproj/project.pbxproj | 2 +- 3 files changed, 67 insertions(+), 38 deletions(-) diff --git a/K-Means/KMeans.swift b/K-Means/KMeans.swift index 9073b7b46..245a93b4a 100644 --- a/K-Means/KMeans.swift +++ b/K-Means/KMeans.swift @@ -5,21 +5,24 @@ import Foundation -class KMeans { +class KMeans { let numCenters: Int - let convergeDist: Double + let labels: Array