comparison src/org/tmatesoft/hg/internal/IntSliceSeq.java @ 674:cce0387c6041

Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 17 Jul 2013 15:40:51 +0200
parents
children 19f5167c2155
comparison
equal deleted inserted replaced
673:545b1d4cc11d 674:cce0387c6041
1 /*
2 * Copyright (c) 2013 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.internal;
18
19 import java.util.Iterator;
20 import java.util.NoSuchElementException;
21
22 /**
23 *
24 * @author Artem Tikhomirov
25 * @author TMate Software Ltd.
26 */
27 public final class IntSliceSeq implements Iterable<IntTuple> {
28 private final IntVector slices;
29 private final int slice;
30
31 public IntSliceSeq(int sliceSize) {
32 // initial size/grow values are pure guess
33 this(sliceSize, 10, 5);
34 }
35
36 public IntSliceSeq(int sliceSize, int initialSlices, int slicesToGrow) {
37 slices = new IntVector(sliceSize * initialSlices, sliceSize*slicesToGrow);
38 slice = sliceSize;
39 }
40
41 public IntSliceSeq add(int... values) {
42 checkValues(values);
43 slices.add(values);
44 return this;
45 }
46
47 public IntSliceSeq set(int sliceIndex, int... values) {
48 checkValues(values);
49 for (int i = 0, j = sliceIndex*slice; i < slice; i++,j++) {
50 slices.set(j, values[i]);
51 }
52 return this;
53 }
54
55 public IntTuple get(int sliceIndex) {
56 checkArgRange(size(), sliceIndex);
57 return new IntTuple(slice).set(slices, sliceIndex*slice);
58 }
59
60 public int get(int sliceIndex, int valueIndex) {
61 checkArgRange(size(), sliceIndex);
62 checkArgRange(slice, valueIndex);
63 return slices.get(sliceIndex*slice + valueIndex);
64 }
65
66 public int size() {
67 return slices.size() / slice;
68 }
69
70 public int sliceSize() {
71 return slice;
72 }
73
74 public void clear() {
75 slices.clear();
76 }
77
78 public IntTuple last() {
79 int lastElementIndex = (size() - 1);
80 if (lastElementIndex < 0) {
81 throw new NoSuchElementException();
82 }
83 return get(lastElementIndex);
84 }
85
86 public Iterator<IntTuple> iterator() {
87 return new Iterator<IntTuple>() {
88 private final IntTuple t = new IntTuple(slice);
89 private int next = 0;
90
91 public boolean hasNext() {
92 return next < size();
93 }
94
95 public IntTuple next() {
96 return t.set(slices, next++*slice);
97 }
98
99 public void remove() {
100 throw new UnsupportedOperationException();
101 }
102 };
103 }
104
105 private void checkArgRange(int rangeSize, int index) {
106 if (index >= 0 && index < rangeSize) {
107 return;
108 }
109 throw new IllegalArgumentException(String.valueOf(index));
110 }
111 private void checkValues(int[] values) {
112 if (values == null || values.length != slice) {
113 throw new IllegalArgumentException(String.valueOf(values == null ? values : values.length));
114 }
115 }
116 }