1 /* |
1 /* |
2 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
50 return lb; |
50 return lb; |
51 } |
51 } |
52 |
52 |
53 /** The list of elements of this buffer. |
53 /** The list of elements of this buffer. |
54 */ |
54 */ |
55 public List<A> elems; |
55 private List<A> elems; |
56 |
56 |
57 /** A pointer pointing to the last, sentinel element of `elems'. |
57 /** A pointer pointing to the last element of 'elems' containing data, |
58 */ |
58 * or null if the list is empty. |
59 public List<A> last; |
59 */ |
|
60 private List<A> last; |
60 |
61 |
61 /** The number of element in this buffer. |
62 /** The number of element in this buffer. |
62 */ |
63 */ |
63 public int count; |
64 private int count; |
64 |
65 |
65 /** Has a list been created from this buffer yet? |
66 /** Has a list been created from this buffer yet? |
66 */ |
67 */ |
67 public boolean shared; |
68 private boolean shared; |
68 |
69 |
69 /** Create a new initially empty list buffer. |
70 /** Create a new initially empty list buffer. |
70 */ |
71 */ |
71 public ListBuffer() { |
72 public ListBuffer() { |
72 clear(); |
73 clear(); |
73 } |
74 } |
74 |
75 |
75 public final void clear() { |
76 public final void clear() { |
76 this.elems = new List<A>(null,null); |
77 this.elems = List.nil(); |
77 this.last = this.elems; |
78 this.last = null; |
78 count = 0; |
79 count = 0; |
79 shared = false; |
80 shared = false; |
80 } |
81 } |
81 |
82 |
82 /** Return the number of elements in this buffer. |
83 /** Return the number of elements in this buffer. |
101 } |
102 } |
102 |
103 |
103 /** Copy list and sets last. |
104 /** Copy list and sets last. |
104 */ |
105 */ |
105 private void copy() { |
106 private void copy() { |
106 List<A> p = elems = new List<A>(elems.head, elems.tail); |
107 if (elems.nonEmpty()) { |
107 while (true) { |
108 List<A> orig = elems; |
108 List<A> tail = p.tail; |
109 |
109 if (tail == null) break; |
110 elems = last = List.<A>of(orig.head); |
110 tail = new List<A>(tail.head, tail.tail); |
111 |
111 p.setTail(tail); |
112 while ((orig = orig.tail).nonEmpty()) { |
112 p = tail; |
113 last.tail = List.<A>of(orig.head); |
113 } |
114 last = last.tail; |
114 last = p; |
115 } |
115 shared = false; |
116 } |
116 } |
117 } |
117 |
118 |
118 /** Prepend an element to buffer. |
119 /** Prepend an element to buffer. |
119 */ |
120 */ |
120 public ListBuffer<A> prepend(A x) { |
121 public ListBuffer<A> prepend(A x) { |
121 elems = elems.prepend(x); |
122 elems = elems.prepend(x); |
|
123 if (last == null) last = elems; |
122 count++; |
124 count++; |
123 return this; |
125 return this; |
124 } |
126 } |
125 |
127 |
126 /** Append an element to buffer. |
128 /** Append an element to buffer. |
127 */ |
129 */ |
128 public ListBuffer<A> append(A x) { |
130 public ListBuffer<A> append(A x) { |
129 x.getClass(); // null check |
131 x.getClass(); // null check |
130 if (shared) copy(); |
132 if (shared) copy(); |
131 last.head = x; |
133 List<A> newLast = List.<A>of(x); |
132 last.setTail(new List<A>(null,null)); |
134 if (last != null) { |
133 last = last.tail; |
135 last.tail = newLast; |
|
136 last = newLast; |
|
137 } else { |
|
138 elems = last = newLast; |
|
139 } |
134 count++; |
140 count++; |
135 return this; |
141 return this; |
136 } |
142 } |
137 |
143 |
138 /** Append all elements in a list to buffer. |
144 /** Append all elements in a list to buffer. |
203 */ |
210 */ |
204 public Iterator<A> iterator() { |
211 public Iterator<A> iterator() { |
205 return new Iterator<A>() { |
212 return new Iterator<A>() { |
206 List<A> elems = ListBuffer.this.elems; |
213 List<A> elems = ListBuffer.this.elems; |
207 public boolean hasNext() { |
214 public boolean hasNext() { |
208 return elems != last; |
215 return !elems.isEmpty(); |
209 } |
216 } |
210 public A next() { |
217 public A next() { |
211 if (elems == last) |
218 if (elems.isEmpty()) |
212 throw new NoSuchElementException(); |
219 throw new NoSuchElementException(); |
213 A elem = elems.head; |
220 A elem = elems.head; |
214 elems = elems.tail; |
221 elems = elems.tail; |
215 return elem; |
222 return elem; |
216 } |
223 } |