]>
git.proxmox.com Git - ceph.git/blob - ceph/src/include/xlist.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
18 #include "include/assert.h"
30 item(T i
) : _item(i
), _prev(0), _next(0), _list(0) {}
32 assert(!is_on_list());
37 item(const item
& other
);
38 const item
& operator= (const item
& right
);
41 xlist
* get_list() { return _list
; }
42 bool is_on_list() const { return _list
? true:false; }
43 bool remove_myself() {
51 void move_to_front() {
53 _list
->push_front(this);
57 _list
->push_back(this);
61 typedef item
* value_type
;
62 typedef item
* const_reference
;
69 xlist(const xlist
& other
) {
70 _front
= other
._front
;
75 xlist() : _front(0), _back(0), _size(0) {}
83 assert((bool)_front
== (bool)_size
);
87 assert((bool)_front
== (bool)_size
);
94 assert((bool)_front
== (bool)_size
);
97 void push_front(item
*i
) {
111 void push_back(item
*i
) {
125 void remove(item
*i
) {
126 assert(i
->_list
== this);
129 i
->_prev
->_next
= i
->_next
;
133 i
->_next
->_prev
= i
->_prev
;
139 i
->_next
= i
->_prev
= 0;
140 assert((bool)_front
== (bool)_size
);
143 T
front() { return static_cast<T
>(_front
->_item
); }
144 const T
front() const { return static_cast<const T
>(_front
->_item
); }
146 T
back() { return static_cast<T
>(_back
->_item
); }
147 const T
back() const { return static_cast<const T
>(_back
->_item
); }
158 class iterator
: std::iterator
<std::forward_iterator_tag
, T
> {
162 iterator(item
*i
= 0) : cur(i
) {}
163 T
operator*() { return static_cast<T
>(cur
->_item
); }
164 iterator
& operator++() {
170 bool end() const { return cur
== 0; }
171 bool operator==(const iterator
& rhs
) const {
172 return cur
== rhs
.cur
;
174 bool operator!=(const iterator
& rhs
) const {
175 return cur
!= rhs
.cur
;
179 iterator
begin() { return iterator(_front
); }
180 iterator
end() { return iterator(NULL
); }
182 class const_iterator
: std::iterator
<std::forward_iterator_tag
, T
> {
186 const_iterator(item
*i
= 0) : cur(i
) {}
187 const T
operator*() { return static_cast<const T
>(cur
->_item
); }
188 const_iterator
& operator++() {
194 bool end() const { return cur
== 0; }
197 const_iterator
begin() const { return const_iterator(_front
); }
198 const_iterator
end() const { return const_iterator(NULL
); }