-
Notifications
You must be signed in to change notification settings - Fork 3
/
ArrayBoolVect.java
94 lines (73 loc) · 2.5 KB
/
ArrayBoolVect.java
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
90
91
92
93
94
/*
Copyright 2022 Massimo Santini
This file is part of "Programmazione 2 @ UniMI" teaching material.
This 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 material 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 file. If not, see <https://www.gnu.org/licenses/>.
*/
import java.util.Arrays;
/** Classe concreta che implementa un {@link BoolVect} denso di taglia assegnata. */
public class ArrayBoolVect extends AbstractBoolVect {
/** I valori del BoolVect. */
private final boolean[] valore;
/** La dimensione del BoolVect. */
private int dimensione = 0;
// RI: * 0 <= dimensione <= valore.length
// * valore[dimensione - 1] == true && valore[p] == false se p >= dimensione
// * valore != null
// AF: BoolVect di taglia valore.length il cui i-esimo valore di verità è l'i-esimo
// elemento dell'array valore
/**
* Costruisce un BoolVect di taglia assegnata.
*
* @param taglia la taglia
* @throws IllegalArgumentException se la taglia non è positiva.
*/
public ArrayBoolVect(final int taglia) {
if (taglia <= 0) throw new IllegalArgumentException("La taglia deve essere positiva.");
valore = new boolean[taglia];
}
/** Costruisce un BoolVect di taglia 1024. */
public ArrayBoolVect() {
this(1024);
}
@Override
public int taglia() {
return valore.length;
}
@Override
public int dimensione() {
return dimensione;
}
@Override
public void pulisci() {
Arrays.fill(valore, false);
}
@Override
public boolean leggiParziale(final int pos) {
return valore[pos];
}
@Override
public void scriviParziale(final int pos, final boolean val) {
valore[pos] = val;
if (val && pos >= dimensione) dimensione = pos + 1;
else if (!val && pos == dimensione - 1)
while (dimensione > 0 && !valore[dimensione - 1]) dimensione--;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof ArrayBoolVect) return Arrays.equals(valore, ((ArrayBoolVect) obj).valore);
return super.equals(obj);
}
@Override
public int hashCode() {
return Arrays.hashCode(valore);
}
}