-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoublyLinkedNode.hpp
149 lines (142 loc) · 3.26 KB
/
DoublyLinkedNode.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef DOUBLYLINKEDNODE_HPP
# define DOUBLYLINKEDNODE_HPP
# include <memory>
namespace ft
{
template <typename Tp, class Alloc = std::allocator<Tp> >
class DoublyLinkedNode;
template <typename Tp, class Alloc>
class DoublyLinkedNode
{
private:
typedef DoublyLinkedNode node;
public:
typedef Tp value_type;
typedef Alloc allocator_type;
typedef typename Alloc::reference reference;
typedef typename Alloc::const_reference const_reference;
typedef typename Alloc::pointer pointer;
typedef typename Alloc::const_pointer const_pointer;
private:
allocator_type allocator_;
value_type* content_;
node* next_;
node* prev_;
public:
explicit DoublyLinkedNode(allocator_type const& alloc = allocator_type()):
allocator_(alloc),
content_(NULL),
next_(this),
prev_(this)
{};
explicit DoublyLinkedNode(
value_type const& val,
allocator_type const& alloc = allocator_type()):
allocator_(alloc),
content_(allocator_.allocate(1)),
next_(this),
prev_(this)
{
allocator_.construct(content_, val);
};
DoublyLinkedNode(
node const& ori_node,
allocator_type const& alloc = allocator_type()):
content_(),
next_(ori_node.next_),
prev_(ori_node.prev_)
{
allocator_ = alloc;
if (&ori_node == this)
{
this->content_ = NULL;
this->next_ = this;
this->prev_ = this;
return ;
}
if (ori_node.content_)
{
this->content_ = allocator_.allocate(1);
allocator_.construct(this->content_, *ori_node.content_);
}
else
this->content_ = NULL;
};
DoublyLinkedNode(
value_type value,
node* next_node,
node* prev_node,
allocator_type const& alloc = allocator_type()):
content_(allocator_.allocate(1)),
next_(next_node),
prev_(prev_node)
{
allocator_ = alloc;
allocator_.construct(this->content_, value);
};
node& operator=(node const& ori_node)
{
if (ori_node.content_)
*this->content_ = *ori_node.content_;
this->next_ = next_;
this->prev_ = prev_;
return (*this);
};
~DoublyLinkedNode(void)
{
if (prev_ != this)
{
prev_->next_ = next_;
next_->prev_ = prev_;
}
if (!content_)
return ;
allocator_.destroy(content_);
allocator_.deallocate(content_, 1);
};
value_type*& getContent(void)
{ return (content_); };
node*& getNext(void)
{ return (next_); };
node*& getPrev(void)
{ return (prev_); };
void setContent(value_type const& val)
{
if (content_ != NULL)
allocator_.destroy(content_);
else
content_ = allocator_.allocate(1);
allocator_.construct(content_, val);
};
void AddNext(DoublyLinkedNode* next_node)
{
this->next_->prev_ = next_node;
next_node->next_ = this->next_;
next_node->prev_ = this;
this->next_ = next_node;
};
void AddPrev(DoublyLinkedNode* prev_node)
{
this->prev_->next_ = prev_node;
prev_node->prev_ = this->prev_;
prev_node->next_ = this;
this->prev_ = prev_node;
};
node* PopGetNode(void)
{
this->next_->prev_ = this->prev_;
this->prev_->next_ = this->next_;
this->next_ = this;
this->prev_ = this;
return (this);
}
void ContentExchange(node*& nod)
{
value_type* temp;
temp = nod->content_;
nod->content_ = this->content_;
this->content_ = temp;
}
};
}
#endif