-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fae1cf5
commit c16618d
Showing
4 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T> { | ||
// 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() | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
DevKit/DevKitTests/Classes/Data Structures/Stack/StackTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Int>() | ||
XCTAssertTrue(stack.isEmpty) | ||
// When | ||
stack.push(1) | ||
// Then | ||
XCTAssertFalse(stack.isEmpty) | ||
} | ||
|
||
func test_stack_push() { | ||
// Given | ||
let stack = Stack<Int>() | ||
// When | ||
stack.push(1) | ||
stack.push(2) | ||
// Then | ||
XCTAssertFalse(stack.isEmpty) | ||
XCTAssertEqual(stack.peek(), 2) | ||
} | ||
|
||
func test_stack_pop() { | ||
// Given | ||
let stack = Stack<Int>() | ||
// 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) | ||
} | ||
|
||
} |