File: id_man.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (107 lines) | stat: -rw-r--r-- 3,652 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef ULTIMA8_MISC_IDMAN_H
#define ULTIMA8_MISC_IDMAN_H

#include "ultima/shared/std/containers.h"

namespace Ultima {
namespace Ultima8 {

//
// idMan. Used to allocate and keep track of unused ids.
// Supposed to be used by Kernel and World for pID and ObjId
//
// The idMan itself uses a nifty linked list that is also an array.
// As such it has the advantages of both. Adds and removals are fast,
// As is checking to see if an ID is used.
//
// idMan works as a LILO (last in last out) for id recycling. So an id that has
// been cleared will not be used until all other currently unused ids have been
// allocated.
//

class idMan {
	uint16      _begin;          //!< start of the available range of IDs
	uint16      _end;            //!< current end of the range
	uint16      _maxEnd;         //!< end of the available range
	uint16      _startCount;     //!< number of IDs to make available initially

	uint16      _usedCount;      //!< number of IDs currently in use

	Std::vector<uint16> _ids;    //!< the 'next' field in a list of free IDs
	uint16      _first;          //!< the first ID in the free list
	uint16      _last;           //!< the last ID in the last list
public:
	//! \param begin start of the range of available IDs
	//! \param max_end end of the range of available IDs
	//! \param startcount number of IDs to make available initially (0 = all)
	idMan(uint16 begin, uint16 max_end, uint16 startcount = 0);
	~idMan();

	//! check if this idMan is full
	bool        isFull() const {
		return _first == 0 && _end >= _maxEnd;
	}

	//! clear all IDs, reset size to the startcount, and set max_end to new_max
	void        clearAll(uint16 new_max = 0);

	//! get a free ID
	//! \return a free ID, or 0 if none are available
	uint16      getNewID();

	//! mark a given ID as used, expanding if necessary.
	//! Note: reserveID is O(n), so don't use too often.
	//! \return false if the ID was already used or is out of range
	bool        reserveID(uint16 id);

	//! release an id
	void        clearID(uint16 id);

	//! check if an ID is in use
	bool        isIDUsed(uint16 id) const {
		return id >= _begin && id <= _end && _ids[id] == 0 && id != _last;
	}

	//! increase the maximum size
	//! Note: this shouldn't be used in normal circumstances.
	//!       It exists for dumpMap currently. If that is rewritten not
	//!       to need more than 32768 object IDs, this function should be
	//!       deleted.
	void        setNewMax(uint16 maxEnd) {
		_maxEnd = maxEnd;
	}

	void save(Common::WriteStream *ws) const;
	bool load(Common::ReadStream *rs, uint32 version);

private:
	//! double the amount of available IDs (up to the maximum passed
	//! to the constructor)
	void expand();
};

} // End of namespace Ultima8
} // End of namespace Ultima

#endif