summaryrefslogtreecommitdiff
path: root/include/intrusive_list_impl.h
blob: 0efe06d2e52e6bd41acf0cf298eaeaae35662560 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2013-2014 Attila Molnar <attilamolnar@hush.com>
 *
 * This file is part of InspIRCd.  InspIRCd is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, version 2.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


template <typename T, typename Tag>
class INSPIRCD_INTRUSIVE_LIST_NAME
{
 public:
	class iterator : public std::iterator<std::bidirectional_iterator_tag, T*>
	{
		T* curr;

	 public:
		iterator(T* i = NULL)
			: curr(i)
		{
		}

		iterator& operator++()
		{
			curr = curr->intrusive_list_node<T, Tag>::ptr_next;
			return *this;
		}

		iterator operator++(int)
		{
			iterator ret(*this);
			operator++();
			return ret;
		}

		iterator& operator--()
		{
			curr = curr->intrusive_list_node<T, Tag>::ptr_prev;
			return *this;
		}

		iterator operator--(int)
		{
			iterator ret(*this);
			operator--();
			return ret;
		}

		bool operator==(const iterator& other) const { return (curr == other.curr); }
		bool operator!=(const iterator& other) const { return (curr != other.curr); }
		T* operator*() const { return curr; }
	};

	typedef iterator const_iterator;

	INSPIRCD_INTRUSIVE_LIST_NAME()
		: listhead(NULL)
#ifdef INSPIRCD_INTRUSIVE_LIST_HAS_TAIL
		, listtail(NULL)
#endif
		, listsize(0)
	{
	}

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

	size_t size() const
	{
		return listsize;
	}

	iterator begin() const
	{
		return iterator(listhead);
	}

	iterator end() const
	{
		return iterator();
	}

	void pop_front()
	{
		erase(listhead);
	}

	T* front() const
	{
		return listhead;
	}

	void push_front(T* x)
	{
		if (listsize++)
		{
			x->intrusive_list_node<T, Tag>::ptr_next = listhead;
			listhead->intrusive_list_node<T, Tag>::ptr_prev = x;
		}
#ifdef INSPIRCD_INTRUSIVE_LIST_HAS_TAIL
		else
			listtail = x;
#endif
		listhead = x;
	}

#ifdef INSPIRCD_INTRUSIVE_LIST_HAS_TAIL
	T* back() const
	{
		return listtail;
	}

	void push_back(T* x)
	{
		if (listsize++)
		{
			x->intrusive_list_node<T, Tag>::ptr_prev = listtail;
			listtail->intrusive_list_node<T, Tag>::ptr_next = x;
		}
		else
			listhead = x;
		listtail = x;
	}

	void pop_back()
	{
		erase(listtail);
	}
#endif

	void erase(const iterator& it)
	{
		erase(*it);
	}

	void erase(T* x)
	{
		if (listhead == x)
			listhead = x->intrusive_list_node<T, Tag>::ptr_next;
#ifdef INSPIRCD_INTRUSIVE_LIST_HAS_TAIL
		if (listtail == x)
			listtail = x->intrusive_list_node<T, Tag>::ptr_prev;
#endif
		x->intrusive_list_node<T, Tag>::unlink();
		listsize--;
	}

 private:
	T* listhead;
#ifdef INSPIRCD_INTRUSIVE_LIST_HAS_TAIL
	T* listtail;
#endif
	size_t listsize;
};