comparison src/org/tmatesoft/hg/internal/IntVector.java @ 551:4ea0351ca878

Better (precise) name for diff facility, tests
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 20 Feb 2013 18:19:52 +0100
parents 83afa680555d
children 45751456b471
comparison
equal deleted inserted replaced
550:c1478cc31f45 551:4ea0351ca878
40 this.increment = increment; 40 this.increment = increment;
41 } 41 }
42 42
43 public void add(int v) { 43 public void add(int v) {
44 if (count == data.length) { 44 if (count == data.length) {
45 grow(); 45 grow(0);
46 } 46 }
47 data[count++] = v; 47 data[count++] = v;
48 }
49
50 public void add(int... values) {
51 if (count + values.length > data.length) {
52 grow(count + values.length - data.length);
53 }
54 for (int v : values) {
55 data[count++] = v;
56 }
48 } 57 }
49 58
50 public int get(int i) { 59 public int get(int i) {
51 if (i < 0 || i >= count) { 60 if (i < 0 || i >= count) {
52 throw new IndexOutOfBoundsException(String.format("Index: %d, size: %d", i, count)); 61 throw new IndexOutOfBoundsException(String.format("Index: %d, size: %d", i, count));
93 return data; 102 return data;
94 } 103 }
95 return toArray(); 104 return toArray();
96 } 105 }
97 106
98 private void grow() { 107 private void grow(int newCapacityHint) {
99 if (increment == 0) { 108 if (increment == 0) {
100 throw new UnsupportedOperationException("This vector is not allowed to expand"); 109 throw new UnsupportedOperationException("This vector is not allowed to expand");
101 } 110 }
102 int newCapacity = increment < 0 ? data.length << 1 : data.length + increment; 111 int newCapacity = increment < 0 ? data.length << 1 : data.length + increment;
112 if (newCapacityHint > 0 && newCapacity < newCapacityHint) {
113 newCapacity = newCapacityHint;
114 }
103 assert newCapacity > 0 && newCapacity != data.length : newCapacity; 115 assert newCapacity > 0 && newCapacity != data.length : newCapacity;
104 int[] newData = new int[newCapacity]; 116 int[] newData = new int[newCapacity];
105 System.arraycopy(data, 0, newData, 0, count); 117 System.arraycopy(data, 0, newData, 0, count);
106 data = newData; 118 data = newData;
107 } 119 }