File: GraphicsId.cpp

package info (click to toggle)
storm-lang 0.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 51,984 kB
  • sloc: ansic: 261,420; cpp: 140,270; sh: 14,877; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (71 lines) | stat: -rw-r--r-- 1,443 bytes parent folder | download | duplicates (4)
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
#include "stdafx.h"
#include "GraphicsId.h"

namespace gui {

	const Nat full = 0xFFFFFFFF;
	const Nat natBits = CHAR_BIT * sizeof(Nat);

	IdMgr::IdMgr() : data(null), count(0) {}

	IdMgr::~IdMgr() {
		delete []data;
	}

	Nat IdMgr::alloc() {
		if (!data) {
			// Initial allocation.
			count = 4;
			data = new Nat[count];
			memset(data, 0, sizeof(Nat) * count);
		}

		for (Nat i = 0; i < count; i++) {
			if (data[i] != full)
				return allocate(data[i]) + i*natBits + 1;
		}

		Nat oldCount = count;
		grow();
		return allocate(data[oldCount]) + oldCount*natBits + 1;
	}

	void IdMgr::free(Nat id) {
		if (id == 0)
			return;

		id--;

		Nat mask = Nat(1) << (id % natBits);
		data[id / natBits] &= ~mask;
	}

	Nat IdMgr::allocate(Nat &in) {
		// Note: This could probably be made more efficient, but it is not really worth it. This is not hot code.
		for (Nat i = 0; i < natBits; i++) {
			Nat mask = Nat(1) << i;
			if ((in & mask) == 0) {
				in |= mask;
				return i;
			}
		}

		// Should not happen, but to keep the compiler happy.
		return 0;
	}

	void IdMgr::grow() {
		// This rate should be more than enough.
		Nat newCount = count + 4;
		Nat *newData = new Nat[newCount];

		// This is a bit wasteful, but does not matter, as we're dealing with very small amounts of data.
		memset(newData, 0, sizeof(Nat) * newCount);
		memcpy(newData, data, sizeof(Nat) * count);

		delete[] data;
		count = newCount;
		data = newData;
	}

}