dump
This commit is contained in:
commit
5626d1eacb
24 changed files with 512 additions and 0 deletions
7
vector/CMakeLists.txt
Normal file
7
vector/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
cmake_minimum_required(VERSION 3.16)
|
||||
project(vec)
|
||||
|
||||
add_executable(main main.cc vec.cc)
|
||||
|
||||
# Link the common library
|
||||
target_link_libraries(main PRIVATE common)
|
||||
4
vector/Makefile
Normal file
4
vector/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
TARGET = main.elf
|
||||
SRCS = vec.cc main.cc
|
||||
|
||||
include ../config.mk
|
||||
29
vector/main.cc
Normal file
29
vector/main.cc
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#include "iassert.h"
|
||||
#include "vec.h"
|
||||
|
||||
int main() {
|
||||
{
|
||||
Vec *v = new Vec();
|
||||
delete v;
|
||||
}
|
||||
|
||||
Vec v;
|
||||
v.push_back(10);
|
||||
ASSERT_MSG(v[0] == 10, "10 should be present at index 0");
|
||||
|
||||
Vec v2{1, 2, 3};
|
||||
ASSERT_MSG(v2.size() == 3, "Size should be three");
|
||||
v2.pop_back();
|
||||
ASSERT_MSG(v2.size() == 2, "Size should be three");
|
||||
|
||||
// Triggers a reallocation (memcpy)
|
||||
Vec v3{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
|
||||
ASSERT_MSG(v3[11] == 12, "Index 11 should hold 12");
|
||||
// ASSERT_MSG(v3[5] == 6, "Index 5 should hold 6"); // ???
|
||||
ASSERT_MSG(v3.size() == 12, "Size should be 12");
|
||||
ASSERT_MSG(v3.capacity() == 20, "Capacity should be 20");
|
||||
|
||||
for (const auto &a : v3) {
|
||||
std::cout << a << std::endl;
|
||||
}
|
||||
}
|
||||
60
vector/vec.cc
Normal file
60
vector/vec.cc
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
#include "vec.h"
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
Vec::Vec() : cap(INITIAL_LEN), w_idx(0), arr(nullptr) {
|
||||
std::cout << "Allocating vector" << std::endl;
|
||||
arr = new T[INITIAL_LEN];
|
||||
}
|
||||
|
||||
Vec::Vec(std::initializer_list<T> il) : Vec() {
|
||||
for (auto &a : il) push_back(a);
|
||||
}
|
||||
|
||||
Vec::~Vec() {
|
||||
std::cout << "De-allocating vector" << std::endl;
|
||||
|
||||
if (arr)
|
||||
delete[] arr;
|
||||
}
|
||||
|
||||
T &Vec::operator[](std::size_t idx) const noexcept {
|
||||
if (idx < w_idx)
|
||||
return arr[idx];
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void Vec::push_back(const T &value) noexcept {
|
||||
arr[w_idx++] = value;
|
||||
|
||||
if (w_idx == cap) {
|
||||
resize(cap * 2);
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t Vec::size() const { return w_idx; }
|
||||
|
||||
std::size_t Vec::capacity() const noexcept { return cap; }
|
||||
|
||||
void Vec::resize(std::size_t newsize) {
|
||||
std::cout << "Reallocating" << std::endl;
|
||||
T *newarr = new T[newsize];
|
||||
std::memcpy(newarr, arr, w_idx);
|
||||
delete[] arr;
|
||||
arr = newarr;
|
||||
cap = newsize;
|
||||
}
|
||||
|
||||
void Vec::pop_back() noexcept { --w_idx; }
|
||||
|
||||
void Vec::clear() noexcept {
|
||||
#ifdef REALCLEAR
|
||||
delete[] arr;
|
||||
arr = new T[INITIAL_LEN];
|
||||
cap = INITIAL_LEN;
|
||||
#endif
|
||||
|
||||
w_idx = 0;
|
||||
}
|
||||
43
vector/vec.h
Normal file
43
vector/vec.h
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#include <initializer_list>
|
||||
#include <iterator>
|
||||
|
||||
typedef int T;
|
||||
constexpr int INITIAL_LEN = 10;
|
||||
|
||||
class Vec {
|
||||
std::size_t cap;
|
||||
std::size_t w_idx;
|
||||
T *arr = nullptr;
|
||||
|
||||
void resize(std::size_t newsize);
|
||||
|
||||
public:
|
||||
Vec();
|
||||
Vec(std::initializer_list<T>);
|
||||
Vec(const Vec &other); // Copy
|
||||
Vec(Vec &&other) noexcept; // Move
|
||||
Vec &operator=(const Vec &other); // CopyAssign
|
||||
Vec &operator=(Vec &&other) noexcept; // MoveAssign
|
||||
~Vec(); // Destructor
|
||||
|
||||
T &operator[](std::size_t) const noexcept;
|
||||
|
||||
std::size_t size() const;
|
||||
std::size_t capacity() const noexcept;
|
||||
|
||||
void push_back(const T &value) noexcept;
|
||||
void pop_back() noexcept;
|
||||
void clear() noexcept;
|
||||
|
||||
using iterator = T *;
|
||||
using const_iterator = const T *;
|
||||
|
||||
iterator begin() noexcept { return arr; }
|
||||
iterator end() noexcept { return arr + w_idx; }
|
||||
|
||||
const_iterator begin() const noexcept { return arr; }
|
||||
const_iterator end() const noexcept { return arr + w_idx; }
|
||||
};
|
||||
|
||||
// std::iterator<Vec> end() const noexcept;
|
||||
// std::iterator begin() const noexcept;
|
||||
Loading…
Add table
Add a link
Reference in a new issue