blob: e76cc0ac785fddf6370449aa70431d593c01942c (
plain)
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
|
/*
* Copyright (C) 2016 Andrew Comminos <andrew@comminos.com>
*
* 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/>.
*/
package se.lublin.humla.model;
/**
* A simple implementation of a fixed-size whisper target list using a bit vector.
* Created by andrew on 29/04/16.
*/
public class WhisperTargetList {
public static final byte TARGET_MIN = 1;
public static final byte TARGET_MAX = 30;
private final WhisperTarget[] mActiveTargets;
// Mumble stores voice targets using a 5-bit identifier.
// Use a bit vector to represent this 32-element range.
private int mTakenIds;
public WhisperTargetList() {
mActiveTargets = new WhisperTarget[TARGET_MAX - TARGET_MIN + 1];
clear();
}
/**
* Assigns the target to a slot.
* @param target The whisper target to assign.
* @return The slot number in range [1, 30].
*/
public byte append(WhisperTarget target) {
byte freeId = -1;
for (byte i = TARGET_MIN; i < TARGET_MAX; i++) {
if ((mTakenIds & (1 << i)) == 0) {
freeId = i;
break;
}
}
if (freeId != -1) {
mActiveTargets[freeId - TARGET_MIN] = target;
}
return freeId;
}
public WhisperTarget get(byte id) {
if ((mTakenIds & (1 << id)) > 0)
return null;
return mActiveTargets[id - TARGET_MIN];
}
public void free(byte slot) {
if (slot < TARGET_MIN || slot > TARGET_MAX)
throw new IllegalArgumentException();
mTakenIds &= ~(1 << slot);
}
public int spaceRemaining() {
int counter = 0;
for (byte i = TARGET_MIN; i < TARGET_MAX; i++) {
if ((mTakenIds & (1 << i)) == 0) {
counter++;
}
}
return counter;
}
public void clear() {
// Slots 0 and 31 are non-whisper targets.
mTakenIds = 1 | (1 << 31);
}
}
|