2009-03-04 03:31:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2005 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//
|
|
|
|
// Templated list class. Normally we'd use STL, but we don't have that.
|
|
|
|
// This class mimics STL's interfaces.
|
|
|
|
//
|
|
|
|
// Objects are copied into the list with the '=' operator or with copy-
|
|
|
|
// construction, so if the compiler's auto-generated versions won't work for
|
|
|
|
// you, define your own.
|
|
|
|
//
|
2009-04-28 05:09:49 +00:00
|
|
|
// The only class you want to use from here is "List".
|
2009-03-04 03:31:44 +00:00
|
|
|
//
|
|
|
|
#ifndef _LIBS_UTILS_LIST_H
|
|
|
|
#define _LIBS_UTILS_LIST_H
|
|
|
|
|
2009-04-28 05:09:49 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-04-28 05:09:49 +00:00
|
|
|
namespace android {
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
/*
|
2009-04-28 05:09:49 +00:00
|
|
|
* Doubly-linked list. Instantiate with "List<MyClass> myList".
|
|
|
|
*
|
|
|
|
* Objects added to the list are copied using the assignment operator,
|
|
|
|
* so this must be defined.
|
2009-03-04 03:31:44 +00:00
|
|
|
*/
|
2009-04-28 05:09:49 +00:00
|
|
|
template<typename T>
|
|
|
|
class List
|
|
|
|
{
|
|
|
|
protected:
|
2009-03-04 03:31:44 +00:00
|
|
|
/*
|
2009-04-28 05:09:49 +00:00
|
|
|
* One element in the list.
|
2009-03-04 03:31:44 +00:00
|
|
|
*/
|
2009-04-28 05:09:49 +00:00
|
|
|
class _Node {
|
|
|
|
public:
|
|
|
|
explicit _Node(const T& val) : mVal(val) {}
|
|
|
|
~_Node() {}
|
|
|
|
inline T& getRef() { return mVal; }
|
|
|
|
inline const T& getRef() const { return mVal; }
|
|
|
|
inline _Node* getPrev() const { return mpPrev; }
|
|
|
|
inline _Node* getNext() const { return mpNext; }
|
|
|
|
inline void setVal(const T& val) { mVal = val; }
|
|
|
|
inline void setPrev(_Node* ptr) { mpPrev = ptr; }
|
|
|
|
inline void setNext(_Node* ptr) { mpNext = ptr; }
|
|
|
|
private:
|
|
|
|
friend class List;
|
|
|
|
friend class _ListIterator;
|
|
|
|
T mVal;
|
|
|
|
_Node* mpPrev;
|
|
|
|
_Node* mpNext;
|
|
|
|
};
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
/*
|
2009-04-28 05:09:49 +00:00
|
|
|
* Iterator for walking through the list.
|
2009-03-04 03:31:44 +00:00
|
|
|
*/
|
2009-04-28 05:09:49 +00:00
|
|
|
|
|
|
|
template <typename TYPE>
|
|
|
|
struct CONST_ITERATOR {
|
|
|
|
typedef _Node const * NodePtr;
|
|
|
|
typedef const TYPE Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename TYPE>
|
|
|
|
struct NON_CONST_ITERATOR {
|
|
|
|
typedef _Node* NodePtr;
|
|
|
|
typedef TYPE Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<
|
|
|
|
typename U,
|
|
|
|
template <class> class Constness
|
|
|
|
>
|
|
|
|
class _ListIterator {
|
|
|
|
typedef _ListIterator<U, Constness> _Iter;
|
|
|
|
typedef typename Constness<U>::NodePtr _NodePtr;
|
|
|
|
typedef typename Constness<U>::Type _Type;
|
|
|
|
|
|
|
|
explicit _ListIterator(_NodePtr ptr) : mpNode(ptr) {}
|
|
|
|
|
|
|
|
public:
|
|
|
|
_ListIterator() {}
|
|
|
|
_ListIterator(const _Iter& rhs) : mpNode(rhs.mpNode) {}
|
|
|
|
~_ListIterator() {}
|
|
|
|
|
|
|
|
// this will handle conversions from iterator to const_iterator
|
|
|
|
// (and also all convertible iterators)
|
|
|
|
// Here, in this implementation, the iterators can be converted
|
|
|
|
// if the nodes can be converted
|
|
|
|
template<typename V> explicit
|
|
|
|
_ListIterator(const V& rhs) : mpNode(rhs.mpNode) {}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dereference operator. Used to get at the juicy insides.
|
|
|
|
*/
|
|
|
|
_Type& operator*() const { return mpNode->getRef(); }
|
|
|
|
_Type* operator->() const { return &(mpNode->getRef()); }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterator comparison.
|
|
|
|
*/
|
|
|
|
inline bool operator==(const _Iter& right) const {
|
|
|
|
return mpNode == right.mpNode; }
|
|
|
|
|
|
|
|
inline bool operator!=(const _Iter& right) const {
|
|
|
|
return mpNode != right.mpNode; }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* handle comparisons between iterator and const_iterator
|
|
|
|
*/
|
|
|
|
template<typename OTHER>
|
|
|
|
inline bool operator==(const OTHER& right) const {
|
|
|
|
return mpNode == right.mpNode; }
|
|
|
|
|
|
|
|
template<typename OTHER>
|
|
|
|
inline bool operator!=(const OTHER& right) const {
|
|
|
|
return mpNode != right.mpNode; }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Incr/decr, used to move through the list.
|
|
|
|
*/
|
|
|
|
inline _Iter& operator++() { // pre-increment
|
|
|
|
mpNode = mpNode->getNext();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const _Iter operator++(int) { // post-increment
|
|
|
|
_Iter tmp(*this);
|
|
|
|
mpNode = mpNode->getNext();
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
inline _Iter& operator--() { // pre-increment
|
|
|
|
mpNode = mpNode->getPrev();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const _Iter operator--(int) { // post-increment
|
|
|
|
_Iter tmp(*this);
|
|
|
|
mpNode = mpNode->getPrev();
|
|
|
|
return tmp;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-04-28 05:09:49 +00:00
|
|
|
inline _NodePtr getNode() const { return mpNode; }
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-04-28 05:09:49 +00:00
|
|
|
private:
|
|
|
|
friend class List;
|
|
|
|
_NodePtr mpNode;
|
|
|
|
};
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
public:
|
2009-04-28 05:09:49 +00:00
|
|
|
List() {
|
2009-03-04 03:31:44 +00:00
|
|
|
prep();
|
|
|
|
}
|
|
|
|
List(const List<T>& src) { // copy-constructor
|
|
|
|
prep();
|
|
|
|
insert(begin(), src.begin(), src.end());
|
|
|
|
}
|
2009-04-28 05:09:49 +00:00
|
|
|
virtual ~List() {
|
2009-03-04 03:31:44 +00:00
|
|
|
clear();
|
|
|
|
delete[] (unsigned char*) mpMiddle;
|
|
|
|
}
|
|
|
|
|
2009-04-28 05:09:49 +00:00
|
|
|
typedef _ListIterator<T, NON_CONST_ITERATOR> iterator;
|
|
|
|
typedef _ListIterator<T, CONST_ITERATOR> const_iterator;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
List<T>& operator=(const List<T>& right);
|
|
|
|
|
|
|
|
/* returns true if the list is empty */
|
2009-04-28 05:09:49 +00:00
|
|
|
inline bool empty() const { return mpMiddle->getNext() == mpMiddle; }
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
/* return #of elements in list */
|
2009-04-28 05:09:49 +00:00
|
|
|
size_t size() const {
|
|
|
|
return size_t(distance(begin(), end()));
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the first element or one past the last element. The
|
2009-04-28 05:09:49 +00:00
|
|
|
* _Node* we're returning is converted to an "iterator" by a
|
2009-03-04 03:31:44 +00:00
|
|
|
* constructor in _ListIterator.
|
|
|
|
*/
|
2009-04-28 05:09:49 +00:00
|
|
|
inline iterator begin() {
|
|
|
|
return iterator(mpMiddle->getNext());
|
|
|
|
}
|
|
|
|
inline const_iterator begin() const {
|
|
|
|
return const_iterator(const_cast<_Node const*>(mpMiddle->getNext()));
|
|
|
|
}
|
|
|
|
inline iterator end() {
|
|
|
|
return iterator(mpMiddle);
|
|
|
|
}
|
|
|
|
inline const_iterator end() const {
|
|
|
|
return const_iterator(const_cast<_Node const*>(mpMiddle));
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
/* add the object to the head or tail of the list */
|
|
|
|
void push_front(const T& val) { insert(begin(), val); }
|
|
|
|
void push_back(const T& val) { insert(end(), val); }
|
|
|
|
|
|
|
|
/* insert before the current node; returns iterator at new node */
|
2009-04-28 05:09:49 +00:00
|
|
|
iterator insert(iterator posn, const T& val)
|
|
|
|
{
|
2009-03-04 03:31:44 +00:00
|
|
|
_Node* newNode = new _Node(val); // alloc & copy-construct
|
|
|
|
newNode->setNext(posn.getNode());
|
|
|
|
newNode->setPrev(posn.getNode()->getPrev());
|
|
|
|
posn.getNode()->getPrev()->setNext(newNode);
|
|
|
|
posn.getNode()->setPrev(newNode);
|
2009-04-28 05:09:49 +00:00
|
|
|
return iterator(newNode);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* insert a range of elements before the current node */
|
|
|
|
void insert(iterator posn, const_iterator first, const_iterator last) {
|
|
|
|
for ( ; first != last; ++first)
|
|
|
|
insert(posn, *first);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove one entry; returns iterator at next node */
|
|
|
|
iterator erase(iterator posn) {
|
|
|
|
_Node* pNext = posn.getNode()->getNext();
|
|
|
|
_Node* pPrev = posn.getNode()->getPrev();
|
|
|
|
pPrev->setNext(pNext);
|
|
|
|
pNext->setPrev(pPrev);
|
|
|
|
delete posn.getNode();
|
2009-04-28 05:09:49 +00:00
|
|
|
return iterator(pNext);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* remove a range of elements */
|
|
|
|
iterator erase(iterator first, iterator last) {
|
|
|
|
while (first != last)
|
|
|
|
erase(first++); // don't erase than incr later!
|
2009-04-28 05:09:49 +00:00
|
|
|
return iterator(last);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* remove all contents of the list */
|
2009-04-28 05:09:49 +00:00
|
|
|
void clear() {
|
2009-03-04 03:31:44 +00:00
|
|
|
_Node* pCurrent = mpMiddle->getNext();
|
|
|
|
_Node* pNext;
|
|
|
|
|
|
|
|
while (pCurrent != mpMiddle) {
|
|
|
|
pNext = pCurrent->getNext();
|
|
|
|
delete pCurrent;
|
|
|
|
pCurrent = pNext;
|
|
|
|
}
|
|
|
|
mpMiddle->setPrev(mpMiddle);
|
|
|
|
mpMiddle->setNext(mpMiddle);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Measure the distance between two iterators. On exist, "first"
|
|
|
|
* will be equal to "last". The iterators must refer to the same
|
|
|
|
* list.
|
|
|
|
*
|
2009-04-28 05:09:49 +00:00
|
|
|
* FIXME: This is actually a generic iterator function. It should be a
|
|
|
|
* template function at the top-level with specializations for things like
|
|
|
|
* vector<>, which can just do pointer math). Here we limit it to
|
|
|
|
* _ListIterator of the same type but different constness.
|
2009-03-04 03:31:44 +00:00
|
|
|
*/
|
2009-04-28 05:09:49 +00:00
|
|
|
template<
|
|
|
|
typename U,
|
|
|
|
template <class> class CL,
|
|
|
|
template <class> class CR
|
|
|
|
>
|
|
|
|
ptrdiff_t distance(
|
|
|
|
_ListIterator<U, CL> first, _ListIterator<U, CR> last) const
|
|
|
|
{
|
|
|
|
ptrdiff_t count = 0;
|
2009-03-04 03:31:44 +00:00
|
|
|
while (first != last) {
|
|
|
|
++first;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/*
|
2009-04-28 05:09:49 +00:00
|
|
|
* I want a _Node but don't need it to hold valid data. More
|
2009-03-04 03:31:44 +00:00
|
|
|
* to the point, I don't want T's constructor to fire, since it
|
|
|
|
* might have side-effects or require arguments. So, we do this
|
|
|
|
* slightly uncouth storage alloc.
|
|
|
|
*/
|
2009-04-28 05:09:49 +00:00
|
|
|
void prep() {
|
2009-03-04 03:31:44 +00:00
|
|
|
mpMiddle = (_Node*) new unsigned char[sizeof(_Node)];
|
|
|
|
mpMiddle->setPrev(mpMiddle);
|
|
|
|
mpMiddle->setNext(mpMiddle);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This node plays the role of "pointer to head" and "pointer to tail".
|
|
|
|
* It sits in the middle of a circular list of nodes. The iterator
|
|
|
|
* runs around the circle until it encounters this one.
|
|
|
|
*/
|
|
|
|
_Node* mpMiddle;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assignment operator.
|
|
|
|
*
|
|
|
|
* The simplest way to do this would be to clear out the target list and
|
|
|
|
* fill it with the source. However, we can speed things along by
|
|
|
|
* re-using existing elements.
|
|
|
|
*/
|
|
|
|
template<class T>
|
|
|
|
List<T>& List<T>::operator=(const List<T>& right)
|
|
|
|
{
|
|
|
|
if (this == &right)
|
|
|
|
return *this; // self-assignment
|
|
|
|
iterator firstDst = begin();
|
|
|
|
iterator lastDst = end();
|
|
|
|
const_iterator firstSrc = right.begin();
|
|
|
|
const_iterator lastSrc = right.end();
|
|
|
|
while (firstSrc != lastSrc && firstDst != lastDst)
|
|
|
|
*firstDst++ = *firstSrc++;
|
|
|
|
if (firstSrc == lastSrc) // ran out of elements in source?
|
|
|
|
erase(firstDst, lastDst); // yes, erase any extras
|
|
|
|
else
|
|
|
|
insert(lastDst, firstSrc, lastSrc); // copy remaining over
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace android
|
|
|
|
|
|
|
|
#endif // _LIBS_UTILS_LIST_H
|