/** * Returns a deep copy of the receiver. * * @return a deep copy of the receiver. */ public Object clone() { // overridden for performance only. BooleanArrayList clone = new BooleanArrayList((boolean[]) elements.clone()); clone.setSizeRaw(size); return clone; } /**
/** * Returns a new list of the part of the receiver between <code>from</code>, inclusive, and <code>to</code>, inclusive. * @param from the index of the first element (inclusive). * @param to the index of the last element (inclusive). * @return a new list * @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>). */ public AbstractBooleanList partFromTo(int from, int to) { if (size==0) return new BooleanArrayList(0); checkRangeFromTo(from, to, size); boolean[] part = new boolean[to-from+1]; System.arraycopy(elements, from, part, 0, to-from+1); return new BooleanArrayList(part); } /**
/** * Returns a deep copy of the receiver; uses <code>clone()</code> and casts the result. * * @return a deep copy of the receiver. */ public BooleanArrayList copy() { return (BooleanArrayList) clone(); } /**
/** * Sorts the specified range of the receiver into ascending numerical order (<tt>false < true</tt>). * * The sorting algorithm is a count sort. This algorithm offers guaranteed * O(n) performance without auxiliary memory. * * @param from the index of the first element (inclusive) to be sorted. * @param to the index of the last element (inclusive) to be sorted. */ public void countSortFromTo(int from, int to) { if (size==0) return; checkRangeFromTo(from, to, size); boolean[] theElements = elements; int trues = 0; for (int i=from; i<=to;) if (theElements[i++]) trues++; int falses = to-from+1-trues; if (falses>0) fillFromToWith(from,from+falses-1,false); if (trues>0) fillFromToWith(from+falses,from+falses-1+trues,true); } /**
int j=0; boolean[] theElements = elements; int mySize = size(); sortedList.quickSort(); if (sortedList.binarySearchFromTo(theElements[i], 0, limit) >= 0) theElements[j++]=theElements[i]; setSize(j); return modified;
/** * Compares the specified Object with the receiver. * Returns true if and only if the specified Object is also an ArrayList of the same type, both Lists have the * same size, and all corresponding pairs of elements in the two Lists are identical. * In other words, two Lists are defined to be equal if they contain the * same elements in the same order. * * @param otherObj the Object to be compared for equality with the receiver. * @return true if the specified Object is equal to the receiver. */ public boolean equals(Object otherObj) { //delta // overridden for performance only. if (! (otherObj instanceof BooleanArrayList)) return super.equals(otherObj); if (this==otherObj) return true; if (otherObj==null) return false; BooleanArrayList other = (BooleanArrayList) otherObj; if (size()!=other.size()) return false; boolean[] theElements = elements(); boolean[] otherElements = other.elements(); for (int i=size(); --i >= 0; ) { if (theElements[i] != otherElements[i]) return false; } return true; } /**
/** * Returns a new list of the part of the receiver between <code>from</code>, inclusive, and <code>to</code>, inclusive. * @param from the index of the first element (inclusive). * @param to the index of the last element (inclusive). * @return a new list * @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>). */ public AbstractBooleanList partFromTo(int from, int to) { checkRangeFromTo(from, to, size); int length = to-from+1; BooleanArrayList part = new BooleanArrayList(length); part.addAllOfFromTo(this,from,to); return part; } /**
/** * Replaces a number of elements in the receiver with the same number of elements of another list. * Replaces elements in the receiver, between <code>from</code> (inclusive) and <code>to</code> (inclusive), * with elements of <code>other</code>, starting from <code>otherFrom</code> (inclusive). * * @param from the position of the first element to be replaced in the receiver * @param to the position of the last element to be replaced in the receiver * @param other list holding elements to be copied into the receiver. * @param otherFrom position of first element within other list to be copied. */ public void replaceFromToWithFrom(int from, int to, AbstractBooleanList other, int otherFrom) { // overridden for performance only. if (! (other instanceof BooleanArrayList)) { // slower super.replaceFromToWithFrom(from,to,other,otherFrom); return; } int length=to-from+1; if (length>0) { checkRangeFromTo(from, to, size()); checkRangeFromTo(otherFrom,otherFrom+length-1,other.size()); System.arraycopy(((BooleanArrayList) other).elements, otherFrom, elements, from, length); } } /**
/** * Sorts the specified range of the receiver into ascending order. * * The sorting algorithm is countsort. * * @param from the index of the first element (inclusive) to be sorted. * @param to the index of the last element (inclusive) to be sorted. * @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>). */ public void sortFromTo(int from, int to) { countSortFromTo(from, to); } /**
/** * Returns the index of the last occurrence of the specified * element. Returns <code>-1</code> if the receiver does not contain this element. * Searches beginning at <code>to</code>, inclusive until <code>from</code>, inclusive. * Tests for identity. * * @param element element to search for. * @param from the leftmost search position, inclusive. * @param to the rightmost search position, inclusive. * @return the index of the last occurrence of the element in the receiver; returns <code>-1</code> if the element is not found. * @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>). */ public int lastIndexOfFromTo(boolean element, int from, int to) { // overridden for performance only. if (size==0) return -1; checkRangeFromTo(from, to, size); boolean[] theElements = elements; for (int i = to ; i >= from; i--) { if (element==theElements[i]) {return i;} //found } return -1; //not found } /**
/** * Sets the receiver's elements to be the specified array. * The size and capacity of the list is the length of the array. * <b>WARNING:</b> For efficiency reasons and to keep memory usage low, this method may decide <b>not to copy the array</b>. * So if subsequently you modify the returned array directly via the [] operator, be sure you know what you're doing. * * @param elements the new elements to be stored. * @return the receiver itself. */ public AbstractBooleanList elements(boolean[] elements) { clear(); addAllOfFromTo(new BooleanArrayList(elements),0,elements.length-1); return this; } /**
/** * Appends the specified element to the end of this list. * * @param element element to be appended to this list. */ public void add(boolean element) { // overridden for performance only. if (size == elements.length) { ensureCapacity(size + 1); } elements[size++] = element; } /**
/** * Constructs a list containing the specified elements. * The initial size and capacity of the list is the length of the array. * * <b>WARNING:</b> For efficiency reasons and to keep memory usage low, <b>the array is not copied</b>. * So if subsequently you modify the specified array directly via the [] operator, be sure you know what you're doing. * * @param elements the array to be backed by the the constructed list */ public BooleanArrayList(boolean[] elements) { elements(elements); } /**
int j=0; boolean[] theElements = elements; int mySize = size(); sortedList.quickSort(); if (sortedList.binarySearchFromTo(theElements[i], 0, limit) >= 0) theElements[j++]=theElements[i]; setSize(j); return modified;
/** * Compares the specified Object with the receiver. * Returns true if and only if the specified Object is also an ArrayList of the same type, both Lists have the * same size, and all corresponding pairs of elements in the two Lists are identical. * In other words, two Lists are defined to be equal if they contain the * same elements in the same order. * * @param otherObj the Object to be compared for equality with the receiver. * @return true if the specified Object is equal to the receiver. */ public boolean equals(Object otherObj) { //delta // overridden for performance only. if (! (otherObj instanceof BooleanArrayList)) return super.equals(otherObj); if (this==otherObj) return true; if (otherObj==null) return false; BooleanArrayList other = (BooleanArrayList) otherObj; if (size()!=other.size()) return false; boolean[] theElements = elements(); boolean[] otherElements = other.elements(); for (int i=size(); --i >= 0; ) { if (theElements[i] != otherElements[i]) return false; } return true; } /**
/** * Returns a new list of the part of the receiver between <code>from</code>, inclusive, and <code>to</code>, inclusive. * @param from the index of the first element (inclusive). * @param to the index of the last element (inclusive). * @return a new list * @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>). */ public AbstractBooleanList partFromTo(int from, int to) { checkRangeFromTo(from, to, size); int length = to-from+1; BooleanArrayList part = new BooleanArrayList(length); part.addAllOfFromTo(this,from,to); return part; } /**
/** * Sorts the specified range of the receiver into ascending numerical order (<tt>false < true</tt>). * * The sorting algorithm is a count sort. This algorithm offers guaranteed * O(n) performance without auxiliary memory. * * @param from the index of the first element (inclusive) to be sorted. * @param to the index of the last element (inclusive) to be sorted. */ public void countSortFromTo(int from, int to) { if (size==0) return; checkRangeFromTo(from, to, size); boolean[] theElements = elements; int trues = 0; for (int i=from; i<=to;) if (theElements[i++]) trues++; int falses = to-from+1-trues; if (falses>0) fillFromToWith(from,from+falses-1,false); if (trues>0) fillFromToWith(from+falses,from+falses-1+trues,true); } /**
/** * Replaces a number of elements in the receiver with the same number of elements of another list. * Replaces elements in the receiver, between <code>from</code> (inclusive) and <code>to</code> (inclusive), * with elements of <code>other</code>, starting from <code>otherFrom</code> (inclusive). * * @param from the position of the first element to be replaced in the receiver * @param to the position of the last element to be replaced in the receiver * @param other list holding elements to be copied into the receiver. * @param otherFrom position of first element within other list to be copied. */ public void replaceFromToWithFrom(int from, int to, AbstractBooleanList other, int otherFrom) { // overridden for performance only. if (! (other instanceof BooleanArrayList)) { // slower super.replaceFromToWithFrom(from,to,other,otherFrom); return; } int length=to-from+1; if (length>0) { checkRangeFromTo(from, to, size()); checkRangeFromTo(otherFrom,otherFrom+length-1,other.size()); System.arraycopy(((BooleanArrayList) other).elements, otherFrom, elements, from, length); } } /**
/** * Sorts the specified range of the receiver into ascending order (<tt>false < true</tt>). * * The sorting algorithm is <b>not</b> a quicksort, but rather a countsort. * This algorithm offers guaranteed O(n) performance. * * @param from the index of the first element (inclusive) to be sorted. * @param to the index of the last element (inclusive) to be sorted. * @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>). */ public void quickSortFromTo(int from, int to) { countSortFromTo(from, to); } /**
/** * Returns the index of the first occurrence of the specified * element. Returns <code>-1</code> if the receiver does not contain this element. * Searches between <code>from</code>, inclusive and <code>to</code>, inclusive. * Tests for identity. * * @param element element to search for. * @param from the leftmost search position, inclusive. * @param to the rightmost search position, inclusive. * @return the index of the first occurrence of the element in the receiver; returns <code>-1</code> if the element is not found. * @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>). */ public int indexOfFromTo(boolean element, int from, int to) { // overridden for performance only. if (size==0) return -1; checkRangeFromTo(from, to, size); boolean[] theElements = elements; for (int i = from ; i <= to; i++) { if (element==theElements[i]) {return i;} //found } return -1; //not found } /**