Skip to content
Permalink
Browse files

get rid of smart pointersp# Please enter the commit message for your …

…changes. Lines starting
  • Loading branch information...
an146 committed Sep 1, 2017
1 parent 9ea74bb commit a64c9433068af468544767de32a1f5b360543346
Showing with 68 additions and 73 deletions.
  1. +21 −0 simple_ptr.h
  2. +39 −65 string.h
  3. +8 −8 test1.cpp
@@ -0,0 +1,21 @@
#ifndef IV_SIMPLE_PTR_H
#define IV_SIMPLE_PTR_H

namespace iv
{

template <class T>
class simple_ptr
{
T *ptr;
public:
simple_ptr(T *_ptr) : ptr(_ptr) { }
T &operator *() { return *ptr; }
T *operator ->() { return ptr; }
T *get() const { return ptr; }
operator T *() const { return ptr; }
};

}

#endif // IV_SIMPLE_PTR_H
104 string.h
@@ -7,6 +7,7 @@
#include <iterator>
#include <memory>
#include <vector>
#include "simple_ptr.h"

namespace iv
{
@@ -16,20 +17,20 @@ namespace internal

struct tree;

struct tree_base : std::enable_shared_from_this<tree_base>
struct tree_base
{
std::shared_ptr<tree> l, r; // left, right
std::weak_ptr<tree_base> p; // parent
tree *l, *r; // left, right
tree_base *p; // parent

tree_base(std::shared_ptr<tree> _l, std::shared_ptr<tree> _r);
tree_base(tree *_l, tree *_r) : l(_l), r(_r) { }

void resurrect();
};

struct tree_head : public tree_base
{
tree_head(std::shared_ptr<tree> root = nullptr) : tree_base(root, NULL) { }
std::shared_ptr<tree> root()
tree_head(tree *_root = nullptr) : tree_base(_root, nullptr) { }
tree *root()
{
return l;
}
@@ -48,29 +49,24 @@ struct tree : public tree_base
return h;
}

tree(std::shared_ptr<tree> _l, char _v, std::shared_ptr<tree> _r)
tree(tree *_l, char _v, tree *_r)
: tree_base(_l, _r), v(_v), h(std::max(l->height(), r->height()) + 1)
{
}

std::shared_ptr<tree> add_min(char x);
std::shared_ptr<tree> add_max(char x);
tree *add_min(char x);
tree *add_max(char x);
};

tree_base::tree_base(std::shared_ptr<tree> _l, std::shared_ptr<tree> _r)
: l(_l), r(_r)
{
}

void tree_base::resurrect()
{
//std::cerr << "Resurrecting " << this << std::endl;
if (l) {
l->p = shared_from_this();
l->p = this;
l->resurrect();
}
if (r) {
r->p = shared_from_this();
r->p = this;
r->resurrect();
}
}
@@ -107,49 +103,49 @@ void tree_base::resurrect()
Node{l; v; r; h=(if hl >= hr then hl + 1 else hr + 1)}
*/

std::shared_ptr<tree> balance(std::shared_ptr<tree> l, char v, std::shared_ptr<tree> r)
tree *balance(tree *l, char v, tree *r)
{
if (l->height() > r->height() + 2) {
if (l->l->height() >= l->r->height()) {
auto right = std::make_shared<tree>(l->r, v, r);
return std::make_shared<tree>(l->l, l->v, right);
auto right = new tree(l->r, v, r);
return new tree(l->l, l->v, right);
} else {
auto left = std::make_shared<tree>(l->l, l->v, l->r->l);
auto right = std::make_shared<tree>(l->r->r, v, r);
return std::make_shared<tree>(left, l->r->v, right);
auto left = new tree(l->l, l->v, l->r->l);
auto right = new tree(l->r->r, v, r);
return new tree(left, l->r->v, right);
}
} else if (r->height() > l->height() + 2) {
if (r->r->height() >= r->l->height()) {
auto left = std::make_shared<tree>(l, v, r->l);
return std::make_shared<tree>(left, r->v, r->r);
auto left = new tree(l, v, r->l);
return new tree(left, r->v, r->r);
} else {
auto right = std::make_shared<tree>(r->l->r, l->v, r->r);
auto left = std::make_shared<tree>(l, v, r->l->l);
return std::make_shared<tree>(left, r->l->v, right);
auto right = new tree(r->l->r, l->v, r->r);
auto left = new tree(l, v, r->l->l);
return new tree(left, r->l->v, right);
}
} else
return std::make_shared<tree>(l, v, r);
return new tree(l, v, r);
}

std::shared_ptr<tree> tree::add_min(char x)
tree *tree::add_min(char x)
{
if (this == nullptr)
return std::make_shared<tree>(nullptr, x, nullptr);
return new tree(nullptr, x, nullptr);
else
return balance(l->add_min(x), v, r);
}

std::shared_ptr<tree> tree::add_max(char x)
tree *tree::add_max(char x)
{
if (this == nullptr)
return std::make_shared<tree>(nullptr, x, nullptr);
return new tree(nullptr, x, nullptr);
else
return balance(l, v, r->add_max(x));
}

} // namespace iv::internal

class string_const_iterator : public std::weak_ptr<const internal::tree_base>
class string_const_iterator : public simple_ptr<const internal::tree_base>
{
public:
using iterator_category = std::bidirectional_iterator_tag;
@@ -158,50 +154,31 @@ class string_const_iterator : public std::weak_ptr<const internal::tree_base>
using pointer = const internal::tree_base *;
using reference = const internal::tree_base &;

string_const_iterator(std::weak_ptr<const internal::tree_base> node = std::weak_ptr<const internal::tree_base>())
: std::weak_ptr<const internal::tree_base>(node)
string_const_iterator(const internal::tree_base *node = nullptr)
: simple_ptr<const internal::tree_base>(node)
{
}

/*string_const_iterator(const std::shared_ptr<const internal::tree_base> &node)
: std::weak_ptr<const internal::tree_base>(node)
{
}*/

/*string_const_iterator(std::weak_ptr<const internal::tree_base> &&node = std::weak_ptr<const internal::tree_base>())
: std::weak_ptr<const internal::tree_base>(node)
{
}*/

string_const_iterator left()
{
auto p = lock();
if (!p)
throw std::runtime_error("expired iterator");
return string_const_iterator(p->l);
return string_const_iterator((*this)->l);
}
string_const_iterator right()
{
auto p = lock();
if (!p)
throw std::runtime_error("expired iterator");
return string_const_iterator(p->r);
return string_const_iterator((*this)->r);
}
string_const_iterator parent()
{
auto p = lock();
if (!p)
throw std::runtime_error("expired iterator");
return string_const_iterator(p->p);
return string_const_iterator((*this)->p);
}

bool operator ==(const string_const_iterator &other)
{
return lock() == other.lock();
return get() == other.get();
}
bool operator !=(const string_const_iterator &other)
{
return lock() != other.lock();
return get() != other.get();
}

operator bool()
@@ -224,18 +201,17 @@ class string_const_iterator : public std::weak_ptr<const internal::tree_base>
}
char operator *()
{
return static_cast<const internal::tree *>(lock().get())->v;
return static_cast<const internal::tree *>(get())->v;
}
};

class string
{
std::shared_ptr<internal::tree_head> head;
std::vector<std::shared_ptr<internal::tree>> history;
simple_ptr<internal::tree_head> head;
public:
typedef string_const_iterator const_iterator;

string() : head(std::make_shared<internal::tree_head>())
string() : head(new internal::tree_head())
{
}

@@ -259,13 +235,11 @@ class string

void push_front(char c)
{
history.push_back(head->root());
head->l = head->root()->add_min(c);
head->resurrect();
}
void push_back(char c)
{
history.push_back(head->root());
head->l = head->root()->add_max(c);
head->resurrect();
//std::cout << "!" << head->l << " " << head->root()->c << std::endl;
@@ -47,19 +47,19 @@ int main(int argc, char **argv)
assert(!j.right());
++j;
assert(j == end);
assert(!s1.begin().expired());
assert(!j.expired());
//assert(!s1.begin().expired());
//assert(!j.expired());
s1.push_back('\001');
iv::string::const_iterator k = s1.begin();
if (k != end)
std::cerr << (int)*k << " " << k.lock() << std::endl;
//if (k != end)
//std::cerr << (int)*k << " " << k.lock() << std::endl;
++k;
if (k != end)
std::cerr << (int)*k << " " << k.lock() << std::endl;
//if (k != end)
//std::cerr << (int)*k << " " << k.lock() << std::endl;
assert(k != end);
++k;
if (k != end)
std::cerr << (int)*k << " " << k.lock() << std::endl;
//if (k != end)
//std::cerr << (int)*k << " " << k.lock() << std::endl;
assert(k == s1.end());
}
std::ifstream random("/dev/urandom", std::ios::binary);

0 comments on commit a64c943

Please sign in to comment.
You can’t perform that action at this time.