1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: UnionIterator.java,v 1.18 2004/02/16 22:54:59 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.dom;
  20. import com.sun.org.apache.xalan.internal.xsltc.DOM;
  21. import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
  22. import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
  23. import com.sun.org.apache.xml.internal.dtm.ref.DTMAxisIteratorBase;
  24. /**
  25. * UnionIterator takes a set of NodeIterators and produces
  26. * a merged NodeSet in document order with duplicates removed
  27. * The individual iterators are supposed to generate nodes
  28. * in document order
  29. * @author Jacek Ambroziak
  30. * @author Santiago Pericas-Geertsen
  31. */
  32. public final class UnionIterator extends DTMAxisIteratorBase {
  33. /** wrapper for NodeIterators to support iterator
  34. comparison on the value of their next() method
  35. */
  36. final private DOM _dom;
  37. private final static class LookAheadIterator {
  38. public int node, markedNode;
  39. public DTMAxisIterator iterator;
  40. public boolean isStartSet = false;
  41. public LookAheadIterator(DTMAxisIterator iterator) {
  42. this.iterator = iterator;
  43. }
  44. public int step() {
  45. node = iterator.next();
  46. return node;
  47. }
  48. public LookAheadIterator cloneIterator() {
  49. final LookAheadIterator clone =
  50. new LookAheadIterator(iterator.cloneIterator());
  51. clone.node = node;
  52. clone.markedNode = node;
  53. return clone;
  54. }
  55. public void setMark() {
  56. markedNode = node;
  57. iterator.setMark();
  58. }
  59. public void gotoMark() {
  60. node = markedNode;
  61. iterator.gotoMark();
  62. }
  63. } // end of LookAheadIterator
  64. private static final int InitSize = 8;
  65. private int _heapSize = 0;
  66. private int _size = InitSize;
  67. private LookAheadIterator[] _heap = new LookAheadIterator[InitSize];
  68. private int _free = 0;
  69. // last node returned by this UnionIterator to the caller of next
  70. // used to prune duplicates
  71. private int _returnedLast;
  72. // cached returned last for use in gotoMark
  73. private int _cachedReturnedLast = END;
  74. // cached heap size for use in gotoMark
  75. private int _cachedHeapSize;
  76. public UnionIterator(DOM dom) {
  77. _dom = dom;
  78. }
  79. public DTMAxisIterator cloneIterator() {
  80. _isRestartable = false;
  81. final LookAheadIterator[] heapCopy =
  82. new LookAheadIterator[_heap.length];
  83. try {
  84. final UnionIterator clone = (UnionIterator)super.clone();
  85. for (int i = 0; i < _free; i++) {
  86. heapCopy[i] = _heap[i].cloneIterator();
  87. }
  88. clone.setRestartable(false);
  89. clone._heap = heapCopy;
  90. return clone.reset();
  91. }
  92. catch (CloneNotSupportedException e) {
  93. BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR,
  94. e.toString());
  95. return null;
  96. }
  97. }
  98. public UnionIterator addIterator(DTMAxisIterator iterator) {
  99. if (_free == _size) {
  100. LookAheadIterator[] newArray = new LookAheadIterator[_size *= 2];
  101. System.arraycopy(_heap, 0, newArray, 0, _free);
  102. _heap = newArray;
  103. }
  104. _heapSize++;
  105. _heap[_free++] = new LookAheadIterator(iterator);
  106. return this;
  107. }
  108. public int next() {
  109. while (_heapSize > 0) {
  110. final int smallest = _heap[0].node;
  111. if (smallest == END) { // iterator _heap[0] is done
  112. if (_heapSize > 1) {
  113. // Swap first and last (iterator must be restartable)
  114. final LookAheadIterator temp = _heap[0];
  115. _heap[0] = _heap[--_heapSize];
  116. _heap[_heapSize] = temp;
  117. }
  118. else {
  119. return END;
  120. }
  121. }
  122. else if (smallest == _returnedLast) { // duplicate
  123. _heap[0].step(); // value consumed
  124. }
  125. else {
  126. _heap[0].step(); // value consumed
  127. heapify(0);
  128. return returnNode(_returnedLast = smallest);
  129. }
  130. // fallthrough if not returned above
  131. heapify(0);
  132. }
  133. return END;
  134. }
  135. public DTMAxisIterator setStartNode(int node) {
  136. if (_isRestartable) {
  137. _startNode = node;
  138. for (int i = 0; i < _free; i++) {
  139. if(!_heap[i].isStartSet){
  140. _heap[i].iterator.setStartNode(node);
  141. _heap[i].step(); // to get the first node
  142. _heap[i].isStartSet = true;
  143. }
  144. }
  145. // build heap
  146. for (int i = (_heapSize = _free)/2; i >= 0; i--) {
  147. heapify(i);
  148. }
  149. _returnedLast = END;
  150. return resetPosition();
  151. }
  152. return this;
  153. }
  154. /* Build a heap in document order. put the smallest node on the top.
  155. * "smallest node" means the node before other nodes in document order
  156. */
  157. private void heapify(int i) {
  158. for (int r, l, smallest;;) {
  159. r = (i + 1) << 1; l = r - 1;
  160. smallest = l < _heapSize
  161. && _dom.lessThan(_heap[l].node, _heap[i].node) ? l : i;
  162. if (r < _heapSize && _dom.lessThan(_heap[r].node,
  163. _heap[smallest].node)) {
  164. smallest = r;
  165. }
  166. if (smallest != i) {
  167. final LookAheadIterator temp = _heap[smallest];
  168. _heap[smallest] = _heap[i];
  169. _heap[i] = temp;
  170. i = smallest;
  171. }
  172. else
  173. break;
  174. }
  175. }
  176. public void setMark() {
  177. for (int i = 0; i < _free; i++) {
  178. _heap[i].setMark();
  179. }
  180. _cachedReturnedLast = _returnedLast;
  181. _cachedHeapSize = _heapSize;
  182. }
  183. public void gotoMark() {
  184. for (int i = 0; i < _free; i++) {
  185. _heap[i].gotoMark();
  186. }
  187. // rebuild heap after call last() function. fix for bug 20913
  188. for (int i = (_heapSize = _cachedHeapSize)/2; i >= 0; i--) {
  189. heapify(i);
  190. }
  191. _returnedLast = _cachedReturnedLast;
  192. }
  193. public DTMAxisIterator reset() {
  194. for (int i = 0; i < _free; i++) {
  195. _heap[i].iterator.reset();
  196. _heap[i].step();
  197. }
  198. // build heap
  199. for (int i = (_heapSize = _free)/2; i >= 0; i--) {
  200. heapify(i);
  201. }
  202. _returnedLast = END;
  203. return resetPosition();
  204. }
  205. }