00001 package ca.mcgill.sable.util; 00002 00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00004 * SableUtil, a clean room implementation of the Collection API. * 00005 * Copyright (C) 1997, 1998 Raja Vallee-Rai (kor@sable.mcgill.ca). * 00006 * All rights reserved. * 00007 * * 00008 * Modifications by Patrick Lam (plam@sable.mcgill.ca) are * 00009 * Copyright (C) 1998 Patrick Lam (plam@sable.mcgill.ca). All * 00010 * rights reserved. * 00011 * * 00012 * This work was done as a project of the Sable Research Group, * 00013 * School of Computer Science, McGill University, Canada * 00014 * (http://www.sable.mcgill.ca/). It is understood that any * 00015 * modification not identified as such is not covered by the * 00016 * preceding statement. * 00017 * * 00018 * This work is free software; you can redistribute it and/or * 00019 * modify it under the terms of the GNU Library General Public * 00020 * License as published by the Free Software Foundation; either * 00021 * version 2 of the License, or (at your option) any later version. * 00022 * * 00023 * This work is distributed in the hope that it will be useful, * 00024 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00025 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00026 * Library General Public License for more details. * 00027 * * 00028 * You should have received a copy of the GNU Library General Public * 00029 * License along with this library; if not, write to the * 00030 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * 00031 * Boston, MA 02111-1307, USA. * 00032 * * 00033 * To submit a bug report, send a comment, or get the latest news on * 00034 * this project and other Sable Research Group projects, please * 00035 * visit the web site: http://www.sable.mcgill.ca/ * 00036 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00037 00038 /* 00039 Reference Version 00040 ----------------- 00041 This is the latest official version on which this file is based. 00042 The reference version is: $SableUtilVersion: 1.11 $ 00043 00044 Change History 00045 -------------- 00046 A) Notes: 00047 00048 Please use the following template. Most recent changes should 00049 appear at the top of the list. 00050 00051 - Modified on [date (March 1, 1900)] by [name]. [(*) if appropriate] 00052 [description of modification]. 00053 00054 Any Modification flagged with "(*)" was done as a project of the 00055 Sable Research Group, School of Computer Science, 00056 McGill University, Canada (http://www.sable.mcgill.ca/). 00057 00058 You should add your copyright, using the following template, at 00059 the top of this file, along with other copyrights. 00060 00061 * * 00062 * Modifications by [name] are * 00063 * Copyright (C) [year(s)] [your name (or company)]. All rights * 00064 * reserved. * 00065 * * 00066 00067 B) Changes: 00068 00069 - Modified on July 22, 1998 by Raja Vallee-Rai (kor@sable.mcgill.ca). (*) 00070 Changed some constructors from private to public to satisfy jikes. 00071 00072 - Modified on June 19, 1998 by Patrick Lam (plam@sable.mcgill.ca). (*) 00073 Merged in Arrays.sort implementation. 00074 00075 - Modified on June 15, 1998 by Raja Vallee-Rai (kor@sable.mcgill.ca). (*) 00076 First release of this file. 00077 00078 */ 00079 00080 public class Arrays 00081 { 00082 private static class ArrayList extends AbstractList 00083 { 00084 private Object[] array; 00085 00086 public ArrayList(Object[] array) 00087 { 00088 this.array = array; 00089 } 00090 00091 public Object get(int index) 00092 { 00093 return array[index]; 00094 } 00095 00096 public Object set(int index, Object element) 00097 { 00098 Object oldElement = array[index]; 00099 array[index] = element; 00100 00101 return oldElement; 00102 } 00103 00104 public int size() 00105 { 00106 return array.length; 00107 } 00108 00109 public Object clone() 00110 { 00111 return array.clone(); 00112 } 00113 00114 } 00115 00116 00117 private static void _sort_helper(Object[] toSort, 00118 int low, int high, Comparator c) 00119 throws ClassCastException 00120 { 00121 if (low < high) 00122 { 00123 int q = partition(toSort, low, high, c); 00124 _sort_helper(toSort, low, q, c); 00125 _sort_helper(toSort, q+1, high, c); 00126 } 00127 } 00128 private static int partition(Object[] toSort, 00129 int low, int high, Comparator c) 00130 throws ClassCastException 00131 { 00132 Object x = toSort[low]; 00133 int i = low - 1; int j = high + 1; 00134 while(true) 00135 { 00136 do { j--; } while (c.compare(toSort[j], x) > 0); 00137 do { i++; } while (c.compare(toSort[i], x) < 0); 00138 if (i < j) 00139 { 00140 x = toSort[i]; toSort[i] = toSort[j]; toSort[j] = x; 00141 } 00142 return j; 00143 } 00144 } 00145 public static void sort(Object[] toSort, Comparator c) 00146 throws ClassCastException 00147 { 00148 _sort_helper(toSort, 0, toSort.length-1, c); 00149 } 00150 public static List toList(Object[] array) 00151 { 00152 return new ArrayList(array); 00153 } 00154 }