internal override IntArrayList GetMatchingResources(out bool sortedById) { sortedById = false; IntArrayList result = null; for (int i = 0; i < _resTypeIds.Length; i++) { ResourceListPredicate basePredicate = new ResourceTypePredicate(_resTypeIds [i]); ResourceListPredicate cachedPredicate = MyPalStorage.Storage.GetCachedPredicate(basePredicate); bool tempSortedById = false; object syncObject = null; IntArrayList tempResult; if (cachedPredicate != null) { tempResult = cachedPredicate.GetSortedMatchingResourcesRef(out syncObject); } else { tempResult = basePredicate.GetMatchingResources(out tempSortedById); syncObject = null; } if (result == null) { if (syncObject == null) { result = tempResult; } else { lock ( syncObject ) { result = (IntArrayList)tempResult.Clone(); } } } else { if (syncObject == null) { result.AddRange(tempResult); } else { lock ( syncObject ) { result.AddRange(tempResult); } } } } if (result == null) { result = new IntArrayList(); } return(result); }
[Test] public void SameValuesInIntersectSortedInplace() { IntArrayList list1 = new IntArrayList(); list1.AddRange(new int[] { 0, 1, 2, 3, 3 }); IntArrayList list2 = new IntArrayList(); list2.AddRange(new int[] { 0, 1, 2, 3 }); list1.IntersectSorted(list2); Assert.AreEqual(4, list1.Count); AssertEquals(0, list1[0]); AssertEquals(1, list1[1]); AssertEquals(2, list1[2]); AssertEquals(3, list1[3]); list1.Clear(); list2.Clear(); list1.AddRange(new int[] { 3, 3 }); list2.AddRange(new int[] { 0, 1, 2, 3 }); list1.IntersectSorted(list2); Assert.AreEqual(1, list1.Count); AssertEquals(3, list1[0]); list1.Clear(); list2.Clear(); list2.AddRange(new int[] { 3, 3 }); list1.AddRange(new int[] { 0, 1, 2, 3 }); list1.IntersectSorted(list2); Assert.AreEqual(1, list1.Count); AssertEquals(3, list1[0]); list1.Clear(); list2.Clear(); list2.AddRange(new int[] { 0, 0, 3, 3 }); list1.AddRange(new int[] { 0, 1, 2 }); list1.IntersectSorted(list2); Assert.AreEqual(1, list1.Count); AssertEquals(0, list1[0]); list1.Clear(); list2.Clear(); list2.AddRange(new int[] { 0, 0, 3, 3, 7, 8, 9, 12, 12, 13 }); list1.AddRange(new int[] { 0, 1, 2, 3, 4, 5, 8, 8, 12 }); list1.IntersectSorted(list2); list1 = (IntArrayList)list1.Clone(); Assert.AreEqual(4, list1.Count); AssertEquals(0, list1[0]); AssertEquals(3, list1[1]); AssertEquals(8, list1[2]); AssertEquals(12, list1[3]); }
[Test] public void SortTests() { IntArrayList list = new IntArrayList(); int[] unsorted = new int[] { 5, 1, 2, 1, 3, 4, 7, 0, -1, 5, 9, 3, 5, 4 }; list.AddRange(unsorted); list.Sort(); int[] array = list.ToArray(); Array.Sort(unsorted); for (int i = 0; i < unsorted.Length; ++i) { AssertEquals(unsorted[i], array[i]); } }
public IResourceList ExpandSelectedResources(IResourceList selection) { IntArrayList resourceIds = new IntArrayList(); resourceIds.AddRange(selection.ResourceIds); for (int i = 0; i < selection.Count; i++) { ConversationNode node = FindConversationNode(selection.ResourceIds [i]); if (node != null && node.LvNode != null && node.LvNode.CollapseState == CollapseState.Collapsed) { CollectResourcesRecursive(node, resourceIds); } } return(Core.ResourceStore.ListFromIds(resourceIds, false)); }
[Test] public void BinarySearchTests() { IntArrayList list = new IntArrayList(); int[] sorted = new int[] { 0, 0, 1, 2, 3, 4, 6, 7, 8, 8, 8, 8, 8, 8, 9, 10 }; list.AddRange(sorted); AssertEquals(Array.BinarySearch(sorted, 5), list.BinarySearch(5)); AssertEquals(Array.BinarySearch(sorted, 11), list.BinarySearch(11)); AssertEquals(Array.BinarySearch(sorted, 8), list.BinarySearch(8)); AssertEquals(Array.BinarySearch(sorted, 0), list.BinarySearch(0)); AssertEquals(Array.BinarySearch(sorted, 10), list.BinarySearch(10)); AssertEquals(Array.BinarySearch(sorted, -1), list.BinarySearch(-1)); AssertEquals(Array.BinarySearch(sorted, 100), list.BinarySearch(100)); list.Clear(); AssertEquals(Array.BinarySearch(new int[] {}, 5), list.BinarySearch(5)); }
[Test] public void SameValuesInIntersectSorted() { IntArrayList list1 = new IntArrayList(); list1.AddRange(new int[] { 0, 1, 2, 3, 3 }); IntArrayList list2 = new IntArrayList(); list2.AddRange(new int[] { 0, 1, 2, 3 }); IntArrayList intersected = IntArrayList.IntersectSorted(list1, list2); Assert.AreEqual(4, intersected.Count); AssertEquals(0, intersected [0]); AssertEquals(1, intersected [1]); AssertEquals(2, intersected [2]); AssertEquals(3, intersected [3]); }
[Test] public void MergeSorted() { IntArrayList list1 = new IntArrayList(); list1.AddRange(new int[] { 2, 3, 5, 8 }); IntArrayList list2 = new IntArrayList(); list2.AddRange(new int[] { 2, 4, 5, 7 }); IntArrayList merged = IntArrayList.MergeSorted(list1, list2); AssertEquals(6, merged.Count); AssertEquals(2, merged [0]); AssertEquals(3, merged [1]); AssertEquals(4, merged [2]); AssertEquals(5, merged [3]); AssertEquals(7, merged [4]); AssertEquals(8, merged [5]); }
public void TestIntArrayListMinusSorted() { IntArrayList array = new IntArrayList(); array.AddRange(new int[] { 0, 1, 2, 3, 4, 5, 5, 5, 6, 7, 8, 9 }); IntArrayList minus = new IntArrayList(); minus.Add(0); minus.Add(1); array.MinusSorted(minus); Assert.AreEqual(10, array.Count); minus.Add(6); minus.Add(7); minus.Add(8); minus.Add(9); array.MinusSorted(minus); Assert.AreEqual(6, array.Count); minus.Clear(); minus.Add(5); array.MinusSorted(minus); Assert.AreEqual(3, array.Count); array.MinusSorted(array); Assert.AreEqual(0, array.Count); }
public void AddRange(IntArrayList list) { _resources.AddRange(list); }
/// <summary> /// Inserts a set of the resources into the user sort order sequence. /// </summary> /// <param name="idPoint">ID of the resource that marks out the insertion point. <c>0</c> to insert without the insertion point (beginning/end of the list).</param> /// <param name="ids">A list of IDSs of the resources that should be inserted, in the <b>direct</b> order.</param> /// <param name="bInsertAfter"><c>False</c> to insert before the marker resource <paramref name="idPoint"/>, and <c>True</c> to insert after it. If the <paramref name="idPoint"/> is <c>0</c>, the new resources go to the beginning/end of the list, respectively.</param> /// <param name="existingids">An optional list of IDs of the existing resources we're inserting between, in the <b>direct</b> order; useful in case there's no user-order list yet.</param> public void Insert(int idPoint, ICollection ids, bool bInsertAfter, ICollection existingids) { if (ids == null) { throw new ArgumentNullException(); } // Create a hash to check quickly whether the particular resource is being inserted and has to be removed from the old list IntHashSet hashNewcomers = new IntHashSet(ids.Count); foreach (int id in ids) { hashNewcomers.Add(id); } IntArrayList arNewcomers = new IntArrayList(ids); // A reverse list of the newly-coming resources arNewcomers.Reverse(); // Get the list of the resources as they were ordered previously (reversed) IntArrayList arOld = ReadOrder(); IntHashSet hashOld = new IntHashSet(arOld.Count); foreach (int id in arOld) { hashOld.Add(id); } // Special case: there's no list yet, so it should be created using the resources we have in the current sort order (if provided) if (existingids != null) { if (arOld.Count == 0) { arOld.AddRange(existingids); arOld.Reverse(); // Reverse the order to make it agree with the other sets } else { // If the list exists, but some entries reported as existing are missing from it, they should be explicidly added also // They should be added to the end, thus to the beginning of the reverse array in the reverse order IntArrayList arOldOld = arOld; arOld = new IntArrayList(existingids.Count); foreach (int id in existingids) { if ((!hashOld.Contains(id)) && (!hashNewcomers.Contains(id))) // Don't add the items that are being dragged { arOld.Add(id); } } arOld.Reverse(); arOld.AddRange(arOldOld); } } // Here the new order will be stored; allocate to the maximum possible size IntArrayList arNew = new IntArrayList(arOld.Count + ids.Count); // Special check: if the droptarget is not present in the list, add all the resources either to the beginning or to the end if ((idPoint == 0) || (arOld.IndexOf(idPoint) < 0)) { // Droptarget not found if (bInsertAfter) { arNew.AddRange(arNewcomers); arNew.AddRange(arOld); } else { arNew.AddRange(arOld); arNew.AddRange(arNewcomers); } } else { // Droptarget is there, go on inserting foreach (int idOld in arOld) { // Take along the current resource (if it's not excluded due to being in the inserted resources, and if it's not a droptarget in case the insertion should go before (=added after) the target) if ((!hashNewcomers.Contains(idOld)) && (!((idOld == idPoint) && (bInsertAfter)))) { arNew.Add(idOld); } // Copy the newcomers if we're on the droptarget if (idOld == idPoint) { arNew.AddRange(arNewcomers); } // Take along the current resource in case it's the droptarget, and the insertion is to go after (=added before) it, and it has not been already included in the newcomers list if ((idOld == idPoint) && (bInsertAfter) && (!hashNewcomers.Contains(idPoint))) { arNew.Add(idPoint); } } } // Commit WriteSortOrder(arNew); }