PageRenderTime 26ms CodeModel.GetById 18ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/llpriqueuemap.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 145 lines | 95 code | 16 blank | 34 comment | 13 complexity | edc0912c31fc0653647ca4d8ce3ff2ab MD5 | raw file
  1/** 
  2 * @file llpriqueuemap.h
  3 * @brief Priority queue implementation
  4 *
  5 * $LicenseInfo:firstyear=2003&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26#ifndef LL_LLPRIQUEUEMAP_H
 27#define LL_LLPRIQUEUEMAP_H
 28
 29#include <map>
 30
 31//
 32// Priority queue, implemented under the hood as a
 33// map.  Needs to be done this way because none of the
 34// standard STL containers provide a representation
 35// where it's easy to reprioritize.
 36//
 37
 38template <class DATA>
 39class LLPQMKey
 40{
 41public:
 42	LLPQMKey(const F32 priority, DATA data) : mPriority(priority), mData(data)
 43	{
 44	}
 45
 46	bool operator<(const LLPQMKey &b) const
 47	{
 48		if (mPriority > b.mPriority)
 49		{
 50			return TRUE;
 51		}
 52		if (mPriority < b.mPriority)
 53		{
 54			return FALSE;
 55		}
 56		if (mData > b.mData)
 57		{
 58			return TRUE;
 59		}
 60		return FALSE;
 61	}
 62
 63	F32 mPriority;
 64	DATA mData;
 65};
 66
 67template <class DATA_TYPE>
 68class LLPriQueueMap
 69{
 70public:
 71	typedef typename std::map<LLPQMKey<DATA_TYPE>, DATA_TYPE>::iterator pqm_iter;
 72	typedef std::pair<LLPQMKey<DATA_TYPE>, DATA_TYPE> pqm_pair;
 73	typedef void (*set_pri_fn)(DATA_TYPE &data, const F32 priority);
 74	typedef F32 (*get_pri_fn)(DATA_TYPE &data);
 75
 76
 77	LLPriQueueMap(set_pri_fn set_pri, get_pri_fn get_pri) : mSetPriority(set_pri), mGetPriority(get_pri)
 78	{
 79	}
 80
 81	void push(const F32 priority, DATA_TYPE data)
 82	{
 83#ifdef _DEBUG
 84		pqm_iter iter = mMap.find(LLPQMKey<DATA_TYPE>(priority, data));
 85		if (iter != mMap.end())
 86		{
 87			llerrs << "Pushing already existing data onto queue!" << llendl;
 88		}
 89#endif
 90		mMap.insert(pqm_pair(LLPQMKey<DATA_TYPE>(priority, data), data));
 91	}
 92
 93	BOOL pop(DATA_TYPE *datap)
 94	{
 95		pqm_iter iter;
 96		iter = mMap.begin();
 97		if (iter == mMap.end())
 98		{
 99			return FALSE;
100		}
101		*datap = (*(iter)).second;
102		mMap.erase(iter);
103
104		return TRUE;
105	}
106
107	void reprioritize(const F32 new_priority, DATA_TYPE data)
108	{
109		pqm_iter iter;
110		F32 cur_priority = mGetPriority(data);
111		LLPQMKey<DATA_TYPE> cur_key(cur_priority, data);
112		iter = mMap.find(cur_key);
113		if (iter == mMap.end())
114		{
115			llwarns << "Data not on priority queue!" << llendl;
116			// OK, try iterating through all of the data and seeing if we just screwed up the priority
117			// somehow.
118			for (iter = mMap.begin(); iter != mMap.end(); iter++)
119			{
120				if ((*(iter)).second == data)
121				{
122					llerrs << "Data on priority queue but priority not matched!" << llendl;
123				}
124			}
125			return;
126		}
127
128		mMap.erase(iter);
129		mSetPriority(data, new_priority);
130		push(new_priority, data);
131	}
132
133	S32 getLength() const
134	{
135		return (S32)mMap.size();
136	}
137
138	// Hack: public for use by the transfer manager, ugh.
139	std::map<LLPQMKey<DATA_TYPE>, DATA_TYPE> mMap;
140protected:
141	void (*mSetPriority)(DATA_TYPE &data, const F32 priority);
142	F32 (*mGetPriority)(DATA_TYPE &data);
143};
144
145#endif // LL_LLPRIQUEUEMAP_H