/* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for Additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ namespace HH.WMS.Utils.NPOI.Util { using System; /// /// A List of int's; as full an implementation of the java.Util.List interface as possible, with an eye toward minimal creation of objects /// /// the mimicry of List is as follows: /// /// /// the mimicry is not perfect, however: /// /// @author Marc Johnson /// public class IntList { private int[] _array; private int _limit; private int fillval = 0; private static int _default_size = 128; /// /// create an IntList of default size /// public IntList() : this(_default_size) { } public IntList(int InitialCapacity) : this(InitialCapacity, 0) { } /// /// create a copy of an existing IntList /// /// the existing IntList public IntList(IntList list) : this(list._array.Length) { Array.Copy(list._array, 0, _array, 0, _array.Length); _limit = list._limit; } /// /// create an IntList with a predefined Initial size /// /// the size for the internal array /// public IntList(int initialCapacity, int fillvalue) { _array = new int[initialCapacity]; if (fillval != 0) { fillval = fillvalue; FillArray(fillval, _array, 0); } _limit = 0; } private void FillArray(int val, int[] array, int index) { for (int k = index; k < array.Length; k++) { array[k] = val; } } /// /// add the specfied value at the specified index /// /// the index where the new value is to be Added /// the new value public void Add(int index, int value) { if (index > _limit) { throw new IndexOutOfRangeException(); } else if (index == _limit) { Add(value); } else { // index < limit -- insert into the middle if (_limit == _array.Length) { growArray(_limit * 2); } Array.Copy(_array, index, _array, index + 1, _limit - index); _array[index] = value; _limit++; } } /// /// Appends the specified element to the end of this list /// /// element to be Appended to this list. /// return true (as per the general contract of the Collection.add method public bool Add(int value) { if (_limit == _array.Length) { growArray(_limit * 2); } _array[_limit++] = value; return true; } /// /// Appends all of the elements in the specified collection to the /// end of this list, in the order that they are returned by the /// specified collection's iterator. The behavior of this /// operation is unspecified if the specified collection is /// modified while the operation is in progress. (Note that this /// will occur if the specified collection is this list, and it's /// nonempty.) /// /// collection whose elements are to be Added to this list. /// return true if this list Changed as a result of the call. public bool AddAll(IntList c) { if (c._limit != 0) { if ((_limit + c._limit) > _array.Length) { growArray(_limit + c._limit); } Array.Copy(c._array, 0, _array, _limit, c._limit); _limit += c._limit; } return true; } /// /// Inserts all of the elements in the specified collection into /// this list at the specified position. Shifts the element /// currently at that position (if any) and any subsequent elements /// to the right (increases their indices). The new elements will /// appear in this list in the order that they are returned by the /// specified collection's iterator. The behavior of this /// operation is unspecified if the specified collection is /// modified while the operation is in progress. (Note that this /// will occur if the specified collection is this list, and it's /// nonempty.) /// /// index at which to insert first element from the specified collection. /// elements to be inserted into this list. /// return true if this list Changed as a result of the call. public bool AddAll(int index, IntList c) { if (index > _limit) { throw new IndexOutOfRangeException(); } if (c._limit != 0) { if ((_limit + c._limit) > _array.Length) { growArray(_limit + c._limit); } // make a hole Array.Copy(_array, index, _array, index + c._limit, _limit - index); // fill it in Array.Copy(c._array, 0, _array, index, c._limit); _limit += c._limit; } return true; } /// /// Removes all of the elements from this list. This list will be /// empty After this call returns (unless it throws an exception). /// public void Clear() { _limit = 0; } /// /// Returns true if this list Contains the specified element. More /// formally, returns true if and only if this list Contains at /// least one element e such that o == e /// /// element whose presence in this list is to be Tested. /// return true if this list Contains the specified element. public bool Contains(int o) { bool rval = false; for (int j = 0; !rval && (j < _limit); j++) { if (_array[j] == o) { rval = true; } } return rval; } /// /// Returns true if this list Contains all of the elements of the /// specified collection. /// /// collection to be Checked for Containment in this list. /// return true if this list Contains all of the elements of the specified collection. public bool ContainsAll(IntList c) { bool rval = true; if (this != c) { for (int j = 0; rval && (j < c._limit); j++) { if (!Contains(c._array[j])) { rval = false; } } } return rval; } /// /// Compares the specified object with this list for Equality. /// Returns true if and only if the specified object is also a /// list, both lists have the same size, and all corresponding /// pairs of elements in the two lists are Equal. (Two elements e1 /// and e2 are equal if e1 == e2.) In other words, two lists are /// defined to be equal if they contain the same elements in the /// same order. This defInition ensures that the Equals method /// works properly across different implementations of the List /// interface. /// /// the object to be Compared for Equality with this list. /// return true if the specified object is equal to this list. public override bool Equals(Object o) { bool rval = this == o; if (!rval && (o != null) && (o.GetType() == this.GetType())) { IntList other = (IntList)o; if (other._limit == _limit) { // assume match rval = true; for (int j = 0; rval && (j < _limit); j++) { rval = _array[j] == other._array[j]; } } } return rval; } /// /// Returns the element at the specified position in this list. /// /// index of element to return. /// return the element at the specified position in this list. public int Get(int index) { if (index >= _limit) { throw new IndexOutOfRangeException( index + " not accessible in a list of length " + _limit ); } return _array[index]; } /// /// Returns the hash code value for this list. The hash code of a /// list is defined to be the result of the following calculation: /// /// /// hashCode = 1; /// Iterator i = list.Iterator(); /// while (i.HasNext()) { /// Object obj = i.Next(); /// hashCode = 31*hashCode + (obj==null ? 0 : obj.HashCode()); /// } /// /// /// This ensures that list1.Equals(list2) implies that /// list1.HashCode()==list2.HashCode() for any two lists, list1 and /// list2, as required by the general contract of Object.HashCode. /// /// /// return the hash code value for this list. public override int GetHashCode() { int hash = 0; for (int j = 0; j < _limit; j++) { hash = (31 * hash) + _array[j]; } return hash; } /// /// Returns the index in this list of the first occurrence of the /// specified element, or -1 if this list does not contain this /// element. More formally, returns the lowest index i such that /// (o == Get(i)), or -1 if there is no such index. /// /// element to search for. /// return the index in this list of the first occurrence of the /// specified element, or -1 if this list does not contain /// this element. public int IndexOf(int o) { int rval = 0; for (; rval < _limit; rval++) { if (o == _array[rval]) { break; } } if (rval == _limit) { rval = -1; // didn't find it } return rval; } /// /// Returns true if this list Contains no elements. /// /// return true if this list Contains no elements. public bool IsEmpty() { return _limit == 0; } /// /// Returns the index in this list of the last occurrence of the /// specified element, or -1 if this list does not contain this /// element. More formally, returns the highest index i such that /// (o == Get(i)), or -1 if there is no such index. /// /// element to search for. /// the index in this list of the last occurrence of the /// specified element, or -1 if this list does not contain /// this element. /// public int LastIndexOf(int o) { int rval = _limit - 1; for (; rval >= 0; rval--) { if (o == _array[rval]) { break; } } return rval; } /// /// Removes the element at the specified position in this list. /// Shifts any subsequent elements to the left (subtracts one from /// their indices). Returns the element that was Removed from the /// list. /// /// the index of the element to Removed. /// return the element previously at the specified position. public int Remove(int index) { if (index >= _limit) { throw new IndexOutOfRangeException(); } int rval = _array[index]; Array.Copy(_array, index + 1, _array, index, _limit - index); _limit--; return rval; } /// /// Removes the first occurrence in this list of the specified /// element (optional operation). If this list does not contain /// the element, it is unChanged. More formally, Removes the /// element with the lowest index i such that (o.Equals(get(i))) /// (if such an element exists). /// /// element to be Removed from this list, if present. /// return true if this list Contained the specified element. public bool RemoveValue(int o) { bool rval = false; for (int j = 0; !rval && (j < _limit); j++) { if (o == _array[j]) { if (j + 1 < _limit) { Array.Copy(_array, j + 1, _array, j, _limit - j); } _limit--; rval = true; } } return rval; } /// /// Removes from this list all the elements that are Contained in /// the specified collection /// /// collection that defines which elements will be Removed from the list. /// return true if this list Changed as a result of the call. public bool RemoveAll(IntList c) { bool rval = false; for (int j = 0; j < c._limit; j++) { if (RemoveValue(c._array[j])) { rval = true; } } return rval; } /// /// Retains only the elements in this list that are Contained in /// the specified collection. In other words, Removes from this /// list all the elements that are not Contained in the specified /// collection. /// /// collection that defines which elements this Set will retain. /// return true if this list Changed as a result of the call. public bool RetainAll(IntList c) { bool rval = false; for (int j = 0; j < _limit; ) { if (!c.Contains(_array[j])) { Remove(j); rval = true; } else { j++; } } return rval; } /// /// Replaces the element at the specified position in this list with the specified element /// /// index of element to Replace. /// element to be stored at the specified position. /// the element previously at the specified position. public int Set(int index, int element) { if (index >= _limit) { throw new IndexOutOfRangeException(); } int rval = _array[index]; _array[index] = element; return rval; } /// /// Returns the number of elements in this list. If this list /// Contains more than Int32.MaxValue elements, returns /// Int32.MaxValue. /// /// the number of elements in this IntList public int Size() { return _limit; } /// /// the number of elements in this IntList /// public int Count { get { return _limit; } } /// /// Returns an array Containing all of the elements in this list in /// proper sequence. Obeys the general contract of the /// Collection.ToArray method. /// /// an array Containing all of the elements in this list in proper sequence. public int[] ToArray() { int[] rval = new int[_limit]; Array.Copy(_array, 0, rval, 0, _limit); return rval; } /// /// Returns an array Containing all of the elements in this list in /// proper sequence. Obeys the general contract of the /// Collection.ToArray(Object[]) method. /// /// the array into which the elements of this list are to /// be stored, if it is big enough; otherwise, a new array /// is allocated for this purpose. /// return an array Containing the elements of this list. public int[] ToArray(int[] a) { int[] rval; if (a.Length == _limit) { Array.Copy(_array, 0, a, 0, _limit); rval = a; } else { rval = ToArray(); } return rval; } private void growArray(int new_size) { int size = (new_size == _array.Length) ? new_size + 1 : new_size; int[] new_array = new int[size]; if (fillval != 0) { FillArray(fillval, new_array, _array.Length); } Array.Copy(_array, 0, new_array, 0, _limit); _array = new_array; } } // end public class IntList }