MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

IdTable.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_ID_TABLE_HXX
21 #define MPD_ID_TABLE_HXX
22
23 #include "Compiler.h"
24
25 #include <algorithm>
26
27 #include <assert.h>
28
32 class IdTable {
33 unsigned size;
34
35 unsigned next;
36
37 int *data;
38
39 public:
40 IdTable(unsigned _size):size(_size), next(1), data(new int[size]) {
41 std::fill_n(data, size, -1);
42 }
43
44 ~IdTable() {
45 delete[] data;
46 }
47
48 int IdToPosition(unsigned id) const {
49 return id < size
50 ? data[id]
51 : -1;
52 }
53
54 unsigned GenerateId() {
55 assert(next > 0);
56 assert(next < size);
57
58 while (true) {
59 unsigned id = next;
60
61 ++next;
62 if (next == size)
63 next = 1;
64
65 if (data[id] < 0)
66 return id;
67 }
68 }
69
70 unsigned Insert(unsigned position) {
71 unsigned id = GenerateId();
72 data[id] = position;
73 return id;
74 }
75
76 void Move(unsigned id, unsigned position) {
77 assert(id < size);
78 assert(data[id] >= 0);
79
80 data[id] = position;
81 }
82
83 void Erase(unsigned id) {
84 assert(id < size);
85 assert(data[id] >= 0);
86
87 data[id] = -1;
88 }
89 };
90
91 #endif
IdTable::~IdTable
~IdTable()
Definition: IdTable.hxx:44
IdTable::Erase
void Erase(unsigned id)
Definition: IdTable.hxx:83
IdTable::IdToPosition
int IdToPosition(unsigned id) const
Definition: IdTable.hxx:48
IdTable
A table that maps id numbers to position numbers.
Definition: IdTable.hxx:32
IdTable::Move
void Move(unsigned id, unsigned position)
Definition: IdTable.hxx:76
IdTable::GenerateId
unsigned GenerateId()
Definition: IdTable.hxx:54
Compiler.h
IdTable::Insert
unsigned Insert(unsigned position)
Definition: IdTable.hxx:70
IdTable::IdTable
IdTable(unsigned _size)
Definition: IdTable.hxx:40