|
4 | 4 | */ |
5 | 5 | package com.deepak.data.structures.Vector; |
6 | 6 |
|
| 7 | +import java.util.Enumeration; |
| 8 | +import java.util.NoSuchElementException; |
| 9 | + |
7 | 10 | /** |
8 | 11 | * Implementation of Vector |
9 | 12 | * |
10 | 13 | * @author Deepak |
11 | 14 | */ |
12 | | -public class CustomVector { |
| 15 | +public class CustomVector<T> { |
| 16 | + |
| 17 | + /* Array to hold elements */ |
| 18 | + private Object[] elements; |
| 19 | + |
| 20 | + /* Size of vector */ |
| 21 | + private int size; |
| 22 | + |
| 23 | + /* Variable for capacity increment when array gets full */ |
| 24 | + private int capacityIncrement; |
| 25 | + |
| 26 | + /* Default capacity */ |
| 27 | + private final static int DEFAULT_CAPACITY = 10; |
| 28 | + |
| 29 | + /** |
| 30 | + * Constructor |
| 31 | + */ |
| 32 | + public CustomVector() { |
| 33 | + this(DEFAULT_CAPACITY); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Constructor |
| 38 | + * |
| 39 | + * @param capacity |
| 40 | + */ |
| 41 | + public CustomVector(int capacity) { |
| 42 | + this(capacity, 0); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Constructor |
| 47 | + * |
| 48 | + * @param capacity |
| 49 | + * @param capacityIncrement |
| 50 | + */ |
| 51 | + public CustomVector(int capacity, int capacityIncrement) { |
| 52 | + if (capacity < 0) { |
| 53 | + throw new IllegalArgumentException("Illegal Capacity: " + capacity); |
| 54 | + } |
| 55 | + this.elements = new Object[capacity]; |
| 56 | + this.capacityIncrement = capacityIncrement; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Method to return current capacity of vector |
| 61 | + * |
| 62 | + * @return {@link int} |
| 63 | + */ |
| 64 | + public synchronized int capacity() { |
| 65 | + return elements.length; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Method to return current size of vector |
| 70 | + * |
| 71 | + * @return {@link int} |
| 72 | + */ |
| 73 | + public synchronized int size() { |
| 74 | + return size; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Method to check if vector is empty |
| 79 | + * |
| 80 | + * @return {@link int} |
| 81 | + */ |
| 82 | + public synchronized boolean isEmpty() { |
| 83 | + return size() == 0; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Method to return enumeration of elements |
| 88 | + * |
| 89 | + * @return {@link Enumeration} |
| 90 | + */ |
| 91 | + public Enumeration<T> elements() { |
| 92 | + return new Enumeration<T>() { |
| 93 | + int count = 0; |
| 94 | + |
| 95 | + public boolean hasMoreElements() { |
| 96 | + return count < size; |
| 97 | + } |
| 98 | + |
| 99 | + public T nextElement() { |
| 100 | + synchronized (CustomVector.this) { |
| 101 | + if (count < size) { |
| 102 | + return elementData(count++); |
| 103 | + } |
| 104 | + } |
| 105 | + throw new NoSuchElementException("Vector Enumeration"); |
| 106 | + } |
| 107 | + }; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Method to check if vector contains the object |
| 112 | + * |
| 113 | + * @param o |
| 114 | + * @return {@link boolean} |
| 115 | + */ |
| 116 | + public synchronized boolean contains(Object o) { |
| 117 | + return indexOf(o) >= 0; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Method to check the index of a given object |
| 122 | + * |
| 123 | + * @param o |
| 124 | + * @return {@link int} |
| 125 | + */ |
| 126 | + public synchronized int indexOf(Object o) { |
| 127 | + return indexOf(o, 0); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Method to find the index of a given object |
| 132 | + * |
| 133 | + * @param o |
| 134 | + * @param index |
| 135 | + * @return {@link int} |
| 136 | + */ |
| 137 | + private synchronized int indexOf(Object o, int index) { |
| 138 | + /* If object is null, return the first index of null value */ |
| 139 | + if (o == null) { |
| 140 | + for (int i = 0; i < size; i++) { |
| 141 | + if (elements[i] == null) { |
| 142 | + return i; |
| 143 | + } |
| 144 | + } |
| 145 | + } else { |
| 146 | + for (int i = 0; i < size; i++) { |
| 147 | + if (o.equals(elements[i])) { |
| 148 | + return i; |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + return -1; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Method to find element at given index |
| 157 | + * |
| 158 | + * @param index |
| 159 | + * @return {@link T} |
| 160 | + */ |
| 161 | + public synchronized T elementAt(int index) { |
| 162 | + if (index >= size) { |
| 163 | + throw new ArrayIndexOutOfBoundsException(index + " >= " + size); |
| 164 | + } |
| 165 | + return elementData(index); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Method to find the first element |
| 170 | + * |
| 171 | + * @return {@link T} |
| 172 | + */ |
| 173 | + public synchronized T firstElement() { |
| 174 | + if (size == 0) { |
| 175 | + throw new NoSuchElementException(); |
| 176 | + } |
| 177 | + return elementData(0); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Method to find the last element |
| 182 | + * |
| 183 | + * @return {@link T} |
| 184 | + */ |
| 185 | + public synchronized T lastElement() { |
| 186 | + if (size == 0) { |
| 187 | + throw new NoSuchElementException(); |
| 188 | + } |
| 189 | + return elementData(size - 1); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Method to set the element at a given index |
| 194 | + * |
| 195 | + * @param object |
| 196 | + * @param index |
| 197 | + */ |
| 198 | + public synchronized void setElementAt(T object, int index) { |
| 199 | + if (index >= size) { |
| 200 | + throw new ArrayIndexOutOfBoundsException(index + " >= " + size); |
| 201 | + } |
| 202 | + elements[index] = object; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Method to remove the element at a given index |
| 207 | + * |
| 208 | + * @param index |
| 209 | + */ |
| 210 | + public synchronized void removeElementAt(int index) { |
| 211 | + if (index >= size) { |
| 212 | + throw new ArrayIndexOutOfBoundsException(index + " >= " + size); |
| 213 | + } else if (index < 0) { |
| 214 | + throw new ArrayIndexOutOfBoundsException(index); |
| 215 | + } |
| 216 | + int j = size - index - 1; |
| 217 | + if (j > 0) { |
| 218 | + System.arraycopy(elements, index + 1, elements, index, j); |
| 219 | + } |
| 220 | + size--; |
| 221 | + elements[size] = null; |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Method to insert element at a given index |
| 226 | + * |
| 227 | + * @param element |
| 228 | + * @param index |
| 229 | + */ |
| 230 | + public synchronized void insertElementAt(T element, int index) { |
| 231 | + if (index >= size) { |
| 232 | + throw new ArrayIndexOutOfBoundsException(index + " >= " + |
| 233 | + size); |
| 234 | + } |
| 235 | + ensureCapacity(size + 1); |
| 236 | + System.arraycopy(elements, index, elements, index + 1, size - index); |
| 237 | + elements[index] = element; |
| 238 | + size++; |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * Method to add element to vector |
| 243 | + * |
| 244 | + * @param element |
| 245 | + */ |
| 246 | + public synchronized void addElement(T element) { |
| 247 | + ensureCapacity(size + 1); |
| 248 | + elements[size++] = element; |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * Method to get a element at given index |
| 253 | + * |
| 254 | + * @param index |
| 255 | + * @return {@link T} |
| 256 | + */ |
| 257 | + public synchronized T get(int index) { |
| 258 | + if (index >= size) { |
| 259 | + throw new ArrayIndexOutOfBoundsException(index); |
| 260 | + } |
| 261 | + return elementData(index); |
| 262 | + } |
| 263 | + |
| 264 | + /** |
| 265 | + * Method to ensure capacity |
| 266 | + * |
| 267 | + * @param newSize |
| 268 | + */ |
| 269 | + private void ensureCapacity(int newSize) { |
| 270 | + if (newSize >= size) { |
| 271 | + /* If capacity increment is given, use that else double the size */ |
| 272 | + if (capacityIncrement > 0) { |
| 273 | + System.arraycopy(elements, 0, elements, size, size + capacityIncrement); |
| 274 | + } else { |
| 275 | + System.arraycopy(elements, 0, elements, size, size * 2); |
| 276 | + } |
| 277 | + } |
| 278 | + } |
| 279 | + |
| 280 | + /** |
| 281 | + * Method to return a type casted value at given index |
| 282 | + * |
| 283 | + * @param index |
| 284 | + * @return {@link T} |
| 285 | + */ |
| 286 | + @SuppressWarnings("unchecked") |
| 287 | + T elementData(int index) { |
| 288 | + return (T) elements[index]; |
| 289 | + } |
13 | 290 |
|
14 | 291 | } |
0 commit comments