PageRenderTime 38ms CodeModel.GetById 15ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/llindexedqueue.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 155 lines | 92 code | 19 blank | 44 comment | 14 complexity | f796b503e1c2091f8ec5f16e9d5c5d2c MD5 | raw file
  1/** 
  2 * @file llindexedqueue.h
  3 * @brief An indexed FIFO queue, where only one element with each key
  4 * can be in the queue.
  5 *
  6 * $LicenseInfo:firstyear=2003&license=viewerlgpl$
  7 * Second Life Viewer Source Code
  8 * Copyright (C) 2010, Linden Research, Inc.
  9 * 
 10 * This library is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU Lesser General Public
 12 * License as published by the Free Software Foundation;
 13 * version 2.1 of the License only.
 14 * 
 15 * This library is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18 * Lesser General Public License for more details.
 19 * 
 20 * You should have received a copy of the GNU Lesser General Public
 21 * License along with this library; if not, write to the Free Software
 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 23 * 
 24 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 25 * $/LicenseInfo$
 26 */
 27
 28#ifndef LL_LLINDEXEDQUEUE_H
 29#define LL_LLINDEXEDQUEUE_H
 30
 31// An indexed FIFO queue, where only one element with each key can be in the queue.
 32// This is ONLY used in the interest list, you'll probably want to review this code
 33// carefully if you want to use it elsewhere - Doug
 34
 35template <typename Type> 
 36class LLIndexedQueue
 37{
 38protected:
 39	typedef std::deque<Type> type_deque;
 40	type_deque mQueue;
 41	std::set<Type> mKeySet;
 42
 43public:
 44	LLIndexedQueue() {}
 45
 46	// move_if_there is an O(n) operation
 47	bool push_back(const Type &value, bool move_if_there = false)
 48	{
 49		if (mKeySet.find(value) != mKeySet.end())
 50		{
 51			// Already on the queue
 52			if (move_if_there)
 53			{
 54				// Remove the existing entry.
 55				typename type_deque::iterator it;
 56				for (it = mQueue.begin(); it != mQueue.end(); ++it)
 57				{
 58					if (*it == value)
 59					{
 60						break;
 61					}
 62				}
 63
 64				// This HAS to succeed, otherwise there's a serious bug in the keyset implementation
 65				// (although this isn't thread safe, at all)
 66
 67				mQueue.erase(it);
 68			}
 69			else
 70			{
 71				// We're not moving it, leave it alone
 72				return false;
 73			}
 74		}
 75		else
 76		{
 77			// Doesn't exist, add it to the key set
 78			mKeySet.insert(value);
 79		}
 80
 81		mQueue.push_back(value);
 82
 83		// We succeeded in adding the new element.
 84		return true;
 85	}
 86
 87	bool push_front(const Type &value, bool move_if_there = false)
 88	{
 89		if (mKeySet.find(value) != mKeySet.end())
 90		{
 91			// Already on the queue
 92			if (move_if_there)
 93			{
 94				// Remove the existing entry.
 95				typename type_deque::iterator it;
 96				for (it = mQueue.begin(); it != mQueue.end(); ++it)
 97				{
 98					if (*it == value)
 99					{
100						break;
101					}
102				}
103
104				// This HAS to succeed, otherwise there's a serious bug in the keyset implementation
105				// (although this isn't thread safe, at all)
106
107				mQueue.erase(it);
108			}
109			else
110			{
111				// We're not moving it, leave it alone
112				return false;
113			}
114		}
115		else
116		{
117			// Doesn't exist, add it to the key set
118			mKeySet.insert(value);
119		}
120
121		mQueue.push_front(value);
122		return true;
123	}
124
125	void pop()
126	{
127		Type value = mQueue.front();
128		mKeySet.erase(value);
129		mQueue.pop_front();
130	}
131
132	Type &front()
133	{
134		return mQueue.front();
135	}
136
137	S32 size() const
138	{
139		return mQueue.size();
140	}
141
142	bool empty() const
143	{
144		return mQueue.empty();
145	}
146
147	void clear()
148	{
149		// Clear out all elements on the queue
150		mQueue.clear();
151		mKeySet.clear();
152	}
153};
154
155#endif // LL_LLINDEXEDQUEUE_H