File: BigListIterators.drv

package info (click to toggle)
libfastutil-java 6.5.15-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,716 kB
  • ctags: 1,035
  • sloc: java: 9,711; sh: 588; makefile: 423; xml: 211
file content (176 lines) | stat: -rw-r--r-- 6,483 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*		 
 * Copyright (C) 2002-2014 Sebastiano Vigna 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */


package PACKAGE;

import java.util.Iterator;
import java.util.NoSuchElementException;

/** A class providing static methods and objects that do useful things with type-specific iterators.
 *
 * @see Iterator
 */

public class BIG_LIST_ITERATORS {

	private BIG_LIST_ITERATORS() {}
	
	/** A class returning no elements and a type-specific big list iterator interface.
	 *
	 * <P>This class may be useful to implement your own in case you subclass
	 * a type-specific iterator.
	 */

	public static class EmptyBigListIterator KEY_GENERIC extends KEY_ABSTRACT_BIG_LIST_ITERATOR KEY_GENERIC implements java.io.Serializable, Cloneable {

		private static final long serialVersionUID = -7046029254386353129L;

		protected EmptyBigListIterator() {}

		public boolean hasNext() { return false; }
		public boolean hasPrevious() { return false; }
		public KEY_GENERIC_TYPE NEXT_KEY() { throw new NoSuchElementException(); }
		public KEY_GENERIC_TYPE PREV_KEY() { throw new NoSuchElementException(); }
		public long nextIndex() { return 0; }
		public long previousIndex() { return -1; }
		public long skip( long n ) { return 0; };
		public long back( long n ) { return 0; };
		public Object clone() { return EMPTY_BIG_LIST_ITERATOR; }
        private Object readResolve() { return EMPTY_BIG_LIST_ITERATOR; }
	}

	/** An empty iterator (immutable). It is serializable and cloneable.
	 *
	 * <P>The class of this objects represent an abstract empty iterator
	 * that can iterate as a type-specific (list) iterator.
	 */

	@SuppressWarnings("rawtypes")
	public final static EmptyBigListIterator EMPTY_BIG_LIST_ITERATOR = new EmptyBigListIterator();


	/** An iterator returning a single element. */

	private static class SingletonBigListIterator KEY_GENERIC extends KEY_ABSTRACT_BIG_LIST_ITERATOR KEY_GENERIC {
		private final KEY_GENERIC_TYPE element;
		private int curr;

		public SingletonBigListIterator( final KEY_GENERIC_TYPE element ) {
			this.element = element;
		}
	
		public boolean hasNext() { return curr == 0; }
		public boolean hasPrevious() { return curr == 1; }

		public KEY_GENERIC_TYPE NEXT_KEY() {
			if ( ! hasNext() ) throw new NoSuchElementException();
			curr = 1;
			return element;
		}

		public KEY_GENERIC_TYPE PREV_KEY() {
			if ( ! hasPrevious() ) throw new NoSuchElementException();
			curr = 0;
			return element;
		}

		public long nextIndex() {
			return curr;
		}

		public long previousIndex() {
			return curr - 1;
		}
	}


	/** Returns an iterator that iterates just over the given element.
	 *
	 * @param element the only element to be returned by a type-specific list iterator.
	 * @return  an iterator that iterates just over <code>element</code>.
	 */
	public static KEY_GENERIC KEY_BIG_LIST_ITERATOR KEY_GENERIC singleton( final KEY_GENERIC_TYPE element ) {
		return new SingletonBigListIterator KEY_GENERIC( element );
	}


  	/** An unmodifiable wrapper class for big list iterators. */

	public static class UnmodifiableBigListIterator KEY_GENERIC extends KEY_ABSTRACT_BIG_LIST_ITERATOR KEY_GENERIC {
		final protected KEY_BIG_LIST_ITERATOR KEY_GENERIC i;

		@SuppressWarnings("unchecked")
		public UnmodifiableBigListIterator( final KEY_BIG_LIST_ITERATOR KEY_GENERIC i ) {
			this.i = i;
		}

		public boolean hasNext() { return i.hasNext(); }
		public boolean hasPrevious() { return i.hasPrevious(); }
		public KEY_GENERIC_TYPE NEXT_KEY() { return i.NEXT_KEY(); }
		public KEY_GENERIC_TYPE PREV_KEY() { return i.PREV_KEY(); }
		public long nextIndex() { return i.nextIndex(); }
		public long previousIndex() { return i.previousIndex(); }
#if #keys(primitive)
		public KEY_GENERIC_CLASS next() { return i.next(); }
		public KEY_GENERIC_CLASS previous() { return i.previous(); }
#endif
	}

	/** Returns an unmodifiable list iterator backed by the specified list iterator.
	 *
	 * @param i the list iterator to be wrapped in an unmodifiable list iterator.
	 * @return an unmodifiable view of the specified list iterator.
	 */
	public static KEY_GENERIC KEY_BIG_LIST_ITERATOR KEY_GENERIC unmodifiable( final KEY_BIG_LIST_ITERATOR KEY_GENERIC i ) { return new UnmodifiableBigListIterator KEY_GENERIC( i ); }


	/** A class exposing a list iterator as a big-list iterator.. */

	public static class BigListIteratorListIterator KEY_GENERIC extends KEY_ABSTRACT_BIG_LIST_ITERATOR KEY_GENERIC {
		protected final KEY_LIST_ITERATOR KEY_GENERIC i;

		protected BigListIteratorListIterator( final KEY_LIST_ITERATOR KEY_GENERIC i ) {
			this.i = i;
		}

		private int intDisplacement( long n ) {
			if ( n < Integer.MIN_VALUE || n > Integer.MAX_VALUE ) throw new IndexOutOfBoundsException( "This big iterator is restricted to 32-bit displacements" );
			return (int)n;
		}
	
		public void set( KEY_GENERIC_TYPE ok ) { i.set( ok ); }
		public void add( KEY_GENERIC_TYPE ok ) { i.add( ok ); }
		public int back( int n ) { return i.back( n ); }
		public long back( long n ) { return i.back( intDisplacement( n ) ); }
		public void remove() { i.remove(); }
		public int skip( int n ) { return i.skip( n ); }
		public long skip( long n ) { return i.skip( intDisplacement( n ) ); }
		public boolean hasNext() { return i.hasNext(); }
		public boolean hasPrevious() { return i.hasPrevious(); }
		public KEY_GENERIC_TYPE NEXT_KEY() { return i.NEXT_KEY(); }
		public KEY_GENERIC_TYPE PREV_KEY() { return i.PREV_KEY(); }
		public long nextIndex() { return i.nextIndex(); }
		public long previousIndex() { return i.previousIndex(); }
	}

	 /** Returns a big-list iterator backed by the specified list iterator.
	  *
	  * @param i the list iterator to adapted to the big-list-iterator interface.
	  * @return a big-list iterator backed by the specified list iterator.
	  */
	public static KEY_GENERIC KEY_BIG_LIST_ITERATOR KEY_GENERIC asBigListIterator( final KEY_LIST_ITERATOR KEY_GENERIC i ) { return new BigListIteratorListIterator KEY_GENERIC( i ); }
}