001: /*
002: * Primitive Collections for Java.
003: * Copyright (C) 2002, 2003 Søren Bak
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package bak.pcj.list;
020:
021: import bak.pcj.DoubleIterator;
022: import bak.pcj.DoubleCollection;
023: import bak.pcj.AbstractDoubleCollection;
024: import bak.pcj.hash.DefaultDoubleHashFunction;
025: import bak.pcj.util.Exceptions;
026:
027: /**
028: * This class represents an abstract base for implementing
029: * lists of double values. All operations that can be implemented
030: * using iterators and the <tt>get()</tt> and <tt>set()</tt> methods
031: * are implemented as such. In most cases, this is
032: * hardly an efficient solution, and at least some of those
033: * methods should be overridden by sub-classes.
034: *
035: * @author Søren Bak
036: * @version 1.2 21-08-2003 19:14
037: * @since 1.0
038: */
039: public abstract class AbstractDoubleList extends
040: AbstractDoubleCollection implements DoubleList {
041:
042: /** Default constructor to be invoked by sub-classes. */
043: protected AbstractDoubleList() {
044: }
045:
046: public boolean add(double v) {
047: add(size(), v);
048: return true;
049: }
050:
051: /**
052: * Throws <tt>UnsupportedOperationException</tt>.
053: *
054: * @throws UnsupportedOperationException
055: * unconditionally.
056: */
057: public void add(int index, double v) {
058: Exceptions.unsupported("add");
059: }
060:
061: public boolean addAll(int index, DoubleCollection c) {
062: if (index < 0 || index > size())
063: Exceptions.indexOutOfBounds(index, 0, size());
064: DoubleIterator i = c.iterator();
065: boolean result = i.hasNext();
066: while (i.hasNext()) {
067: add(index, i.next());
068: index++;
069: }
070: return result;
071: }
072:
073: public int indexOf(double c) {
074: return indexOf(0, c);
075: }
076:
077: /**
078: * @since 1.2
079: */
080: public int indexOf(int index, double c) {
081: DoubleListIterator i = listIterator(index);
082: while (i.hasNext())
083: if (i.next() == c)
084: return i.previousIndex();
085: return -1;
086: }
087:
088: public DoubleIterator iterator() {
089: return listIterator();
090: }
091:
092: public int lastIndexOf(double c) {
093: DoubleListIterator i = listIterator(size());
094: while (i.hasPrevious())
095: if (i.previous() == c)
096: return i.nextIndex();
097: return -1;
098: }
099:
100: public int lastIndexOf(int index, double c) {
101: DoubleListIterator i = listIterator(index);
102: while (i.hasPrevious())
103: if (i.previous() == c)
104: return i.nextIndex();
105: return -1;
106: }
107:
108: public DoubleListIterator listIterator() {
109: return listIterator(0);
110: }
111:
112: public DoubleListIterator listIterator(final int index) {
113: if (index < 0 || index > size())
114: Exceptions.indexOutOfBounds(index, 0, size());
115:
116: return new DoubleListIterator() {
117: private int ptr = index;
118: private int lptr = -1;
119:
120: // -------------------------------------------------------
121: // Implementation of Iterator
122: // -------------------------------------------------------
123:
124: public boolean hasNext() {
125: return ptr < size();
126: }
127:
128: public double next() {
129: if (ptr == size())
130: Exceptions.endOfIterator();
131: lptr = ptr++;
132: return get(lptr);
133: }
134:
135: public void remove() {
136: if (lptr == -1)
137: Exceptions.noElementToRemove();
138: AbstractDoubleList.this .removeElementAt(lptr);
139: if (lptr < ptr)
140: ptr--;
141: lptr = -1;
142: }
143:
144: // -------------------------------------------------------
145: // Implementation of ListIterator
146: // -------------------------------------------------------
147:
148: public void add(double v) {
149: AbstractDoubleList.this .add(ptr++, v);
150: lptr = -1;
151: }
152:
153: public boolean hasPrevious() {
154: return ptr > 0;
155: }
156:
157: public int nextIndex() {
158: return ptr;
159: }
160:
161: public double previous() {
162: if (ptr == 0)
163: Exceptions.startOfIterator();
164: ptr--;
165: lptr = ptr;
166: return get(ptr);
167: }
168:
169: public int previousIndex() {
170: return ptr - 1;
171: }
172:
173: public void set(double v) {
174: if (lptr == -1)
175: Exceptions.noElementToSet();
176: AbstractDoubleList.this .set(lptr, v);
177: }
178:
179: };
180: }
181:
182: /**
183: * Throws <tt>UnsupportedOperationException</tt>.
184: *
185: * @throws UnsupportedOperationException
186: * unconditionally.
187: */
188: public double removeElementAt(int index) {
189: Exceptions.unsupported("removeElementAt");
190: throw new RuntimeException();
191: }
192:
193: public boolean equals(Object obj) {
194: if (this == obj)
195: return true;
196: if (!(obj instanceof DoubleList))
197: return false;
198: DoubleListIterator i1 = listIterator();
199: DoubleListIterator i2 = ((DoubleList) obj).listIterator();
200: while (i1.hasNext() && i2.hasNext())
201: if (i1.next() != i2.next())
202: return false;
203: return !(i1.hasNext() || i2.hasNext());
204: }
205:
206: public int hashCode() {
207: int h = 1;
208: DoubleIterator i = iterator();
209: while (i.hasNext())
210: h = (int) (31 * h + DefaultDoubleHashFunction.INSTANCE
211: .hash(i.next()));
212: return h;
213: }
214:
215: }
|