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: SortingIterator.java,v 1.6 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.runtime.BasisLibrary;
  21. import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
  22. import com.sun.org.apache.xml.internal.dtm.ref.DTMAxisIteratorBase;
  23. /**
  24. * @author Jacek Ambroziak
  25. * @author Santiago Pericas-Geertsen
  26. * @author Morten Jorgensen
  27. */
  28. public final class SortingIterator extends DTMAxisIteratorBase {
  29. private final static int INIT_DATA_SIZE = 16;
  30. private DTMAxisIterator _source;
  31. private NodeSortRecordFactory _factory;
  32. private NodeSortRecord[] _data;
  33. private int _free = 0;
  34. private int _current; // index in _nodes of the next node to try
  35. public SortingIterator(DTMAxisIterator source,
  36. NodeSortRecordFactory factory) {
  37. _source = source;
  38. _factory = factory;
  39. }
  40. public int next() {
  41. return _current < _free ? _data[_current++].getNode() : END;
  42. }
  43. public DTMAxisIterator setStartNode(int node) {
  44. try {
  45. _source.setStartNode(_startNode = node);
  46. _data = new NodeSortRecord[INIT_DATA_SIZE];
  47. _free = 0;
  48. // gather all nodes from the source iterator
  49. while ((node = _source.next()) != END) {
  50. addRecord(_factory.makeNodeSortRecord(node,_free));
  51. }
  52. // now sort the records
  53. quicksort(0, _free - 1);
  54. _current = 0;
  55. return this;
  56. }
  57. catch (Exception e) {
  58. return this;
  59. }
  60. }
  61. public int getPosition() {
  62. return _current == 0 ? 1 : _current;
  63. }
  64. public int getLast() {
  65. return _free;
  66. }
  67. public void setMark() {
  68. _source.setMark();
  69. _markedNode = _current;
  70. }
  71. public void gotoMark() {
  72. _source.gotoMark();
  73. _current = _markedNode;
  74. }
  75. /**
  76. * Clone a <code>SortingIterator</code> by cloning its source
  77. * iterator and then sharing the factory and the array of
  78. * <code>NodeSortRecords</code>.
  79. */
  80. public DTMAxisIterator cloneIterator() {
  81. try {
  82. final SortingIterator clone = (SortingIterator) super.clone();
  83. clone._source = _source.cloneIterator();
  84. clone._factory = _factory; // shared between clones
  85. clone._data = _data; // shared between clones
  86. clone._free = _free;
  87. clone._current = _current;
  88. clone.setRestartable(false);
  89. return clone.reset();
  90. }
  91. catch (CloneNotSupportedException e) {
  92. BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR,
  93. e.toString());
  94. return null;
  95. }
  96. }
  97. private void addRecord(NodeSortRecord record) {
  98. if (_free == _data.length) {
  99. NodeSortRecord[] newArray = new NodeSortRecord[_data.length * 2];
  100. System.arraycopy(_data, 0, newArray, 0, _free);
  101. _data = newArray;
  102. }
  103. _data[_free++] = record;
  104. }
  105. private void quicksort(int p, int r) {
  106. while (p < r) {
  107. final int q = partition(p, r);
  108. quicksort(p, q);
  109. p = q + 1;
  110. }
  111. }
  112. private int partition(int p, int r) {
  113. final NodeSortRecord x = _data[(p + r) >>> 1];
  114. int i = p - 1;
  115. int j = r + 1;
  116. while (true) {
  117. while (x.compareTo(_data[--j]) < 0);
  118. while (x.compareTo(_data[++i]) > 0);
  119. if (i < j) {
  120. final NodeSortRecord t = _data[i];
  121. _data[i] = _data[j];
  122. _data[j] = t;
  123. }
  124. else {
  125. return(j);
  126. }
  127. }
  128. }
  129. }