Mercurial > hg4j
comparison src/org/tmatesoft/hg/internal/IntVector.java @ 288:b11f6a08f748
Avoid boxing int values and list resizes on revlog read
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Sat, 10 Sep 2011 00:18:39 +0200 |
parents | |
children | a674b8590362 |
comparison
equal
deleted
inserted
replaced
287:ed6b74a58c66 | 288:b11f6a08f748 |
---|---|
1 /* | |
2 * Copyright (c) 2011 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 /** | |
20 * | |
21 * @author Artem Tikhomirov | |
22 * @author TMate Software Ltd. | |
23 */ | |
24 class IntVector { | |
25 | |
26 private int[] data; | |
27 private final int increment; | |
28 private int count; | |
29 | |
30 | |
31 public IntVector() { | |
32 this(16, -1); | |
33 } | |
34 | |
35 // increment == -1: grow by power of two. | |
36 // increment == 0: no resize (Exception will be thrown on attempt to add past capacity) | |
37 public IntVector(int initialCapacity, int increment) { | |
38 data = new int[initialCapacity]; | |
39 this.increment = increment; | |
40 } | |
41 | |
42 public void add(int v) { | |
43 if (count == data.length) { | |
44 grow(); | |
45 } | |
46 data[count++] = v; | |
47 } | |
48 | |
49 public int get(int i) { | |
50 if (i < 0 || i >= count) { | |
51 throw new IndexOutOfBoundsException(String.format("Index: %d, size: %d", i, count)); | |
52 } | |
53 return data[i]; | |
54 } | |
55 | |
56 public int size() { | |
57 return count; | |
58 } | |
59 | |
60 public int[] toArray() { | |
61 int[] rv = new int[count]; | |
62 System.arraycopy(data, 0, rv, 0, count); | |
63 return rv; | |
64 } | |
65 | |
66 /** | |
67 * Use only when this instance won't be used any longer | |
68 */ | |
69 @Experimental | |
70 int[] toArray(boolean internalIfSizeMatchCapacity) { | |
71 if (count == data.length) { | |
72 return data; | |
73 } | |
74 return toArray(); | |
75 } | |
76 | |
77 private void grow() { | |
78 if (increment == 0) { | |
79 // throw specific exception right away | |
80 return; | |
81 } | |
82 int newCapacity = increment < 0 ? data.length << 1 : data.length + increment; | |
83 assert newCapacity > 0 && newCapacity != data.length : newCapacity; | |
84 int[] newData = new int[newCapacity]; | |
85 System.arraycopy(data, 0, newData, 0, count); | |
86 data = newData; | |
87 } | |
88 } |