From c16618d5108762b771792c84bb345a9dade6ec5a Mon Sep 17 00:00:00 2001 From: ThibaultKlein Date: Fri, 31 Aug 2018 15:25:20 -0400 Subject: [PATCH] Added stack in data structures --- DevKit.podspec | 5 ++ DevKit/DevKit.xcodeproj/project.pbxproj | 24 +++++++ .../Classes/Data Structures/Stack/Stack.swift | 63 +++++++++++++++++++ .../Data Structures/Stack/StackTests.swift | 49 +++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 DevKit/DevKit/Classes/Data Structures/Stack/Stack.swift create mode 100644 DevKit/DevKitTests/Classes/Data Structures/Stack/StackTests.swift diff --git a/DevKit.podspec b/DevKit.podspec index 132f786..2668b51 100644 --- a/DevKit.podspec +++ b/DevKit.podspec @@ -122,6 +122,11 @@ Collection of commonly used swift code. sp.subspec 'LinkedList' do |ssp| ssp.source_files = 'DevKit/DevKit/Classes/Data\ Structures/Linked\ List/*' end + + # Stack + sp.subspec 'Stack' do |ssp| + ssp.source_files = 'DevKit/DevKit/Classes/Data\ Structures/Stack/*' + end end end diff --git a/DevKit/DevKit.xcodeproj/project.pbxproj b/DevKit/DevKit.xcodeproj/project.pbxproj index 36a3b4d..24bdd60 100644 --- a/DevKit/DevKit.xcodeproj/project.pbxproj +++ b/DevKit/DevKit.xcodeproj/project.pbxproj @@ -45,6 +45,8 @@ 8E8F4AB52138698B00E2FD66 /* LinkedListNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E8F4AB42138698B00E2FD66 /* LinkedListNode.swift */; }; 8E8F4AB7213869E000E2FD66 /* LinkedList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E8F4AB6213869E000E2FD66 /* LinkedList.swift */; }; 8E8F4ABC2138731500E2FD66 /* LinkedListTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E8F4ABB2138731500E2FD66 /* LinkedListTests.swift */; }; + 8E8F4ABF2139C6E300E2FD66 /* Stack.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E8F4ABE2139C6E300E2FD66 /* Stack.swift */; }; + 8E8F4AC22139C87300E2FD66 /* StackTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E8F4AC12139C87300E2FD66 /* StackTests.swift */; }; D7C6243C4F18537B1510C7A1 /* Pods_DevKitUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3939CE1C2FCEB6AE9D5E4974 /* Pods_DevKitUITests.framework */; }; /* End PBXBuildFile section */ @@ -112,6 +114,8 @@ 8E8F4AB42138698B00E2FD66 /* LinkedListNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkedListNode.swift; sourceTree = ""; }; 8E8F4AB6213869E000E2FD66 /* LinkedList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkedList.swift; sourceTree = ""; }; 8E8F4ABB2138731500E2FD66 /* LinkedListTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkedListTests.swift; sourceTree = ""; }; + 8E8F4ABE2139C6E300E2FD66 /* Stack.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Stack.swift; sourceTree = ""; }; + 8E8F4AC12139C87300E2FD66 /* StackTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StackTests.swift; sourceTree = ""; }; 9D986637F245DC8CE1CCA9B0 /* Pods-DevKitUITests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DevKitUITests.release.xcconfig"; path = "Pods/Target Support Files/Pods-DevKitUITests/Pods-DevKitUITests.release.xcconfig"; sourceTree = ""; }; A70409DC5600E02F5134B243 /* Pods-DevKitTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DevKitTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-DevKitTests/Pods-DevKitTests.debug.xcconfig"; sourceTree = ""; }; AA8FB83E8BE3E5509A2577E0 /* Pods_DevKitTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_DevKitTests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -484,6 +488,7 @@ 8E8F4AB22138696E00E2FD66 /* Data Structures */ = { isa = PBXGroup; children = ( + 8E8F4ABD2139C6D800E2FD66 /* Stack */, 8E8F4AB32138697600E2FD66 /* Linked List */, ); path = "Data Structures"; @@ -509,6 +514,7 @@ 8E8F4AB9213872F900E2FD66 /* Data Structures */ = { isa = PBXGroup; children = ( + 8E8F4AC02139C86700E2FD66 /* Stack */, 8E8F4ABA2138730100E2FD66 /* Linked List */, ); path = "Data Structures"; @@ -522,6 +528,22 @@ path = "Linked List"; sourceTree = ""; }; + 8E8F4ABD2139C6D800E2FD66 /* Stack */ = { + isa = PBXGroup; + children = ( + 8E8F4ABE2139C6E300E2FD66 /* Stack.swift */, + ); + path = Stack; + sourceTree = ""; + }; + 8E8F4AC02139C86700E2FD66 /* Stack */ = { + isa = PBXGroup; + children = ( + 8E8F4AC12139C87300E2FD66 /* StackTests.swift */, + ); + path = Stack; + sourceTree = ""; + }; B4EADB9A0901D69BD2E4AFA6 /* Frameworks */ = { isa = PBXGroup; children = ( @@ -789,6 +811,7 @@ 8E8F4AB52138698B00E2FD66 /* LinkedListNode.swift in Sources */, 205246D3209B9E8F0067A328 /* TransitioningNavigationView.swift in Sources */, 20524697209B9C250067A328 /* AppDelegate.swift in Sources */, + 8E8F4ABF2139C6E300E2FD66 /* Stack.swift in Sources */, 20524720209BB3240067A328 /* CameraPhotoLibraryPermissionsValidator.swift in Sources */, 205246F3209BA8B40067A328 /* UIViewControllerExtension.swift in Sources */, 205246F2209BA8B40067A328 /* UIFontExtension.swift in Sources */, @@ -799,6 +822,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 8E8F4AC22139C87300E2FD66 /* StackTests.swift in Sources */, 8E8F4ABC2138731500E2FD66 /* LinkedListTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/DevKit/DevKit/Classes/Data Structures/Stack/Stack.swift b/DevKit/DevKit/Classes/Data Structures/Stack/Stack.swift new file mode 100644 index 0000000..b61b8b0 --- /dev/null +++ b/DevKit/DevKit/Classes/Data Structures/Stack/Stack.swift @@ -0,0 +1,63 @@ +// +// Stack.swift +// DevKit +// +// Created by Thibault Klein on 8/31/18. +// Copyright © 2018 Jonathan Samudio. All rights reserved. +// + +import Foundation + +/// Stack. +/// A stack is a collection of elements following a First-In-Last-Out (FILO) order. +/// +/// - Note: +/// Use a Stack if you need a sequence ordered as FILO. +open class Stack { + // MARK: - Public Properties + + /// `true` if the stack is empty. `false` if not. + public var isEmpty: Bool { + return peek() == nil + } + + // MARK: - Private Properties + + private var storage: [T] = [] + + // MARK: - Initialization + + public init() { } + + // MARK: - Public Functions + // MARK: General Functions + + /// Returns the value at the top of the stack. + /// + /// - Complexity: O(1) + /// - Returns: The value at the top of the stack. + public func peek() -> T? { + return storage.last + } + + // MARK: Adding Functions + + /// Adds a value to the top of the stack. + /// + /// - Complexity: O(1) + /// - Parameter value: The value to add. + public func push(_ value: T) { + storage.append(value) + } + + // MARK: Removing Functions + + /// Returns the value at the top of the stack and removes it from it. + /// + /// - Complexity: O(1) + /// - Returns: The value at the top of the stack. + public func pop() -> T? { + return storage.popLast() + } + +} diff --git a/DevKit/DevKitTests/Classes/Data Structures/Stack/StackTests.swift b/DevKit/DevKitTests/Classes/Data Structures/Stack/StackTests.swift new file mode 100644 index 0000000..46abf47 --- /dev/null +++ b/DevKit/DevKitTests/Classes/Data Structures/Stack/StackTests.swift @@ -0,0 +1,49 @@ +// +// StackTests.swift +// DevKitTests +// +// Created by Thibault Klein on 8/31/18. +// Copyright © 2018 Jonathan Samudio. All rights reserved. +// + +import DevKit +import XCTest + +class StackTests: XCTestCase { + + func test_stack_isEmpty() { + // Given + let stack = Stack() + XCTAssertTrue(stack.isEmpty) + // When + stack.push(1) + // Then + XCTAssertFalse(stack.isEmpty) + } + + func test_stack_push() { + // Given + let stack = Stack() + // When + stack.push(1) + stack.push(2) + // Then + XCTAssertFalse(stack.isEmpty) + XCTAssertEqual(stack.peek(), 2) + } + + func test_stack_pop() { + // Given + let stack = Stack() + // When + stack.push(1) + stack.push(2) + stack.push(3) + let poppedValue = stack.pop() + // Then + XCTAssertFalse(stack.isEmpty) + XCTAssertEqual(poppedValue, 3) + XCTAssertEqual(stack.peek(), 2) + } + +}