Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarfovich committed Sep 14, 2024
1 parent 799be5b commit 721ada6
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 88 deletions.
102 changes: 56 additions & 46 deletions HW3/cc_lib/include/cc/forward_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <type_traits>
#include <cinttypes>

#include <list>

namespace cc {

namespace details {
Expand Down Expand Up @@ -34,37 +32,25 @@ class ForwardListIterator
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = typename ForwardList::value_type;
using pointer = typename ForwardList::pointer;
using reference = typename ForwardList::reference;
using pointer = typename ForwardList::pointer;
using reference = typename ForwardList::reference;

public: // methods
ForwardListIterator() = default;
ForwardListIterator(const ForwardList* list);
ForwardListIterator(const ForwardList* list, details::Node* node);
//ForwardListIterator(ForwardList* list, details::Node* node);

operator ForwardListIterator<const ForwardList>() const
{
return {list, node};
}


const ForwardList* getList() const{
return list;
}
bool operator==(const ForwardListIterator<const ForwardList>& rhs) const
{
return node == rhs.getNode() && list == rhs.getList();
}
//bool operator==(const ForwardListIterator<ForwardList>& rhs) { return node == rhs.node && list == rhs.list; }
operator ForwardListIterator<const ForwardList>() const;

//auto operator<=>(const ForwardListIterator&) const = default;
const value_type& operator*() const { return static_cast<details::DataNode<ForwardList::value_type>*>(node)->data; }
value_type& operator*() { return static_cast<details::DataNode<ForwardList::value_type>*>(node)->data; }
bool operator==(const ForwardListIterator<const ForwardList>& rhs) const;
auto operator<=>(const ForwardListIterator&) const = default;
const value_type& operator*() const;
value_type& operator*();
ForwardListIterator& operator++();
ForwardListIterator operator++(int);

details::Node* getNode() const noexcept { return node; }
const ForwardList* getList() const noexcept;
details::Node* getNode() const noexcept;

private:
details::Node* node = nullptr;
Expand Down Expand Up @@ -96,27 +82,20 @@ class ForwardList
const_iterator cend() const noexcept;
bool empty() const noexcept;
std::size_t size() noexcept;
iterator insert_after(iterator position, const value_type& value);
iterator erase_after(iterator position);
iterator insert_after(const_iterator position, const value_type& value);
iterator erase_after(const_iterator position);

private: // data
Allocator allocator;
details::Node head;
std::size_t listSize = 0;
};

//template<typename T, typename U>
//bool operator==(const ForwardListIterator<ForwardList<T>>& lhs, const ForwardListIterator<ForwardList<U>>& rhs)
//{
// return lhs.node == rhs.node && lhs.list == rhs.list;
//}

//template<typename T, typename U>
//bool operator==(const ForwardListIterator<const ForwardList<T>>& lhs,
// const ForwardListIterator<const ForwardList<U>>& rhs)
//{
// return lhs.node == rhs.node && lhs.list == rhs.list;
//}
template<class ForwardList>
bool operator==(const ForwardListIterator<ForwardList>& lhs, const ForwardListIterator<ForwardList>& rhs)
{
return lhs.getNode() == rhs.getNode() && lhs.getList() == rhs.getList();
}

template<class ForwardList>
ForwardListIterator<ForwardList>& ForwardListIterator<ForwardList>::operator++()
Expand Down Expand Up @@ -144,15 +123,49 @@ ForwardListIterator<ForwardList>::ForwardListIterator(const ForwardList* aList,
{
}

template<class ForwardList>
bool ForwardListIterator<ForwardList>::operator==(const ForwardListIterator<const ForwardList>& rhs) const
{
return node == rhs.getNode() && list == rhs.getList();
}

template<class ForwardList>
auto ForwardListIterator<ForwardList>::operator*() const -> const value_type&
{
return static_cast<details::DataNode<ForwardList::value_type>*>(node)->data;
}

template<class ForwardList>
auto ForwardListIterator<ForwardList>::operator*() -> value_type&
{
return static_cast<details::DataNode<ForwardList::value_type>*>(node)->data;
}

template<class ForwardList>
ForwardListIterator<ForwardList>::operator ForwardListIterator<const ForwardList>() const
{
return { list, node };
}

template<class ForwardList>
const ForwardList* ForwardListIterator<ForwardList>::getList() const noexcept
{
return list;
}

template<class ForwardList>
details::Node* ForwardListIterator<ForwardList>::getNode() const noexcept
{
return node;
}

template<typename T, typename Allocator>
ForwardList<T, Allocator>::ForwardList()
{
}

template<typename T, typename Allocator>
auto ForwardList<T, Allocator>::insert_after(iterator position, const value_type& value) -> iterator
auto ForwardList<T, Allocator>::insert_after(const_iterator position, const value_type& value) -> iterator
{
using namespace details;
using NodeAllcoator = typename std::allocator_traits<Allocator>::template rebind_alloc<DataNode<T>>;
Expand All @@ -173,10 +186,10 @@ auto ForwardList<T, Allocator>::insert_after(iterator position, const value_type
}

template<typename T, typename Allocator>
auto ForwardList<T, Allocator>::erase_after(iterator position) -> iterator
auto ForwardList<T, Allocator>::erase_after(const_iterator position) -> iterator
{
auto iterToErase = std::next(position);
auto nextIter = std::next(iterToErase);
auto iterToErase = std::next(position);
auto nextIter = std::next(iterToErase);
using namespace details;
using NodeAllcoator = typename std::allocator_traits<Allocator>::template rebind_alloc<DataNode<T>>;
using NodeAllocatorTraits = std::allocator_traits<NodeAllcoator>;
Expand All @@ -203,10 +216,7 @@ auto ForwardList<T, Allocator>::before_begin() noexcept -> iterator
template<typename T, typename Allocator>
auto ForwardList<T, Allocator>::cbefore_begin() const noexcept -> const_iterator
{
return {};
// return { this, &head };
//return ForwardListIterator<decltype(this)>{ this, &head };
//return { this, &head };
return { this, const_cast<details::Node*>(&head) };
}

template<typename T, typename Allocator>
Expand All @@ -230,7 +240,7 @@ auto ForwardList<T, Allocator>::end() noexcept -> iterator
template<typename T, typename Allocator>
auto ForwardList<T, Allocator>::cend() const noexcept -> const_iterator
{
return end();
return { this };
}

template<typename T, typename Allocator>
Expand Down
28 changes: 9 additions & 19 deletions HW3/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
#include <forward_list>

template<typename T>
T factorial(T n){
if(n < 0){
T factorial(T n)
{
if (n < 0) {
return 0;
}

if(n == 0 || n == 1){
if (n == 0 || n == 1) {
return 1;
}

T f = 1;
for(T i = 2; i <= n; ++i) {
for (T i = 2; i <= n; ++i) {
f *= i;
}

Expand All @@ -27,16 +28,16 @@ T factorial(T n){
int main()
{
std::map<int, int> map;
for (int i = 0; i < 10; ++i){
map[i]= factorial(i);
for (int i = 0; i < 10; ++i) {
map[i] = factorial(i);
}
std::cout << "Map with std::allocator:\n";
for (const auto& [key, value] : map) {
std::cout << key << ": " << value << '\n';
}

using ValueType = std::map<int, int>::value_type;
using MMA = MemoryManagerAllocator<ValueType>;
using MMA = MemoryManagerAllocator<ValueType>;
std::map<int, int, std::less<>, MMA> mmamap;
for (int i = 0; i < 10; ++i) {
mmamap[i] = factorial(i);
Expand All @@ -47,18 +48,7 @@ int main()
}

cc::ForwardList<int> list;
if (list.cbegin() == list.begin() && list.begin() == list.cbegin()){
std::cout << "Yes\n";
}
//auto iter = list.begin();
//list.insert_after(iter, 77);

std::forward_list<short> l;
l.insert_after(l.before_begin(), 88);
/*std::cout << std::boolalpha << (l.cbegin() == l.cbefore_begin()) << '\n';
std::cout << sizeof(list) << ' ' << sizeof(l) << '\n';
std::forward_list<int> l2;
std::cout << std::boolalpha << (l.cbegin() == l.cend()) << '\n';*/


return 0;
}
Loading

0 comments on commit 721ada6

Please sign in to comment.