jaulib v1.3.0
Jau Support Library (C++, Java, ..)
Bitfield.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 * Copyright (c) 2015 Gothel Software e.K.
5 * Copyright (c) 2015 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26package org.jau.util;
27
28import jau.util.SyncedBitfield;
29
30/**
31 * Simple bitfield interface for efficient bit storage access in O(1).
32 * @since 0.3.0
33 */
34public interface Bitfield {
35 /**
36 * Simple {@link Bitfield} factory for returning the efficient implementation.
37 */
38 public static class Factory {
39 /**
40 * Creates am efficient {@link Bitfield} instance based on the requested {@code storageBitSize}.
41 * <p>
42 * Implementation returns a plain 32 bit integer field implementation for
43 * {@code storageBitSize} &le; 32 bits or an 32 bit integer array implementation otherwise.
44 * </p>
45 */
46 public static Bitfield create(final int storageBitSize) {
47 if( 32 >= storageBitSize ) {
48 return new jau.util.Int32Bitfield();
49 } else {
50 return new jau.util.Int32ArrayBitfield(storageBitSize);
51 }
52 }
53 /**
54 * Creates a synchronized {@link Bitfield} by wrapping the given {@link Bitfield} instance.
55 */
56 public static Bitfield synchronize(final Bitfield impl) {
57 return new SyncedBitfield(impl);
58 }
59 }
60
61 /**
62 * Returns the storage size in bit units, e.g. 32 bit for implementations using one {@code int} field.
63 */
64 int size();
65
66
67 /**
68 * Set all bits of this bitfield to the given value {@code bit}.
69 */
70 void clearField(final boolean bit);
71
72 /**
73 * Returns {@code length} bits from this storage,
74 * starting with the lowest bit from the storage position {@code lowBitnum}.
75 * @param lowBitnum storage bit position of the lowest bit, restricted to [0..{@link #size()}-{@code length}].
76 * @param length number of bits to read, constrained to [0..32].
77 * @throws IndexOutOfBoundsException if {@code rightBitnum} is out of bounds
78 * @see #put32(int, int, int)
79 */
80 int get32(final int lowBitnum, final int length) throws IndexOutOfBoundsException;
81
82 /**
83 * Puts {@code length} bits of given {@code data} into this storage,
84 * starting w/ the lowest bit to the storage position {@code lowBitnum}.
85 * @param lowBitnum storage bit position of the lowest bit, restricted to [0..{@link #size()}-{@code length}].
86 * @param length number of bits to write, constrained to [0..32].
87 * @param data the actual bits to be put into this storage
88 * @throws IndexOutOfBoundsException if {@code rightBitnum} is out of bounds
89 * @see #get32(int, int)
90 */
91 void put32(final int lowBitnum, final int length, final int data) throws IndexOutOfBoundsException;
92
93 /**
94 * Copies {@code length} bits at position {@code srcLowBitnum} to position {@code dstLowBitnum}
95 * and returning the bits.
96 * <p>
97 * Implementation shall operate as if invoking {@link #get32(int, int)}
98 * and then {@link #put32(int, int, int)} sequentially.
99 * </p>
100 * @param srcLowBitnum source bit number, restricted to [0..{@link #size()}-1].
101 * @param dstLowBitnum destination bit number, restricted to [0..{@link #size()}-1].
102 * @throws IndexOutOfBoundsException if {@code bitnum} is out of bounds
103 * @see #get32(int, int)
104 * @see #put32(int, int, int)
105 */
106 int copy32(final int srcLowBitnum, final int dstLowBitnum, final int length) throws IndexOutOfBoundsException;
107
108 /**
109 * Return <code>true</code> if the bit at position <code>bitnum</code> is set, otherwise <code>false</code>.
110 * @param bitnum bit number, restricted to [0..{@link #size()}-1].
111 * @throws IndexOutOfBoundsException if {@code bitnum} is out of bounds
112 */
113 boolean get(final int bitnum) throws IndexOutOfBoundsException;
114
115 /**
116 * Set or clear the bit at position <code>bitnum</code> according to <code>bit</code>
117 * and return the previous value.
118 * @param bitnum bit number, restricted to [0..{@link #size()}-1].
119 * @throws IndexOutOfBoundsException if {@code bitnum} is out of bounds
120 */
121 boolean put(final int bitnum, final boolean bit) throws IndexOutOfBoundsException;
122
123 /**
124 * Set the bit at position <code>bitnum</code> according to <code>bit</code>.
125 * @param bitnum bit number, restricted to [0..{@link #size()}-1].
126 * @throws IndexOutOfBoundsException if {@code bitnum} is out of bounds
127 */
128 void set(final int bitnum) throws IndexOutOfBoundsException;
129
130 /**
131 * Clear the bit at position <code>bitnum</code> according to <code>bit</code>.
132 * @param bitnum bit number, restricted to [0..{@link #size()}-1].
133 * @throws IndexOutOfBoundsException if {@code bitnum} is out of bounds
134 */
135 void clear(final int bitnum) throws IndexOutOfBoundsException;
136
137 /**
138 * Copies the bit at position {@code srcBitnum} to position {@code dstBitnum}
139 * and returning <code>true</code> if the bit is set, otherwise <code>false</code>.
140 * @param srcBitnum source bit number, restricted to [0..{@link #size()}-1].
141 * @param dstBitnum destination bit number, restricted to [0..{@link #size()}-1].
142 * @throws IndexOutOfBoundsException if {@code bitnum} is out of bounds
143 */
144 boolean copy(final int srcBitnum, final int dstBitnum) throws IndexOutOfBoundsException;
145
146 /**
147 * Returns the number of one bits within this bitfield.
148 * <p>
149 * Utilizes {#link {@link Bitfield.Util#bitCount(int)}}.
150 * </p>
151 */
152 int bitCount();
153}
Simple synchronized Bitfield by wrapping an existing Bitfield.
Simple Bitfield factory for returning the efficient implementation.
Definition: Bitfield.java:38
static Bitfield synchronize(final Bitfield impl)
Creates a synchronized Bitfield by wrapping the given Bitfield instance.
Definition: Bitfield.java:56
static Bitfield create(final int storageBitSize)
Creates am efficient Bitfield instance based on the requested storageBitSize.
Definition: Bitfield.java:46
Simple bitfield interface for efficient bit storage access in O(1).
Definition: Bitfield.java:34
void clearField(final boolean bit)
Set all bits of this bitfield to the given value bit.
boolean copy(final int srcBitnum, final int dstBitnum)
Copies the bit at position srcBitnum to position dstBitnum and returning true if the bit is set,...
int copy32(final int srcLowBitnum, final int dstLowBitnum, final int length)
Copies length bits at position srcLowBitnum to position dstLowBitnum and returning the bits.
int size()
Returns the storage size in bit units, e.g.
void clear(final int bitnum)
Clear the bit at position bitnum according to bit.
boolean put(final int bitnum, final boolean bit)
Set or clear the bit at position bitnum according to bit and return the previous value.
int bitCount()
Returns the number of one bits within this bitfield.
int get32(final int lowBitnum, final int length)
Returns length bits from this storage, starting with the lowest bit from the storage position lowBitn...
void put32(final int lowBitnum, final int length, final int data)
Puts length bits of given data into this storage, starting w/ the lowest bit to the storage position ...