Skip to content

Commit 3106be6

Browse files
committed
update Array
1 parent 7c79002 commit 3106be6

File tree

12 files changed

+167
-3
lines changed

12 files changed

+167
-3
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
strategy:
1717
matrix:
1818
# Add your projects from 'data-structures'
19-
project: [LinkedList, DoublyLinkedList, Queue, Stack]
19+
project: [Array, LinkedList, DoublyLinkedList, Queue, Stack]
2020

2121
steps:
2222
# Step 1: Checkout the repository

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Remember that each data has its own trade-offs. And you need to pay attention mo
2121

2222
`B` - Beginner, `A` - Advanced
2323

24+
* `B` [Array](data-structures/Array)
2425
* `B` [Linked List](data-structures/LinkedList)
2526
* `B` [Doubly Linked List](data-structures/DoublyLinkedList)
2627
* `B` [Queue](data-structures/Queue)

README.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

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

21+
* `B` [数组](data-structures/Array)
2122
* `B` [单链表](data-structures/LinkedList)
2223
* `B` [双链表](data-structures/DoublyLinkedList)
2324
* `B` [队列](data-structures/Queue)

data-structures/Array/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Specify the minimum version of CMake required
2+
cmake_minimum_required(VERSION 3.10)
3+
4+
# Project name and version
5+
project(ArrayProject VERSION 1.0)
6+
7+
# Set C++ standard
8+
set(CMAKE_CXX_STANDARD 11)
9+
set(CMAKE_CXX_STANDARD_REQUIRED True)
10+
11+
# Include directories
12+
include_directories(include)
13+
14+
# Output executables to the 'bin' directory
15+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../bin)
16+
17+
# Add the test executable
18+
add_executable(test_Array __test__/test_Array.cpp)
19+
20+
# Link the Array library to the test executable
21+
target_link_libraries(test_Array)

data-structures/Array/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 数组
2+
3+
![数组](./assets/array.png)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "../include/Array.h"
2+
#include <cassert>
3+
#include <iostream>
4+
5+
void test_array() {
6+
Array<int, 0> arr_0;
7+
assert(arr_0.size() == 0);
8+
assert(arr_0.empty() == true);
9+
try{
10+
arr_0.front();
11+
} catch(const std::underflow_error &e){
12+
std::cout << "front() throw an exception PASSED! " << e.what() << std::endl; // Expected
13+
}
14+
try{
15+
arr_0.back();
16+
} catch(const std::underflow_error &e){
17+
std::cout << "back() throw an exception PASSED! " << e.what() << std::endl; // Expected
18+
}
19+
20+
Array<int, 5> arr_5;
21+
assert(arr_5.size() == 5);
22+
assert(arr_5.empty() == false);
23+
try {
24+
assert(arr_5[0] == 0);
25+
assert(arr_5.at(4) == 0);
26+
arr_5.at(5);
27+
} catch (const std::out_of_range& e) {
28+
std::cout << "at() throw an exception PASSED! " << e.what() << std::endl; // Expected
29+
}
30+
for (int i = 0; i < 5; ++i) {
31+
arr_5[i] = i + 1;
32+
}
33+
assert(arr_5.front() == 1);
34+
assert(arr_5.back() == 5);
35+
assert(arr_5.at(2) == 3);
36+
assert(arr_5[3] == 4);
37+
arr_5[3] = 9;
38+
assert(arr_5[3] == 9);
39+
for(int i = 0; i < arr_5.size(); ++i){
40+
std::cout << arr_5[i] << " ";
41+
}
42+
std::cout << std::endl;
43+
std::cout << "All tests passed!" << std::endl;
44+
}
45+
46+
int main() {
47+
test_array();
48+
return 0;
49+
}
60.3 KB
Loading

data-structures/Array/build.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
# Create the build directory if it doesn't exist
4+
if [ ! -d "build" ]; then
5+
mkdir build
6+
fi
7+
8+
# Run CMake in the build directory
9+
cd build
10+
cmake ..
11+
12+
# Build the project using make
13+
make
14+
15+
# Return to the project root directory
16+
cd ..
17+
18+
# Inform the user where the executable can be found
19+
echo "Build complete. Executable located in the ./bin directory."

data-structures/Array/clean.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
# Remove build and bin directories
4+
rm -rf build
5+
rm -rf bin
6+
7+
echo "Clean complete. Build and bin directories removed."

data-structures/Array/include/Array.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#ifndef ARRAY_H
2+
#define ARRAY_H
3+
4+
#include <cstddef>
5+
#include <stdexcept>
6+
7+
template <typename T, size_t N>
8+
class Array{
9+
public:
10+
Array();
11+
~Array() = default;
12+
public:
13+
// Capacity
14+
bool empty() const {return N == 0;}
15+
size_t size() const {return N;}
16+
// Element access
17+
T& front();
18+
T& back();
19+
T& at(const size_t index);
20+
T& operator[](const size_t index);
21+
private:
22+
T _data[N];
23+
};
24+
25+
// Element access
26+
template <typename T, size_t N>
27+
T& Array<T, N>::front(){
28+
if(N == 0){
29+
throw std::underflow_error("front(): This Array is empty.");
30+
}
31+
return _data[0];
32+
}
33+
34+
template <typename T, size_t N>
35+
T& Array<T, N>::back(){
36+
if(N == 0){
37+
throw std::underflow_error("back(): This Array is empty.");
38+
}
39+
return _data[N - 1];
40+
}
41+
42+
template <typename T, size_t N>
43+
T& Array<T, N>::at(const size_t index){
44+
if(index >= N){
45+
throw std::out_of_range("at(): Index out of range.");
46+
}
47+
return _data[index];
48+
}
49+
50+
template <typename T, size_t N>
51+
T& Array<T, N>::operator[](const size_t index){
52+
return _data[index];
53+
}
54+
55+
// Constructor
56+
template <typename T, size_t N>
57+
Array<T, N>::Array(){
58+
for(size_t i = 0; i < N; ++i){
59+
_data[i] = T();
60+
}
61+
}
62+
63+
#endif // ARRAY_H

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy