Skip to content

Commit 991a536

Browse files
Merge pull request #3 from prolificinteractive/feature/stack-data-structure
Added stack in data structures
2 parents 99b6eef + c16618d commit 991a536

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

DevKit.podspec

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ Collection of commonly used swift code.
122122
sp.subspec 'LinkedList' do |ssp|
123123
ssp.source_files = 'DevKit/DevKit/Classes/Data\ Structures/Linked\ List/*'
124124
end
125+
126+
# Stack
127+
sp.subspec 'Stack' do |ssp|
128+
ssp.source_files = 'DevKit/DevKit/Classes/Data\ Structures/Stack/*'
129+
end
125130
end
126131

127132
end

DevKit/DevKit.xcodeproj/project.pbxproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
8E8F4AB52138698B00E2FD66 /* LinkedListNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E8F4AB42138698B00E2FD66 /* LinkedListNode.swift */; };
4646
8E8F4AB7213869E000E2FD66 /* LinkedList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E8F4AB6213869E000E2FD66 /* LinkedList.swift */; };
4747
8E8F4ABC2138731500E2FD66 /* LinkedListTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E8F4ABB2138731500E2FD66 /* LinkedListTests.swift */; };
48+
8E8F4ABF2139C6E300E2FD66 /* Stack.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E8F4ABE2139C6E300E2FD66 /* Stack.swift */; };
49+
8E8F4AC22139C87300E2FD66 /* StackTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E8F4AC12139C87300E2FD66 /* StackTests.swift */; };
4850
D7C6243C4F18537B1510C7A1 /* Pods_DevKitUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3939CE1C2FCEB6AE9D5E4974 /* Pods_DevKitUITests.framework */; };
4951
/* End PBXBuildFile section */
5052

@@ -112,6 +114,8 @@
112114
8E8F4AB42138698B00E2FD66 /* LinkedListNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkedListNode.swift; sourceTree = "<group>"; };
113115
8E8F4AB6213869E000E2FD66 /* LinkedList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkedList.swift; sourceTree = "<group>"; };
114116
8E8F4ABB2138731500E2FD66 /* LinkedListTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkedListTests.swift; sourceTree = "<group>"; };
117+
8E8F4ABE2139C6E300E2FD66 /* Stack.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Stack.swift; sourceTree = "<group>"; };
118+
8E8F4AC12139C87300E2FD66 /* StackTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StackTests.swift; sourceTree = "<group>"; };
115119
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 = "<group>"; };
116120
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 = "<group>"; };
117121
AA8FB83E8BE3E5509A2577E0 /* Pods_DevKitTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_DevKitTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -484,6 +488,7 @@
484488
8E8F4AB22138696E00E2FD66 /* Data Structures */ = {
485489
isa = PBXGroup;
486490
children = (
491+
8E8F4ABD2139C6D800E2FD66 /* Stack */,
487492
8E8F4AB32138697600E2FD66 /* Linked List */,
488493
);
489494
path = "Data Structures";
@@ -509,6 +514,7 @@
509514
8E8F4AB9213872F900E2FD66 /* Data Structures */ = {
510515
isa = PBXGroup;
511516
children = (
517+
8E8F4AC02139C86700E2FD66 /* Stack */,
512518
8E8F4ABA2138730100E2FD66 /* Linked List */,
513519
);
514520
path = "Data Structures";
@@ -522,6 +528,22 @@
522528
path = "Linked List";
523529
sourceTree = "<group>";
524530
};
531+
8E8F4ABD2139C6D800E2FD66 /* Stack */ = {
532+
isa = PBXGroup;
533+
children = (
534+
8E8F4ABE2139C6E300E2FD66 /* Stack.swift */,
535+
);
536+
path = Stack;
537+
sourceTree = "<group>";
538+
};
539+
8E8F4AC02139C86700E2FD66 /* Stack */ = {
540+
isa = PBXGroup;
541+
children = (
542+
8E8F4AC12139C87300E2FD66 /* StackTests.swift */,
543+
);
544+
path = Stack;
545+
sourceTree = "<group>";
546+
};
525547
B4EADB9A0901D69BD2E4AFA6 /* Frameworks */ = {
526548
isa = PBXGroup;
527549
children = (
@@ -789,6 +811,7 @@
789811
8E8F4AB52138698B00E2FD66 /* LinkedListNode.swift in Sources */,
790812
205246D3209B9E8F0067A328 /* TransitioningNavigationView.swift in Sources */,
791813
20524697209B9C250067A328 /* AppDelegate.swift in Sources */,
814+
8E8F4ABF2139C6E300E2FD66 /* Stack.swift in Sources */,
792815
20524720209BB3240067A328 /* CameraPhotoLibraryPermissionsValidator.swift in Sources */,
793816
205246F3209BA8B40067A328 /* UIViewControllerExtension.swift in Sources */,
794817
205246F2209BA8B40067A328 /* UIFontExtension.swift in Sources */,
@@ -799,6 +822,7 @@
799822
isa = PBXSourcesBuildPhase;
800823
buildActionMask = 2147483647;
801824
files = (
825+
8E8F4AC22139C87300E2FD66 /* StackTests.swift in Sources */,
802826
8E8F4ABC2138731500E2FD66 /* LinkedListTests.swift in Sources */,
803827
);
804828
runOnlyForDeploymentPostprocessing = 0;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// Stack.swift
3+
// DevKit
4+
//
5+
// Created by Thibault Klein on 8/31/18.
6+
// Copyright © 2018 Jonathan Samudio. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
/// Stack.
12+
/// A stack is a collection of elements following a First-In-Last-Out (FILO) order.
13+
///
14+
/// - Note:
15+
/// Use a Stack if you need a sequence ordered as FILO.
16+
open class Stack<T> {
17+
// MARK: - Public Properties
18+
19+
/// `true` if the stack is empty. `false` if not.
20+
public var isEmpty: Bool {
21+
return peek() == nil
22+
}
23+
24+
// MARK: - Private Properties
25+
26+
private var storage: [T] = []
27+
28+
// MARK: - Initialization
29+
30+
public init() { }
31+
32+
// MARK: - Public Functions
33+
// MARK: General Functions
34+
35+
/// Returns the value at the top of the stack.
36+
///
37+
/// - Complexity: O(1)
38+
/// - Returns: The value at the top of the stack.
39+
public func peek() -> T? {
40+
return storage.last
41+
}
42+
43+
// MARK: Adding Functions
44+
45+
/// Adds a value to the top of the stack.
46+
///
47+
/// - Complexity: O(1)
48+
/// - Parameter value: The value to add.
49+
public func push(_ value: T) {
50+
storage.append(value)
51+
}
52+
53+
// MARK: Removing Functions
54+
55+
/// Returns the value at the top of the stack and removes it from it.
56+
///
57+
/// - Complexity: O(1)
58+
/// - Returns: The value at the top of the stack.
59+
public func pop() -> T? {
60+
return storage.popLast()
61+
}
62+
63+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// StackTests.swift
3+
// DevKitTests
4+
//
5+
// Created by Thibault Klein on 8/31/18.
6+
// Copyright © 2018 Jonathan Samudio. All rights reserved.
7+
//
8+
9+
import DevKit
10+
import XCTest
11+
12+
class StackTests: XCTestCase {
13+
14+
func test_stack_isEmpty() {
15+
// Given
16+
let stack = Stack<Int>()
17+
XCTAssertTrue(stack.isEmpty)
18+
// When
19+
stack.push(1)
20+
// Then
21+
XCTAssertFalse(stack.isEmpty)
22+
}
23+
24+
func test_stack_push() {
25+
// Given
26+
let stack = Stack<Int>()
27+
// When
28+
stack.push(1)
29+
stack.push(2)
30+
// Then
31+
XCTAssertFalse(stack.isEmpty)
32+
XCTAssertEqual(stack.peek(), 2)
33+
}
34+
35+
func test_stack_pop() {
36+
// Given
37+
let stack = Stack<Int>()
38+
// When
39+
stack.push(1)
40+
stack.push(2)
41+
stack.push(3)
42+
let poppedValue = stack.pop()
43+
// Then
44+
XCTAssertFalse(stack.isEmpty)
45+
XCTAssertEqual(poppedValue, 3)
46+
XCTAssertEqual(stack.peek(), 2)
47+
}
48+
49+
}

0 commit comments

Comments
 (0)