Skip to content

Commit

Permalink
update Array
Browse files Browse the repository at this point in the history
  • Loading branch information
bitdove committed Sep 29, 2024
1 parent 7c79002 commit 3106be6
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
matrix:
# Add your projects from 'data-structures'
project: [LinkedList, DoublyLinkedList, Queue, Stack]
project: [Array, LinkedList, DoublyLinkedList, Queue, Stack]

steps:
# Step 1: Checkout the repository
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Remember that each data has its own trade-offs. And you need to pay attention mo

`B` - Beginner, `A` - Advanced

* `B` [Array](data-structures/Array)
* `B` [Linked List](data-structures/LinkedList)
* `B` [Doubly Linked List](data-structures/DoublyLinkedList)
* `B` [Queue](data-structures/Queue)
Expand Down
1 change: 1 addition & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

`B` - 初学者, `A` - 进阶

* `B` [数组](data-structures/Array)
* `B` [单链表](data-structures/LinkedList)
* `B` [双链表](data-structures/DoublyLinkedList)
* `B` [队列](data-structures/Queue)
Expand Down
21 changes: 21 additions & 0 deletions data-structures/Array/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Specify the minimum version of CMake required
cmake_minimum_required(VERSION 3.10)

# Project name and version
project(ArrayProject VERSION 1.0)

# Set C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# Include directories
include_directories(include)

# Output executables to the 'bin' directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../bin)

# Add the test executable
add_executable(test_Array __test__/test_Array.cpp)

# Link the Array library to the test executable
target_link_libraries(test_Array)
3 changes: 3 additions & 0 deletions data-structures/Array/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 数组

![数组](./assets/array.png)
49 changes: 49 additions & 0 deletions data-structures/Array/__test__/test_Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "../include/Array.h"
#include <cassert>
#include <iostream>

void test_array() {
Array<int, 0> arr_0;
assert(arr_0.size() == 0);
assert(arr_0.empty() == true);
try{
arr_0.front();
} catch(const std::underflow_error &e){
std::cout << "front() throw an exception PASSED! " << e.what() << std::endl; // Expected
}
try{
arr_0.back();
} catch(const std::underflow_error &e){
std::cout << "back() throw an exception PASSED! " << e.what() << std::endl; // Expected
}

Array<int, 5> arr_5;
assert(arr_5.size() == 5);
assert(arr_5.empty() == false);
try {
assert(arr_5[0] == 0);
assert(arr_5.at(4) == 0);
arr_5.at(5);
} catch (const std::out_of_range& e) {
std::cout << "at() throw an exception PASSED! " << e.what() << std::endl; // Expected
}
for (int i = 0; i < 5; ++i) {
arr_5[i] = i + 1;
}
assert(arr_5.front() == 1);
assert(arr_5.back() == 5);
assert(arr_5.at(2) == 3);
assert(arr_5[3] == 4);
arr_5[3] = 9;
assert(arr_5[3] == 9);
for(int i = 0; i < arr_5.size(); ++i){
std::cout << arr_5[i] << " ";
}
std::cout << std::endl;
std::cout << "All tests passed!" << std::endl;
}

int main() {
test_array();
return 0;
}
Binary file added data-structures/Array/assets/array.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions data-structures/Array/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# Create the build directory if it doesn't exist
if [ ! -d "build" ]; then
mkdir build
fi

# Run CMake in the build directory
cd build
cmake ..

# Build the project using make
make

# Return to the project root directory
cd ..

# Inform the user where the executable can be found
echo "Build complete. Executable located in the ./bin directory."
7 changes: 7 additions & 0 deletions data-structures/Array/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# Remove build and bin directories
rm -rf build
rm -rf bin

echo "Clean complete. Build and bin directories removed."
63 changes: 63 additions & 0 deletions data-structures/Array/include/Array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifndef ARRAY_H
#define ARRAY_H

#include <cstddef>
#include <stdexcept>

template <typename T, size_t N>
class Array{
public:
Array();
~Array() = default;
public:
// Capacity
bool empty() const {return N == 0;}
size_t size() const {return N;}
// Element access
T& front();
T& back();
T& at(const size_t index);
T& operator[](const size_t index);
private:
T _data[N];
};

// Element access
template <typename T, size_t N>
T& Array<T, N>::front(){
if(N == 0){
throw std::underflow_error("front(): This Array is empty.");
}
return _data[0];
}

template <typename T, size_t N>
T& Array<T, N>::back(){
if(N == 0){
throw std::underflow_error("back(): This Array is empty.");
}
return _data[N - 1];
}

template <typename T, size_t N>
T& Array<T, N>::at(const size_t index){
if(index >= N){
throw std::out_of_range("at(): Index out of range.");
}
return _data[index];
}

template <typename T, size_t N>
T& Array<T, N>::operator[](const size_t index){
return _data[index];
}

// Constructor
template <typename T, size_t N>
Array<T, N>::Array(){
for(size_t i = 0; i < N; ++i){
_data[i] = T();
}
}

#endif // ARRAY_H
2 changes: 1 addition & 1 deletion data-structures/Queue/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../bin)
# Add the test executable
add_executable(test_Queue __test__/test_Queue.cpp)

# Link the LinkedList library to the test executable
# Link the Queue library to the test executable
target_link_libraries(test_Queue)
2 changes: 1 addition & 1 deletion data-structures/Stack/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../bin)
# Add the test executable
add_executable(test_Stack __test__/test_Stack.cpp)

# Link the LinkedList library to the test executable
# Link the Stack library to the test executable
target_link_libraries(test_Stack)

0 comments on commit 3106be6

Please sign in to comment.