Welcome to mirror list, hosted at ThFree Co, Russian Federation.

WhisperTargetList.java « model « humla « lublin « se « java « main « src - gitlab.com/quite/humla.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ae76454bdefc9e63e02f486fc9dbb1ee7e6a9be1 (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
86
87
88
89
/*
 * 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;
            mTakenIds |= (1 << freeId);
        }

        return freeId;
    }

    public WhisperTarget get(byte id) {
        if (id < TARGET_MIN || id > TARGET_MAX)
            throw new IndexOutOfBoundsException();

        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 IndexOutOfBoundsException();

        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() {
        mTakenIds = 0;
    }
}