File: IntArray.java

package info (click to toggle)
beagle 5.1-200518%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,480 kB
  • sloc: java: 14,430; sh: 55; makefile: 11
file content (220 lines) | stat: -rw-r--r-- 7,174 bytes parent folder | download
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Copyright (C) 2014-2016 Brian L. Browning
 *
 * This file is part of Beagle
 *
 * Beagle 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.
 *
 * Beagle 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 ints;

import java.util.Arrays;

/**
 * <p>Interface {@code IntArray} represents an immutable {@code int[]} array.
 * </p>
 * Instances of class {@code IntArray} are required to be immutable.
 *
 * @author Brian L. Browning {@code <browning@uw.edu>}
 */
public interface IntArray {

    /**
     * Returns the number of elements in this {@code IntArray}.
     * @return the number of elements in this {@code IntArray}
     */
    int size();

    /**
     * Returns the specified array element.
     * @param index an array index
     * @return the specified array element
     * @throws IndexOutOfBoundsException if
     * {@code index < 0 ||  index >= this.size()}
     */
    int get(int index);

    /**
     * Returns an {@code IntArray} instance of the specified size whose
     * {@code k}-th element is {@code k}.
     * @param size the size of the returned array
     * @return the identity {@code IntArray} having the specified size.
     */
    static IntArray identity(int size) {
        return new IntArray() {
            @Override
            public int size() {
                return size;
            }

            @Override
            public int get(int index) {
                if (index<0 || index>=size) {
                    throw new IllegalArgumentException(String.valueOf(size));
                }
                return index;
            }
        } ;
    }

    /**
     * Returns a copy of the specified array.
     * @param ia a list of integers
     * @return a copy of the specified array
     * @throws NullPointerException if {@code ia == null}
     */
    static int[] toArray(IntArray ia) {
        int[] copy = new int[ia.size()];
        for (int j=0; j<copy.length; ++j) {
            copy[j] = ia.get(j);
        }
        return copy;
    }

    /**
     * Returns a string representation of this {@code IntArray} by applying
     * {@code java.utils.Arrays.toString()} to an equivalent {@code int[]}
     * object.
     *
     * @param ia a list of integers
     * @return a string representation of this {@code IntArray}.
     * @throws NullPointerException if {@code ia == null}
     */
    static String asString(IntArray ia) {
        return Arrays.toString(toArray(ia));
    }

    /**
     * Returns {@code true} if the specified {@code IntArray} objects
     * represent the same sequence of integer values, and returns {@code false}
     * otherwise.
     * @param a a sequence of integer values
     * @param b a sequence of integer values
     * @return {@code true} if the specified {@code IntArray} objects
     * represent the same sequence of integer values
     */
    static boolean equals(IntArray a, IntArray b) {
        if (a==b) {
            return true;
        }
        else if (a.size()!=b.size()) {
            return false;
        }
        else {
            for (int j=0, n=a.size(); j<n; ++j) {
                if (a.get(j)!=b.get(j)) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * Returns the maximum element, or {@code Integer.MIN_VALUE} if
     * {@code this.size() == 0}.
     * @param ia a list of integers
     * @return the maximum element
     * @throws NullPointerException if {@code ia == null}
     */
    static int max(IntArray ia) {
        int max = Integer.MIN_VALUE;
        for (int j=0, n=ia.size(); j<n; ++j) {
            if (ia.get(j) > max) {
                max = ia.get(j);
            }
        }
        return max;
    }

    /**
     * Returns the minimum element, or {@code Integer.MAX_VALUE} if
     * {@code this.size() == 0}.
     * @param ia a list of integers
     * @return the minimum element
     * @throws NullPointerException if {@code ia == null}
     */
    static int min(IntArray ia) {
        int min = Integer.MAX_VALUE;
        for (int j=0, n=ia.size(); j<n; ++j) {
            if (ia.get(j) < min) {
                min = ia.get(j);
            }
        }
        return min;
    }

    /**
     * Returns a new {@code IntArray} instance that has the same
     * sequence of integers as the specified array.
     * @param ia the array of integers to be copied
     * @param valueSize the exclusive end of the range of non-negative
     * array values
     * @return a new {@code IntArray} instance that has
     * the same sequence of integers as the specified array
     * @throws IllegalArgumentException if {@code valueSize < 1}
     * @throws IllegalArgumentException if
     * {@code (ia[j] < 0 || ia[j] > valueSize)} for any index {@code j}
     * satisfying  {@code (j >= 0 && j < ia.length)}
     * @throws NullPointerException if {@code ia == null}
     */
    static IntArray create(int[] ia, int valueSize) {
        if (valueSize < 1) {
            throw new IllegalArgumentException(String.valueOf(valueSize));
        }
        if (valueSize<=16) {
            return new PackedIntArray(ia, valueSize);
        }
        else if (valueSize<=256) {
            return new UnsignedByteArray(ia, valueSize);
        }
        else if (valueSize<=65536) {
            return new CharArray(ia, valueSize);
        }
        else {
            return new WrappedIntArray(ia, valueSize);
        }
    }

    /**
     * Returns a new {@code IntArray} instance that has the same
     * sequence of integers as the specified list.
     * @param il the list of integers to be copied
     * @param valueSize the exclusive end of the range of non-negative
     * array values
     * @return a new {@code IntArray} instance that has
     * the same sequence of integers as the specified list
     * @throws IllegalArgumentException if {@code valueSize < 1}
     * @throws IllegalArgumentException if
     * {@code (ia[j] < 0 || ia[j] > valueSize)} for any index {@code j}
     * satisfying  {@code (j >= 0 && j < ia.length)}
     * @throws NullPointerException if {@code ia == null}
     */
    static IntArray create(IntList il, int valueSize) {
        if (valueSize < 1) {
            throw new IllegalArgumentException(String.valueOf(valueSize));
        }
        if (valueSize<=16) {
            return new PackedIntArray(il, valueSize);
        }
        else if (valueSize<=256) {
            return new UnsignedByteArray(il, valueSize);
        }
        else if (valueSize<=65536) {
            return new CharArray(il, valueSize);
        }
        else {
            return new WrappedIntArray(il, valueSize);
        }
    }
}