#ifndef GC_HPP
#define GC_HPP


#include <cstddef>
#include <cassert>
#include <vector>
#include <memory>


template <class T> class gc_collection;
class gc_ptr_base;
class gc_object;
template <class T> class gc_member_ptr;


//collection node; must be part of the object (the collections are intrusive).
template <class T> class gc_collection_node {
public:
    //not allowed
    gc_collection_node(const gc_collection_node<T> &) = delete;

    //not allowed
    gc_collection_node(gc_collection_node<T> &&) = delete;

    //not allowed
    gc_collection_node<T> &operator = (const gc_collection_node<T> &) = delete;

    //not allowed
    gc_collection_node<T> &operator = (gc_collection_node<T> &n) = delete;

protected:
    //the default constructor
    gc_collection_node() {
        assert((m_collection = nullptr) == nullptr);
    }

    //does nothing
    ~gc_collection_node() {
        assert(m_collection == nullptr);
    }

private:
    //pointer to the collection
    gc_collection<T> *m_collection;

    //index into the collection
    size_t m_collection_index;

    friend class gc_collection<T>;
    friend class gc;
    friend class gc_ptr_base;
    friend class gc_object;
};


//collection of items.
template <class T> class gc_collection {
public:

    //add an object
    void add(T *object) {
        assert(object->m_collection == nullptr);
        object->m_collection = this;
        object->m_collection_index = m_objects.size() - 1;
        m_objects.push_back(object);
    }

    //remove an object
    void remove(T *object) {
        assert(object->m_collection == this);
        if (m_objects.size() > 1) {
            T *last = m_objects.back();
            if (last != object) {
                m_objects[object->m_collection_index] = last;
                last->m_collection_index = object->m_collection_index;
            }
        }
        m_objects.pop_back();
        assert((object->m_collection = nullptr) == nullptr);
    }

private:
    //objects
    std::vector<T *> m_objects;

    friend class gc_object_collection;
    friend class gc;
};


//gc ptr collection
typedef gc_collection<gc_ptr_base> gc_ptr_collection;


//gc object collection
class gc_object_collection : public gc_collection<gc_object> {
public:
    //delete all objects
    ~gc_object_collection() {
        delete_objects();
    }

    //deletes all objects
    void delete_objects();
};


//pointer to the collection of objects
typedef std::shared_ptr<gc_object_collection> gc_object_collection_ptr;


//the garbage collector
class gc {
public:
    //collects garbage, using the mark & sweep algorithm
    static void collect();

private:
    //get the collection of root ptrs
    static gc_ptr_collection *get_root_ptrs() {
        static gc_ptr_collection p;
        return &p;
    }

    //get the current collection of objects
    static gc_object_collection_ptr &get_objects() {
        static gc_object_collection_ptr p{std::make_shared<gc_object_collection>()};
        return p;
    }

    //scan ptrs
    static void _scan(gc_ptr_collection &ptrs, gc_object_collection &old, gc_object_collection &reached);

    template <class T> friend class gc_root_ptr;
    friend class gc_object;
};


//gc ptr
class gc_ptr_base : public gc_collection_node<gc_ptr_base> {
public:
    //not allowed
    gc_ptr_base(const gc_ptr_base &) = delete;

    //not allowed
    gc_ptr_base(gc_ptr_base &) = delete;

    //not allowed
    gc_ptr_base &operator = (const gc_ptr_base &) = delete;

    //not allowed
    gc_ptr_base &operator = (gc_ptr_base &) = delete;

protected:
    //the pointer to the object
    gc_object *m_object;

    //the default constructor
    gc_ptr_base(gc_ptr_collection *owner, gc_object *object = nullptr) :
        m_object(object)
    {
        owner->add(this);
    }

    //remove the pointer from the owning collection
    ~gc_ptr_base() {
        m_collection->remove(this);
    }

    friend class gc;
};


//A root ptr.
template <class T> class gc_root_ptr : public gc_ptr_base {
public:
    //The default constructor.
    gc_root_ptr(T *object = nullptr) :
        gc_ptr_base(gc::get_root_ptrs(), object)
    {
    }

    //The copy constructor.
    gc_root_ptr(const gc_root_ptr<T> &ptr) :
        gc_ptr_base(gc::get_root_ptrs(), ptr.m_object)
    {
    }

    //The move constructor.
    gc_root_ptr(gc_root_ptr<T> &&ptr) :
        gc_ptr_base(gc::get_root_ptrs(), ptr.m_object)
    {
    }

    //Assignment from object.
    gc_root_ptr<T> &operator = (T *object) {
        m_object = object;
        return *this;
    }

    //Assignment from pointer.
    gc_root_ptr<T> &operator = (const gc_root_ptr<T> &ptr) {
        m_object = ptr.m_object;
        return *this;
    }

    //Returns the value of the pointer.
    T *get() const {
        assert(m_object || static_cast<T *>(m_object) == nullptr);
        return static_cast<T *>(m_object);
    }

    //Auto conversion to ptr value.
    operator T* () const {
        return get();
    }

    //Accesses the underlying object's members.
    T *operator ->() const {
        assert(m_object);
        return get();
    }
};


//alternative type for gc root pointer
template <class T> using gc_ptr = gc_root_ptr<T>;


//gc object
class gc_object : public gc_collection_node<gc_object> {
public:
    //alternative type for gc root pointer
    template <class T> using gc_ptr = gc_member_ptr<T>;

    //registers the object to the collector
    gc_object() {
        gc::get_objects()->add(this);
    }

    //registers the object to the collector
    gc_object(const gc_object &obj) :
        gc_object()
    {
    }

    //registers the object to the collector
    gc_object(gc_object &&obj) :
        gc_object()
    {
    }

    //removes the object from the collector
    virtual ~gc_object() {
        m_collection->remove(this);
    }

    //not allowed
    gc_object &operator = (const gc_object &) = delete;

    //not allowed
    gc_object &operator = (gc_object &&) = delete;

private:
    //member pointers
    gc_ptr_collection m_member_ptrs;

    friend class gc;
    template <class T> friend class gc_member_ptr;
};


//A member ptr.
template <class T> class gc_member_ptr : public gc_ptr_base {
public:
    //The default constructor.
    gc_member_ptr(gc_object *owner, T *object = nullptr) :
        gc_ptr_base(&owner->m_member_ptrs, object)
    {
    }

    //The copy constructor.
    gc_member_ptr(gc_object *owner, const gc_member_ptr<T> &ptr) :
        gc_ptr_base(&owner->m_member_ptrs, ptr.m_object)
    {
    }

    //The move constructor.
    gc_member_ptr(gc_object *owner, gc_member_ptr<T> &&ptr) :
        gc_ptr_base(&owner->m_member_ptrs, ptr.m_object)
    {
    }

    //Assignment from object.
    gc_member_ptr<T> &operator = (T *object) {
        m_object = object;
        return *this;
    }

    //Assignment from pointer.
    gc_member_ptr<T> &operator = (const gc_member_ptr<T> &ptr) {
        m_object = ptr.m_object;
        return *this;
    }

    //Returns the value of the pointer.
    T *get() const {
        assert(m_object || static_cast<T *>(m_object) == nullptr);
        return static_cast<T *>(m_object);
    }

    //Auto conversion to ptr value.
    operator T* () const {
        return get();
    }

    //Accesses the underlying object's members.
    T *operator ->() const {
        assert(m_object);
        return get();
    }
};


#endif //GC_HPP


//delete all objects
void gc_object_collection::delete_objects() {
    while (!m_objects.empty())
        delete m_objects.back();
}


//collects garbage, using the mark & sweep algorithm
void gc::collect() {
    gc_object_collection_ptr reachable{std::make_shared<gc_object_collection>()};
    _scan(*get_root_ptrs(), *get_objects().get(), *reachable.get());
    get_objects()->delete_objects();
    std::swap(get_objects(), reachable);
}


//scan ptrs
void gc::_scan(gc_ptr_collection &ptrs, gc_object_collection &old, gc_object_collection &reached) {
    for(gc_ptr_base *ptr : ptrs.m_objects) {
        if (ptr->m_object && ptr->m_object->m_collection == &old) {
            old.remove(ptr->m_object);
            reached.add(ptr->m_object);
            _scan(ptr->m_object->m_member_ptrs, old, reached);
        }
    }
}

// test code
#include <random>
#include <iostream>
#include <new>

size_t objects = 0;
class Cell;
typedef gc_ptr<Cell> CellPtr;

class Cell : public gc_object {
public:
    Cell(size_t tag) : tag(tag), next(nullptr) {
        ++objects;
    }

    ~Cell() {
        --objects;
    }

    size_t tag;
    CellPtr next;

    void mangle() {
        if (next && next->next) {
            next = next->next;
            next->next->mangle();
        }
    }

};

CellPtr create_cell_list(size_t length) {
    gc_ptr<Cell> c = nullptr;
    if (length != 0) {
        c = new Cell(length);//make_collected<Cell>(length);
        c->next = create_cell_list(length-1);
    }
    return c;
}

// try to allocate a gc_ptr<T>, running a garbage collection if we run out of memory.
template <class T, class ...Args>
gc_ptr<T> make_collected(Args &&...args) {
    static_assert(std::is_base_of<gc_object, T>::value, "");
    void *ptr = ::operator new(sizeof(T), std::nothrow);
    if (ptr == nullptr) {
        gc::collect();
        std::cout << "[Running automatic garbage collection]\n";
        // don't pass nothrow so that we throw bad_alloc on failure
        ptr = ::operator new(sizeof(T));
    }
    try {
        new (ptr) T(std::forward<Args>(args)...);
    } catch (...) {
        ::operator delete(ptr);
        throw;
    }
    return gc_ptr<T>{static_cast<T*>(ptr)};
}

CellPtr create_cell_list_autocollect(size_t length) {
    gc_ptr<Cell> c = nullptr;
    if (length != 0) {
        c = make_collected<Cell>(length);
        c->next = create_cell_list(length-1);
    }
    return c;
}

void test_cycle_collection() {
    std::cout << "Testing cycle collection\n"
                 "  number of live objects (pretest): " << objects << '\n';

    CellPtr list = create_cell_list(100);
    std::cout << "  number of live objects (initial): " << objects << '\n';
    // find the last Cell, and make it's next point to the first element
    {
        Cell *l = list.get();
        while (l && l->next) {
            l = l->next;
        }
        l->next = l;
    }
    std::cout << "  collecting\n";
    gc::collect();
    std::cout << "  number of live objects (after collection): " << objects << '\n';
}


void test_dropped_pointers() {
    std::cout << "Testing dropped pointers (single)\n"
                 "  number of live objects (pretest): " << objects << '\n';

    CellPtr list = create_cell_list(100);

    std::cout << "  number of live objects (initial): " << objects << '\n';
    list->mangle();

    std::cout << "  collecting\n";
    gc::collect();

    std::cout << "  number of live objects (after collection): " << objects << '\n';
}

void test_dropped_pointers_stress() {
    std::cout << "Testing dropped pointers (multiple, autocollecting)\n";

    CellPtr list;
    for (size_t i = 0; i < 10; ++i) {
        std::cout << "  number of live objects (pre " << i << "): " << objects << '\n';
        for (size_t j = 0; j < 100; ++j) {
            list = create_cell_list_autocollect(1000);
            list->mangle();
        }
        std::cout << "  number of live objects (post " << i << "): " << objects << '\n';
    }

    std::cout << "  number of live objects (before collection): " << objects << '\n';
    gc::collect();
    std::cout << "  number of live objects (after collection): " << objects << '\n';
}


int main() {
    test_cycle_collection();
    test_dropped_pointers();
    test_dropped_pointers_stress();
    return 0;
}




