⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 number_pool.cpp

📁 这是一款2d游戏引擎
💻 CPP
字号:
/*
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This library 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
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
**
*/

#include "Core/precomp.h"
#include "API/Core/Math/number_pool.h"

CL_NumberPool::CL_NumberPool() :
initial_number_range(0)
{
}

//: Return a unique number.
unsigned short CL_NumberPool::get_number()
{
	unsigned short ret_val;

	// look for available numbers in the number pool
	if (available_numbers.size() > 0)
	{
		std::set<unsigned short>::iterator iter;

		// get the first number available
		iter = available_numbers.begin();

		// store data to the return variable
		ret_val = *iter;

		// now remove the number from the pool
		available_numbers.erase(iter);
	}
	// there are no available numbers in the number pool
	else
	{
		// make sure that the initial_number_range has not reached the max
		if (initial_number_range == USHRT_MAX)
		{
			// throw an exeption stating that no more unique numbers can be generated
			throw CL_Error("CL_NumberPool::get_number failed: All unique numbers have been used up.");
		}

		// generate the a new available number
		ret_val = initial_number_range++;
	}

	return ret_val;
}

//: Pass a number back in so as to beable to reuse the unique number. NOTE: numbers that were not given before or already returned back will be discarded.
void CL_NumberPool::return_number(unsigned short number)
{
	// check if the given number is within the initial_number_range
	// if it's not, discard it
	if (number >= initial_number_range)
		return;

	// add number into list, be it already inserted or not.
	// since a set can only have unique data, so it doesn't matters
	available_numbers.insert(number);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -