View Javadoc

1   /**
2    * Copyright (c) 2012, University of Konstanz, Distributed Systems Group
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    * * Redistributions of source code must retain the above copyright
8    * notice, this list of conditions and the following disclaimer.
9    * * Redistributions in binary form must reproduce the above copyright
10   * notice, this list of conditions and the following disclaimer in the
11   * documentation and/or other materials provided with the distribution.
12   * * Neither the name of the University of Konstanz nor the
13   * names of its contributors may be used to endorse or promote products
14   * derived from this software without specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   */
27  package org.perfidix.example.list;
28  
29  import java.util.Iterator;
30  
31  import org.perfidix.example.list.Array;
32  
33  /**
34   * This is a simple container for native integer arrays.
35   * 
36   * @author BaseX Team 2005-11, BSD License
37   * @author Christian Gruen
38   */
39  public final class IntArrayList extends ElementList implements Iterable<int[]> {
40      /** Elements container. */
41      int[][] list;
42  
43      /**
44       * Default constructor.
45       */
46      public IntArrayList() {
47          this(CAP);
48      }
49  
50      /**
51       * Constructor, specifying an initial array capacity.
52       * 
53       * @param c
54       *            initial capacity
55       */
56      public IntArrayList(final int c) {
57          list = new int[c][];
58      }
59  
60      /**
61       * Adds an element.
62       * 
63       * @param e
64       *            element to be added
65       */
66      public void add(final int[] e) {
67          if (size == list.length)
68              list = Array.copyOf(list, newSize());
69          list[size++] = e;
70      }
71  
72      /**
73       * Returns the element at the specified index.
74       * 
75       * @param i
76       *            index
77       * @return element
78       */
79      public int[] get(final int i) {
80          return list[i];
81      }
82  
83      /**
84       * Sets an element at the specified index.
85       * 
86       * @param i
87       *            index
88       * @param e
89       *            element to be set
90       */
91      public void set(final int i, final int[] e) {
92          if (i >= list.length)
93              list = Array.copyOf(list, newSize(i + 1));
94          list[i] = e;
95          size = Math.max(size, i + 1);
96      }
97  
98      @Override
99      public Iterator<int[]> iterator() {
100         return new Iterator<int[]>() {
101             private int c = -1;
102 
103             @Override
104             public boolean hasNext() {
105                 return ++c < size;
106             }
107 
108             @Override
109             public int[] next() {
110                 return list[c];
111             }
112 
113             @Override
114             public void remove() {
115                 // Util.notexpected();
116             }
117         };
118     }
119 }