Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bazel-*
10 changes: 10 additions & 0 deletions 10 BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cc_library(
name = "build_header",
hdrs = [
"ali_vector.h"
],
copts = [
"-std=c++2a",
],
visibility = ["//visibility:public"]
)
8 changes: 8 additions & 0 deletions 8 README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
# vector

My implementation of std::vector

## How to test?

<div>
<pre><code>$ bazel test tests:ali_vector_test --repo_env=CC=c++
</code></pre>
</div>
7 changes: 7 additions & 0 deletions 7 WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
name = "googletest",
remote = "https://github.com/google/googletest",
tag = "release-1.10.0",
)
205 changes: 205 additions & 0 deletions 205 ali_vector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
//
// Created by Ali Moameri on 4/10/22.
//

#ifndef ALI_VECTOR_H
#define ALI_VECTOR_H

#include <cstddef>
#include <exception>
#include <algorithm>
#include <memory>

namespace ali {
template <typename T> class vector {
public:
vector()
: m_size{0}, m_capacity{0}, ptr{nullptr} {}

explicit vector(const std::size_t& _size)
: m_size{_size}, m_capacity{_size * 2} {
ptr = m_alloc.allocate(m_capacity);
}

explicit vector(const std::size_t& _size, const T& value)
: m_size{_size}, m_capacity{_size * 2} {
ptr = m_alloc.allocate(m_capacity);
for(std::size_t i{0}; i < m_size; ++i)
m_alloc.construct(ptr + i, value);
}

vector(const std::initializer_list<T>& ls)
: m_size{ls.size()}, m_capacity{ls.size() * 2} {
ptr = m_alloc.allocate(m_capacity);
for(std::size_t i{0}; i < ls.size(); ++i)
m_alloc.construct(ptr + i, *(ls.begin() + i));
}

vector(const ali::vector<T>& rhs)
:m_size{rhs.size()}, m_capacity{rhs.capacity()} {
ptr = m_alloc.allocate(m_capacity);
for(std::size_t i{0}; i < rhs.size(); ++i)
m_alloc.construct(ptr + i, rhs[i]);
}

vector(ali::vector<T>&& rhs) noexcept
:m_size{rhs.size()}, m_capacity{rhs.capacity()} {
ptr = m_alloc.allocate(m_capacity);
for(std::size_t i{0}; i < rhs.size(); ++i)
m_alloc.construct(ptr + i, rhs[i]);
rhs.clear();
m_capacity = 0;
}

~vector() { m_alloc.deallocate(ptr, m_capacity); }

[[nodiscard]] constexpr std::size_t size() const noexcept { return m_size; }
[[nodiscard]] constexpr std::size_t capacity() const noexcept { return m_capacity; }

void clear() noexcept {
delete[] ptr;
ptr = nullptr;
m_size = 0;
}

void push_back(const T& value) {
if(m_capacity == 0)
reserve(8);
else if(m_size == m_capacity)
reserve(m_size * 2);
ptr[m_size++] = value;
}


void pop_back() {
m_size = m_size > 0 ? m_size - 1 : m_size;
}

T& at(std::size_t index) {
if (index < m_size)
return ptr[index];
else
throw std::out_of_range("index out of range");
}

const T& at(std::size_t index) const {
if (index < m_size)
return ptr[index];
else
throw std::out_of_range("index out of range");
}

void shrink_to_fit() {
if (m_size < m_capacity) {
T* new_ptr{m_alloc.allocate(m_size)};
for(std::size_t i{0}; i < m_size; ++i)
m_alloc.construct(new_ptr + i, ptr[i]);
for(std::size_t i{0}; i < m_size; ++i)
m_alloc.destroy(ptr + i);
m_alloc.deallocate(ptr, m_capacity);
ptr = new_ptr;
m_capacity = m_size;
}
}

void reserve(std::size_t space) {
if (space > m_capacity) {
T* new_ptr{m_alloc.allocate(space)};

for(std::size_t i{0}; i < m_size; ++i)
m_alloc.construct(new_ptr + i, ptr[i]);
for(std::size_t i{0}; i < m_size; ++i)
m_alloc.destroy(ptr + i);

m_alloc.deallocate(ptr, m_capacity);
ptr = new_ptr;
m_capacity = space;
}
}

[[nodiscard]] bool empty() const { return size() == 0; }

void swap(ali::vector<T>& rhs) {
std::swap(m_size, rhs.m_size);
std::swap(m_capacity, rhs.m_capacity);
std::swap(m_alloc, rhs.m_alloc);
std::swap(ptr, rhs.ptr);
}

T& operator[](const std::size_t& index) {
if(index >= size())
throw std::out_of_range("out of range index");
else
return ptr[index];
}

const T& operator[](const std::size_t& index) const {
if(index >= size())
throw std::out_of_range("out of range index");
else
return ptr[index];
}

ali::vector<T>& operator=(const ali::vector<T>& rhs) noexcept {
if(this == &rhs)
return *this;

if(rhs.size() <= m_capacity) {
std::copy(rhs.ptr, rhs.ptr + rhs.size(), ptr);
m_size = rhs.size();
return *this;
}

T* new_ptr{m_alloc.allocate(rhs.size())};
for(std::size_t i{0}; i < rhs.size(); ++i)
m_alloc.construct(new_ptr + i, rhs[i]);
for(std::size_t i{0}; i < m_size; ++i)
m_alloc.destroy(ptr + i);
m_alloc.deallocate(ptr, m_capacity);
ptr = new_ptr;
m_capacity = m_size = rhs.size();
return *this;
}

ali::vector<T>& operator=(ali::vector<T>&& rhs) noexcept {
if(this == &rhs)
return *this;

if(rhs.size() <= m_capacity) {
std::copy(rhs.ptr, rhs.ptr + rhs.size(), ptr);
m_size = rhs.size();
} else {
T* new_ptr{m_alloc.allocate(rhs.size())};
for(std::size_t i{0}; i < rhs.size(); ++i)
m_alloc.construct(new_ptr + i, rhs[i]);
for(std::size_t i{0}; i < m_size; ++i)
m_alloc.destroy(ptr + i);
m_alloc.deallocate(ptr, m_capacity);
ptr = new_ptr;
m_capacity = m_size = rhs.size();
}

rhs.clear();
rhs.m_capacity = 0;
return *this;
}

bool operator==(const vector &rhs) const {
if (size() != rhs.size())
return false;
for (std::size_t i{0}; i < size(); ++i) {
if (ptr[i] != rhs[i])
return false;
}
return true;
}

private:
std::size_t m_size{0};
std::size_t m_capacity{0};
std::allocator<T> m_alloc;
T* ptr{nullptr};
};
}

#endif //ALI_VECTOR_H
11 changes: 11 additions & 0 deletions 11 tests/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cc_test(
name = "ali_vector_test",
srcs = glob(["**/*.cc"]),
copts = [
"-std=c++2a",
],
deps = [
"//:build_header",
"@googletest//:gtest_main",
],
)
99 changes: 99 additions & 0 deletions 99 tests/ali_vector_ptr_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "ali_vector.h"
#include <cctype>
#include <gtest/gtest.h>

struct T {
int x;

T(const int input) : x(input)
{}
};

constexpr std::size_t size {10};
constexpr std::size_t capacity {size*2};

TEST(VectorTest, DefaultConstructor) {
ali::vector<T> v;
EXPECT_EQ(v.size(), 0);
EXPECT_EQ(v.capacity(), 0);
}

TEST(VectorTest, Constructors) {
ali::vector<T> v(size);
EXPECT_EQ(v.size(), size);
EXPECT_EQ(v.capacity(), capacity);

ali::vector<T> v2(size, 6);
EXPECT_EQ(v2.size(), size);
EXPECT_EQ(v2.capacity(), capacity);
EXPECT_EQ(v2[5].x, 6);

ali::vector<T> v3{1, 2 ,3 ,4};
EXPECT_EQ(v3.size(), 4);
EXPECT_EQ(v3[1].x, 2);
}

TEST(VectorTest, CopyConstructor) {
ali::vector<int> v{1, 2, 3};
ali::vector<int> v2{v};
EXPECT_EQ(v == v2, true);
}

TEST(VectorTest, MoveConstructor) {
ali::vector<T> v{1, 2, 3};
ali::vector<T> v2{std::move(v)};
EXPECT_EQ(v.empty(), true);
EXPECT_EQ(v2[1].x, 2);
}

TEST(VectorTest, Clear) {
ali::vector<T> v{1, 2, 3};
v.clear();
EXPECT_EQ(v.size(), 0);
}

TEST(VectorTest, PushBack) {
ali::vector<T> v{1, 2, 3};
v.push_back(T{4});
EXPECT_EQ(v.size(), 4);
EXPECT_EQ(v[3].x, 4);
}

TEST(VectorTest, PopBack) {
ali::vector<T> v{1, 2, 3};
v.pop_back();
EXPECT_EQ(v.size(), 2);
}

TEST(VectorTest, Empty) {
ali::vector<T> v{1, 2, 3};
ali::vector<T> v2;
EXPECT_EQ(v.empty(), false);
EXPECT_EQ(v2.empty(), true);
}

TEST(VectorTest, Assigment) {
ali::vector<T> v{1, 2, 3};
ali::vector<T> v2;
v2 = v;
for(std::size_t i{0}; i < v.size(); ++i)
EXPECT_EQ(v[i].x, v2[i].x);
}

TEST(VectorTest, MoveAssigment) {
ali::vector<T> v{1, 2, 3};
ali::vector<T> v2{v};
ali::vector<T> v3;
v3 = std::move(v);

for(std::size_t i{0}; i < v3.size(); ++i)
EXPECT_EQ(v3[i].x, v2[i].x);

EXPECT_EQ(v.empty(), true);
}

TEST(VectorTest, ConstDerefence) {
const ali::vector<T> v{1, 2, 3};
EXPECT_EQ(v[2].x, 3);
}

Morty Proxy This is a proxified and sanitized view of the page, visit original site.