1/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved. 2 * 3 * This program and the accompanying materials are made available under 4 * the terms of the Common Public License v1.0 which accompanies this distribution, 5 * and is available at http://www.eclipse.org/legal/cpl-v10.html 6 * 7 * $Id: IntVector.java,v 1.1.1.1 2004/05/09 16:57:53 vlad_r Exp $ 8 */ 9package com.vladium.util; 10 11// ---------------------------------------------------------------------------- 12/** 13 * @author Vlad Roubtsov, (C) 2001 14 */ 15public 16final class IntVector implements Cloneable 17{ 18 // public: ................................................................ 19 20 public IntVector () 21 { 22 this (5); 23 } 24 25 public IntVector (final int initCapacity) 26 { 27 m_values = new int [initCapacity]; 28 } 29 30 // ACCESSORS: 31 32 public int get (final int index) 33 { 34 if (index > m_size - 1) 35 throw new IndexOutOfBoundsException ("get[" + index + "] on vector of size " + m_size); 36 37 return m_values [index]; 38 } 39 40 public int [] values () 41 { 42 if (m_size == 0) 43 return IConstants.EMPTY_INT_ARRAY; 44 else 45 { 46 final int size = m_size; 47 final int [] result = new int [size]; 48 49 if (size < COPY_THRESHOLD) 50 { 51 for (int i = 0; i < size; ++ i) result [i] = m_values [i]; 52 } 53 else 54 { 55 System.arraycopy (m_values, 0, result, 0, size); 56 } 57 58 return result; 59 } 60 } 61 62 public int size () 63 { 64 return m_size; 65 } 66 67 // Cloneable: 68 69 /** 70 * Performs deep copy. 71 */ 72 public Object clone () 73 { 74 try 75 { 76 final IntVector _clone = (IntVector) super.clone (); 77 78 // deep clone: 79 if (m_size < COPY_THRESHOLD) 80 { 81 _clone.m_values = new int [m_values.length]; 82 final int [] _clone_values = _clone.m_values; 83 for (int i = 0; i < m_size; ++ i) _clone_values [i] = m_values [i]; 84 } 85 else 86 { 87 _clone.m_values = (int []) m_values.clone (); 88 } 89 90 return _clone; 91 } 92 catch (CloneNotSupportedException e) 93 { 94 throw new InternalError (e.toString ()); 95 } 96 } 97 98 public String toString () 99 { 100 final StringBuffer s = new StringBuffer (super.toString() + ", size " + m_size + ": "); 101 for (int i = 0; i < m_size; ++ i) 102 { 103 if (i > 0) s.append (", "); 104 s.append (m_values [i]); 105 } 106 107 return s.toString (); 108 } 109 110 // MUTATORS: 111 112 public int set (final int index, final int value) 113 { 114 if (index > m_size - 1) 115 throw new IndexOutOfBoundsException ("get[" + index + "] on vector of size " + m_size); 116 117 final int current_value = m_values [index]; 118 m_values [index] = value; 119 120 return current_value; 121 } 122 123 public void add (final int value) 124 { 125 final int capacity = m_values.length; 126 if (capacity == m_size) 127 { 128 final int [] values = new int [1 + (capacity << 1)]; 129 if (capacity < COPY_THRESHOLD) 130 { 131 for (int i = 0; i < capacity; ++ i) values [i] = m_values [i]; 132 } 133 else 134 { 135 System.arraycopy (m_values, 0, values, 0, capacity); 136 } 137 138 m_values = values; 139 } 140 141 m_values [m_size ++] = value; 142 } 143 144 // protected: ............................................................. 145 146 // package: ............................................................... 147 148 // private: ............................................................... 149 150 151 private int [] m_values; // never null 152 private int m_size; 153 154 private static final int COPY_THRESHOLD = 10; 155 156} // end of class 157// ----------------------------------------------------------------------------