1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
|
/*
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Diagnostics;
using SCG = System.Collections.Generic;
namespace C5
{
/// <summary>
/// A collection class implementing a sorted dynamic array data structure.
/// </summary>
[Serializable]
public class SortedArray<T> : ArrayBase<T>, IIndexedSorted<T>
{
#region Features
/// <summary>
/// A debugging artifact. To be removed.
/// </summary>
[Flags]
public enum Feature : short
{
/// <summary>
/// A debugging artifact. To be removed.
/// </summary>
Standard = 0
}
static Feature features = Feature.Standard;
/// <summary>
/// A debugging artifact. To be removed.
/// </summary>
/// <value></value>
public static Feature Features { get { return features; } }
#endregion
#region Events
/// <summary>
///
/// </summary>
/// <value></value>
public override EventTypeEnum ListenableEvents { get { return EventTypeEnum.Basic; } }
#endregion
#region Fields
SCG.IComparer<T> comparer;
#endregion
#region Util
/// <summary>
///
/// </summary>
/// <param name="item">The item to search for</param>
/// <param name="mid">The least index, mid, for which array[mid] >= item</param>
/// <returns>True if item found</returns>
private bool binarySearch(T item, out int mid)
{
int bot = 0, top = size;
mid = top / 2;
while (top > bot)
{
int c;
if ((c = comparer.Compare(array[mid], item)) == 0)
return true;
if (c > 0)
{ top = mid; }
else
{ bot = mid + 1; }
mid = (bot + top) / 2;
}
return false;
}
private int indexOf(T item)
{
int ind;
if (binarySearch(item, out ind))
return ind;
return ~ind;
}
#endregion
#region Constructors
/// <summary>
/// Create a dynamic sorted array with a natural comparer
/// (and item equalityComparer, assumed compatible)
/// </summary>
/// <exception cref="NotComparableException">If <code>T</code> is not comparable.
/// </exception>
public SortedArray() : this(8) { }
/// <summary>
/// Create a dynamic sorted array with a natural comparer
/// (and item equalityComparer, assumed compatible)
/// and prescribed initial capacity.
/// </summary>
/// <exception cref="NotComparableException">If <code>T</code> is not comparable.
/// </exception>
/// <param name="capacity">The capacity</param>
public SortedArray(int capacity)
: this(capacity, Comparer<T>.Default, EqualityComparer<T>.Default) { }
/// <summary>
/// Create a dynamic sorted array with an external comparer.
/// <para>The itemequalityComparer will be compatible
/// <see cref="T:C5.ComparerZeroHashCodeEqualityComparer`1"/> since the
/// default equalityComparer for T (<see cref="P:C5.EqualityComparer`1.Default"/>)
/// is unlikely to be compatible with the external comparer. This makes the
/// array inadequate for use as item in a collection of unsequenced or sequenced sets or bags
/// (<see cref="T:C5.ICollection`1"/> and <see cref="T:C5.ISequenced`1"/>)
/// </para>
/// </summary>
/// <param name="comparer">The comparer</param>
public SortedArray(SCG.IComparer<T> comparer)
: this(8, comparer) { }
/// <summary>
/// Create a dynamic sorted array with an external comparer
/// and prescribed initial capacity.
/// <para>The itemequalityComparer will be a compatible
/// <see cref="T:C5.ComparerZeroHashCodeEqualityComparer`1"/> since the
/// default equalityComparer for T (<see cref="P:C5.EqualityComparer`1.Default"/>)
/// is unlikely to be compatible with the external comparer. This makes the
/// sorted array inadequate for use as item in a collection of unsequenced or sequenced sets or bags
/// (<see cref="T:C5.ICollection`1"/> and <see cref="T:C5.ISequenced`1"/>)
/// </para>
/// </summary>
/// <param name="capacity">The capacity</param>
/// <param name="comparer">The comparer</param>
public SortedArray(int capacity, SCG.IComparer<T> comparer)
: this(capacity, comparer, new ComparerZeroHashCodeEqualityComparer<T>(comparer)) { }
/// <summary>
/// Create a dynamic sorted array with an external comparer, an external item equalityComparer
/// and prescribed initial capacity. This is the constructor to use if the collection
/// will be used as item in a hash table based collection.
/// </summary>
/// <param name="capacity">The capacity</param>
/// <param name="comparer">The item comparer</param>
/// <param name="equalityComparer">The item equalityComparer (assumed compatible)</param>
public SortedArray(int capacity, SCG.IComparer<T> comparer, SCG.IEqualityComparer<T> equalityComparer)
: base(capacity, equalityComparer)
{
if (comparer == null)
throw new NullReferenceException("Comparer cannot be null");
this.comparer = comparer;
}
#endregion
#region IIndexedSorted<T> Members
/// <summary>
/// Determine the number of items at or above a supplied threshold.
/// </summary>
/// <param name="bot">The lower bound (inclusive)</param>
/// <returns>The number of matcing items.</returns>
[Tested]
public int CountFrom(T bot)
{
int lo;
binarySearch(bot, out lo);
return size - lo;
}
/// <summary>
/// Determine the number of items between two supplied thresholds.
/// </summary>
/// <param name="bot">The lower bound (inclusive)</param>
/// <param name="top">The upper bound (exclusive)</param>
/// <returns>The number of matcing items.</returns>
[Tested]
public int CountFromTo(T bot, T top)
{
int lo, hi;
binarySearch(bot, out lo);
binarySearch(top, out hi);
return hi > lo ? hi - lo : 0;
}
/// <summary>
/// Determine the number of items below a supplied threshold.
/// </summary>
/// <param name="top">The upper bound (exclusive)</param>
/// <returns>The number of matcing items.</returns>
[Tested]
public int CountTo(T top)
{
int hi;
binarySearch(top, out hi);
return hi;
}
/// <summary>
/// Query this sorted collection for items greater than or equal to a supplied value.
/// </summary>
/// <param name="bot">The lower bound (inclusive).</param>
/// <returns>The result directed collection.</returns>
[Tested]
public IDirectedCollectionValue<T> RangeFrom(T bot)
{
int lo;
binarySearch(bot, out lo);
return new Range(this, lo, size - lo, true);
}
/// <summary>
/// Query this sorted collection for items between two supplied values.
/// </summary>
/// <param name="bot">The lower bound (inclusive).</param>
/// <param name="top">The upper bound (exclusive).</param>
/// <returns>The result directed collection.</returns>
[Tested]
public IDirectedCollectionValue<T> RangeFromTo(T bot, T top)
{
int lo, hi;
binarySearch(bot, out lo);
binarySearch(top, out hi);
int sz = hi - lo;
return new Range(this, lo, sz, true);
}
/// <summary>
/// Query this sorted collection for items less than a supplied value.
/// </summary>
/// <param name="top">The upper bound (exclusive).</param>
/// <returns>The result directed collection.</returns>
[Tested]
public IDirectedCollectionValue<T> RangeTo(T top)
{
int hi;
binarySearch(top, out hi);
return new Range(this, 0, hi, true);
}
/// <summary>
/// Create a new indexed sorted collection consisting of the items of this
/// indexed sorted collection satisfying a certain predicate.
/// </summary>
/// <param name="f">The filter delegate defining the predicate.</param>
/// <returns>The new indexed sorted collection.</returns>
[Tested]
public IIndexedSorted<T> FindAll(Fun<T, bool> f)
{
SortedArray<T> res = new SortedArray<T>(comparer);
int j = 0, rescap = res.array.Length;
for (int i = 0; i < size; i++)
{
T a = array[i];
if (f(a))
{
if (j == rescap) res.expand(rescap = 2 * rescap, j);
res.array[j++] = a;
}
}
res.size = j;
return res;
}
/// <summary>
/// Create a new indexed sorted collection consisting of the results of
/// mapping all items of this list.
/// <exception cref="ArgumentException"/> if the map is not increasing over
/// the items of this collection (with respect to the two given comparison
/// relations).
/// </summary>
/// <param name="m">The delegate definging the map.</param>
/// <param name="c">The comparion relation to use for the result.</param>
/// <returns>The new sorted collection.</returns>
[Tested]
public IIndexedSorted<V> Map<V>(Fun<T, V> m, SCG.IComparer<V> c)
{
SortedArray<V> res = new SortedArray<V>(size, c);
if (size > 0)
{
V oldv = res.array[0] = m(array[0]), newv;
for (int i = 1; i < size; i++)
{
if (c.Compare(oldv, newv = res.array[i] = m(array[i])) >= 0)
throw new ArgumentException("mapper not monotonic");
oldv = newv;
}
}
res.size = size;
return res;
}
#endregion
#region ISorted<T> Members
/// <summary>
/// Find the strict predecessor of item in the sorted array,
/// that is, the greatest item in the collection smaller than the item.
/// </summary>
/// <param name="item">The item to find the predecessor for.</param>
/// <param name="res">The predecessor, if any; otherwise the default value for T.</param>
/// <returns>True if item has a predecessor; otherwise false.</returns>
public bool TryPredecessor(T item, out T res)
{
int lo;
binarySearch(item, out lo);
if (lo == 0)
{
res = default(T);
return false;
}
else
{
res = array[lo - 1];
return true;
}
}
/// <summary>
/// Find the strict successor of item in the sorted array,
/// that is, the least item in the collection greater than the supplied value.
/// </summary>
/// <param name="item">The item to find the successor for.</param>
/// <param name="res">The successor, if any; otherwise the default value for T.</param>
/// <returns>True if item has a successor; otherwise false.</returns>
public bool TrySuccessor(T item, out T res)
{
int hi;
if (binarySearch(item, out hi))
hi++;
if (hi >= size)
{
res = default(T);
return false;
} else {
res = array[hi];
return true;
}
}
/// <summary>
/// Find the weak predecessor of item in the sorted array,
/// that is, the greatest item in the collection smaller than or equal to the item.
/// </summary>
/// <param name="item">The item to find the weak predecessor for.</param>
/// <param name="res">The weak predecessor, if any; otherwise the default value for T.</param>
/// <returns>True if item has a weak predecessor; otherwise false.</returns>
public bool TryWeakPredecessor(T item, out T res)
{
int lo;
if (!binarySearch(item, out lo))
lo--;
if (lo < 0)
{
res = default(T);
return false;
}
else
{
res = array[lo];
return true;
}
}
/// <summary>
/// Find the weak successor of item in the sorted array,
/// that is, the least item in the collection greater than or equal to the supplied value.
/// </summary>
/// <param name="item">The item to find the weak successor for.</param>
/// <param name="res">The weak successor, if any; otherwise the default value for T.</param>
/// <returns>True if item has a weak successor; otherwise false.</returns>
public bool TryWeakSuccessor(T item, out T res)
{
int hi;
binarySearch(item, out hi);
if (hi >= size)
{
res = default(T);
return false;
}
else
{
res = array[hi];
return true;
}
}
/// <summary>
/// Find the strict predecessor in the sorted collection of a particular value,
/// i.e. the largest item in the collection less than the supplied value.
/// </summary>
/// <exception cref="NoSuchItemException"> if no such element exists (the
/// supplied value is less than or equal to the minimum of this collection.)</exception>
/// <param name="item">The item to find the predecessor for.</param>
/// <returns>The predecessor.</returns>
[Tested]
public T Predecessor(T item)
{
int lo;
binarySearch(item, out lo);
if (lo == 0)
throw new NoSuchItemException();
return array[lo - 1];
}
/// <summary>
/// Find the strict successor in the sorted collection of a particular value,
/// i.e. the least item in the collection greater than the supplied value.
/// </summary>
/// <exception cref="NoSuchItemException"> if no such element exists (the
/// supplied value is greater than or equal to the maximum of this collection.)</exception>
/// <param name="item">The item to find the successor for.</param>
/// <returns>The successor.</returns>
[Tested]
public T Successor(T item)
{
int hi;
if (binarySearch(item, out hi)) hi++;
if (hi >= size)
throw new NoSuchItemException();
return array[hi];
}
/// <summary>
/// Find the weak predecessor in the sorted collection of a particular value,
/// i.e. the largest item in the collection less than or equal to the supplied value.
/// <exception cref="NoSuchItemException"/> if no such element exists (the
/// supplied value is less than the minimum of this collection.)
/// </summary>
/// <param name="item">The item to find the weak predecessor for.</param>
/// <returns>The weak predecessor.</returns>
[Tested]
public T WeakPredecessor(T item)
{
int lo;
if (!binarySearch(item, out lo)) lo--;
if (lo < 0)
throw new NoSuchItemException();
return array[lo];
}
/// <summary>
/// Find the weak successor in the sorted collection of a particular value,
/// i.e. the least item in the collection greater than or equal to the supplied value.
/// </summary>
/// <exception cref="NoSuchItemException"> if no such element exists (the
/// supplied value is greater than the maximum of this collection.)</exception>
/// <param name="item">The item to find the weak successor for.</param>
/// <returns>The weak successor.</returns>
[Tested]
public T WeakSuccessor(T item)
{
int hi;
binarySearch(item, out hi);
if (hi >= size)
throw new NoSuchItemException();
return array[hi];
}
/// <summary>
/// Perform a search in the sorted collection for the ranges in which a
/// non-increasing (i.e. weakly decrerasing) function from the item type to
/// <code>int</code> is
/// negative, zero respectively positive. If the supplied cut function is
/// not non-increasing, the result of this call is undefined.
/// </summary>
/// <param name="c">The cut function <code>T</code> to <code>int</code>, given
/// as an <code>IComparable<T></code> object, where the cut function is
/// the <code>c.CompareTo(T that)</code> method.</param>
/// <param name="low">Returns the largest item in the collection, where the
/// cut function is positive (if any).</param>
/// <param name="lowIsValid">True if the cut function is positive somewhere
/// on this collection.</param>
/// <param name="high">Returns the least item in the collection, where the
/// cut function is negative (if any).</param>
/// <param name="highIsValid">True if the cut function is negative somewhere
/// on this collection.</param>
/// <returns></returns>
[Tested]
public bool Cut(IComparable<T> c, out T low, out bool lowIsValid, out T high, out bool highIsValid)
{
int lbest = -1, rbest = size;
low = default(T);
lowIsValid = false;
high = default(T);
highIsValid = false;
int bot = 0, top = size, mid, comp = -1, sol;
mid = top / 2;
while (top > bot)
{
if ((comp = c.CompareTo(array[mid])) == 0)
break;
if (comp < 0)
{ rbest = top = mid; }
else
{ lbest = mid; bot = mid + 1; }
mid = (bot + top) / 2;
}
if (comp != 0)
{
if (lbest >= 0) { lowIsValid = true; low = array[lbest]; }
if (rbest < size) { highIsValid = true; high = array[rbest]; }
return false;
}
sol = mid;
bot = sol - 1;
//Invariant: c.Compare(array[x]) < 0 when rbest <= x < size
// c.Compare(array[x]) >= 0 when x < bot)
//(Assuming c.Compare monotonic)
while (rbest > bot)
{
mid = (bot + rbest) / 2;
if (c.CompareTo(array[mid]) < 0)
{ rbest = mid; }
else
{ bot = mid + 1; }
}
if (rbest < size) { highIsValid = true; high = array[rbest]; }
top = sol + 1;
//Invariant: c.Compare(array[x]) > 0 when 0 <= x <= lbest
// c.Compare(array[x]) <= 0 when x>top)
//(Assuming c.Compare monotonic)
while (top > lbest)
{
mid = (lbest + top + 1) / 2;
if (c.CompareTo(array[mid]) > 0)
{ lbest = mid; }
else
{ top = mid - 1; }
}
if (lbest >= 0) { lowIsValid = true; low = array[lbest]; }
return true;
}
IDirectedEnumerable<T> ISorted<T>.RangeFrom(T bot)
{ return RangeFrom(bot); }
IDirectedEnumerable<T> ISorted<T>.RangeFromTo(T bot, T top)
{ return RangeFromTo(bot, top); }
IDirectedEnumerable<T> ISorted<T>.RangeTo(T top)
{ return RangeTo(top); }
/// <summary>
/// Create a directed collection with the same items as this collection.
/// </summary>
/// <returns>The result directed collection.</returns>
[Tested]
public IDirectedCollectionValue<T> RangeAll()
{ return new Range(this, 0, size, true); }
/// <summary>
/// Add all the items from another collection with an enumeration order that
/// is increasing in the items.
/// <exception cref="ArgumentException"/> if the enumerated items turns out
/// not to be in increasing order.
/// </summary>
/// <param name="items">The collection to add.</param>
/// <typeparam name="U"></typeparam>
[Tested]
public void AddSorted<U>(SCG.IEnumerable<U> items) where U : T
{
//Unless items have <=1 elements we would expect it to be
//too expensive to do repeated inserts, thus:
updatecheck();
int j = 0, i = 0, c = -1, itemcount = EnumerableBase<U>.countItems(items), numAdded = 0;
SortedArray<T> res = new SortedArray<T>(size + itemcount, comparer);
T lastitem = default(T);
T[] addedItems = new T[itemcount];
foreach (T item in items)
{
while (i < size && (c = comparer.Compare(array[i], item)) <= 0)
{
lastitem = res.array[j++] = array[i++];
if (c == 0)
goto next;
}
if (j > 0 && comparer.Compare(lastitem, item) >= 0)
throw new ArgumentException("Argument not sorted");
addedItems[numAdded++] = lastitem = res.array[j++] = item;
next:
c = -1;
}
while (i < size) res.array[j++] = array[i++];
size = j;
array = res.array;
raiseForAddAll(addedItems, numAdded);
}
/// <summary>
/// Remove all items of this collection above or at a supplied threshold.
/// </summary>
/// <param name="low">The lower threshold (inclusive).</param>
[Tested]
public void RemoveRangeFrom(T low)
{
int lowind;
binarySearch(low, out lowind);
if (lowind == size)
return;
T[] removed = new T[size - lowind];
Array.Copy(array, lowind, removed, 0, removed.Length);
Array.Reverse(removed);
Array.Clear(array, lowind, size - lowind);
size = lowind;
raiseForRemoveRange(removed);
}
/// <summary>
/// Remove all items of this collection between two supplied thresholds.
/// </summary>
/// <param name="low">The lower threshold (inclusive).</param>
/// <param name="hi">The upper threshold (exclusive).</param>
[Tested]
public void RemoveRangeFromTo(T low, T hi)
{
int lowind, highind;
binarySearch(low, out lowind);
binarySearch(hi, out highind);
if (highind <= lowind)
return;
T[] removed = new T[highind - lowind];
Array.Copy(array, lowind, removed, 0, removed.Length);
Array.Reverse(removed);
Array.Copy(array, highind, array, lowind, size - highind);
Array.Clear(array, size - highind + lowind, highind - lowind);
size -= highind - lowind;
raiseForRemoveRange(removed);
}
/// <summary>
/// Remove all items of this collection below a supplied threshold.
/// </summary>
/// <param name="hi">The upper threshold (exclusive).</param>
[Tested]
public void RemoveRangeTo(T hi)
{
int highind;
binarySearch(hi, out highind);
if (highind == 0)
return;
T[] removed = new T[highind];
Array.Copy(array, 0, removed, 0, removed.Length);
Array.Copy(array, highind, array, 0, size - highind);
Array.Clear(array, size - highind, highind);
size = size - highind;
raiseForRemoveRange(removed);
}
private void raiseForRemoveRange(T[] removed)
{
foreach(T item in removed)
raiseItemsRemoved(item, 1);
if(removed.Length > 0)
raiseCollectionChanged();
}
#endregion
#region ICollection<T> Members
/// <summary>
/// The value is symbolic indicating the type of asymptotic complexity
/// in terms of the size of this collection (worst-case).
/// </summary>
/// <value>Speed.Log</value>
[Tested]
public Speed ContainsSpeed { [Tested]get { return Speed.Log; } }
/// <summary>
/// Remove all items from this collection, resetting internal array size.
/// </summary>
[Tested]
override public void Clear()
{
int oldCount = size;
base.Clear();
if(oldCount > 0)
{
raiseCollectionCleared(true, oldCount);
raiseCollectionChanged();
}
}
/// <summary>
/// Check if this collection contains (an item equivalent according to the
/// itemequalityComparer) to a particular value.
/// </summary>
/// <param name="item">The value to check for.</param>
/// <returns>True if the items is in this collection.</returns>
[Tested]
public bool Contains(T item)
{
int ind;
return binarySearch(item, out ind);
}
/// <summary>
/// Check if this collection contains an item equivalent according to the
/// itemequalityComparer to a particular value. If so, return in the ref argument (a
/// binary copy of) the actual value found.
/// </summary>
/// <param name="item">The value to look for.</param>
/// <returns>True if the items is in this collection.</returns>
[Tested]
public bool Find(ref T item)
{
int ind;
if (binarySearch(item, out ind))
{
item = array[ind];
return true;
}
return false;
}
//This should probably just be bool Add(ref T item); !!!
/// <summary>
/// Check if this collection contains an item equivalent according to the
/// itemequalityComparer to a particular value. If so, return in the ref argument (a
/// binary copy of) the actual value found. Else, add the item to the collection.
/// </summary>
/// <param name="item">The value to look for.</param>
/// <returns>True if the item was added (hence not found).</returns>
[Tested]
public bool FindOrAdd(ref T item)
{
updatecheck();
int ind;
if (binarySearch(item, out ind))
{
item = array[ind];
return true;
}
if (size == array.Length) expand();
Array.Copy(array, ind, array, ind + 1, size - ind);
array[ind] = item;
size++;
raiseForAdd(item);
return false;
}
/// <summary>
/// Check if this collection contains an item equivalent according to the
/// itemequalityComparer to a particular value. If so, update the item in the collection
/// to with a binary copy of the supplied value. If the collection has bag semantics,
/// it is implementation dependent if this updates all equivalent copies in
/// the collection or just one.
/// </summary>
/// <param name="item">Value to update.</param>
/// <returns>True if the item was found and hence updated.</returns>
[Tested]
public bool Update(T item)
{ T olditem; return Update(item, out olditem); }
/// <summary>
///
/// </summary>
/// <param name="item"></param>
/// <param name="olditem"></param>
/// <returns></returns>
public bool Update(T item, out T olditem)
{
updatecheck();
int ind;
if (binarySearch(item, out ind))
{
olditem = array[ind];
array[ind] = item;
raiseForUpdate(item, olditem);
return true;
}
olditem = default(T);
return false;
}
/// <summary>
/// Check if this collection contains an item equivalent according to the
/// itemequalityComparer to a particular value. If so, update the item in the collection
/// to with a binary copy of the supplied value; else add the value to the collection.
/// </summary>
/// <param name="item">Value to add or update.</param>
/// <returns>True if the item was found and updated (hence not added).</returns>
[Tested]
public bool UpdateOrAdd(T item)
{ T olditem; return UpdateOrAdd(item, out olditem); }
/// <summary>
///
/// </summary>
/// <param name="item"></param>
/// <param name="olditem"></param>
/// <returns></returns>
public bool UpdateOrAdd(T item, out T olditem)
{
updatecheck();
int ind;
if (binarySearch(item, out ind))
{
olditem = array[ind];
array[ind] = item;
raiseForUpdate(item, olditem);
return true;
}
if (size == array.Length) expand();
Array.Copy(array, ind, array, ind + 1, size - ind);
array[ind] = item;
size++;
olditem = default(T);
raiseForAdd(item);
return false;
}
/// <summary>
/// Remove a particular item from this collection. If the collection has bag
/// semantics only one copy equivalent to the supplied item is removed.
/// </summary>
/// <param name="item">The value to remove.</param>
/// <returns>True if the item was found (and removed).</returns>
[Tested]
public bool Remove(T item)
{
int ind;
updatecheck();
if (binarySearch(item, out ind))
{
T removeditem = array[ind];
Array.Copy(array, ind + 1, array, ind, size - ind - 1);
array[--size] = default(T);
raiseForRemove(removeditem);
return true;
}
return false;
}
/// <summary>
/// Remove a particular item from this collection if found. If the collection
/// has bag semantics only one copy equivalent to the supplied item is removed,
/// which one is implementation dependent.
/// If an item was removed, report a binary copy of the actual item removed in
/// the argument.
/// </summary>
/// <param name="item">The value to remove.</param>
/// <param name="removeditem">The removed value.</param>
/// <returns>True if the item was found (and removed).</returns>
[Tested]
public bool Remove(T item, out T removeditem)
{
int ind;
updatecheck();
if (binarySearch(item, out ind))
{
removeditem = array[ind];
Array.Copy(array, ind + 1, array, ind, size - ind - 1);
array[--size] = default(T);
raiseForRemove(removeditem);
return true;
}
removeditem = default(T);
return false;
}
/// <summary>
/// Remove all items in another collection from this one.
/// </summary>
/// <typeparam name="U"></typeparam>
/// <param name="items">The items to remove.</param>
[Tested]
public void RemoveAll<U>(SCG.IEnumerable<U> items) where U : T
{
//This is O(m*logn) with n bits extra storage
//(Not better to collect the m items and sort them)
updatecheck();
RaiseForRemoveAllHandler raiseHandler = new RaiseForRemoveAllHandler(this);
bool mustFire = raiseHandler.MustFire;
int[] toremove = new int[(size >> 5) + 1];
int ind, j = 0;
foreach (T item in items)
if (binarySearch(item, out ind))
toremove[ind >> 5] |= 1 << (ind & 31);
for (int i = 0; i < size; i++)
if ((toremove[i >> 5] & (1 << (i & 31))) == 0)
array[j++] = array[i];
else if (mustFire)
raiseHandler.Remove(array[i]);
Array.Clear(array, j, size - j);
size = j;
if (mustFire)
raiseHandler.Raise();
}
/// <summary>
/// Remove all items not in some other collection from this one.
/// </summary>
/// <typeparam name="U"></typeparam>
/// <param name="items">The items to retain.</param>
[Tested]
public void RetainAll<U>(SCG.IEnumerable<U> items) where U : T
{
//This is O(m*logn) with n bits extra storage
//(Not better to collect the m items and sort them)
updatecheck();
RaiseForRemoveAllHandler raiseHandler = new RaiseForRemoveAllHandler(this);
bool mustFire = raiseHandler.MustFire;
int[] toretain = new int[(size >> 5) + 1];
int ind, j = 0;
foreach (T item in items)
if (binarySearch(item, out ind))
toretain[ind >> 5] |= 1 << (ind & 31);
for (int i = 0; i < size; i++)
if ((toretain[i >> 5] & (1 << (i & 31))) != 0)
array[j++] = array[i];
else if (mustFire)
raiseHandler.Remove(array[i]);
Array.Clear(array, j, size - j);
size = j;
if (mustFire)
raiseHandler.Raise();
}
/// <summary>
/// Check if this collection contains all the values in another collection.
/// Multiplicities are not taken into account.
/// </summary>
/// <param name="items">The </param>
/// <typeparam name="U"></typeparam>
/// <returns>True if all values in <code>items</code>is in this collection.</returns>
[Tested]
public bool ContainsAll<U>(SCG.IEnumerable<U> items) where U : T
{
int tmp;
foreach (T item in items)
if (!binarySearch(item, out tmp))
return false;
return true;
}
/// <summary>
/// Count the number of items of the collection equal to a particular value.
/// Returns 0 if and only if the value is not in the collection.
/// </summary>
/// <param name="item">The value to count.</param>
/// <returns>The number of copies found (0 or 1).</returns>
[Tested]
public int ContainsCount(T item)
{
int tmp;
return binarySearch(item, out tmp) ? 1 : 0;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public virtual ICollectionValue<T> UniqueItems() { return this; }
/// <summary>
///
/// </summary>
/// <returns></returns>
public virtual ICollectionValue<KeyValuePair<T, int>> ItemMultiplicities()
{
return new MultiplicityOne<T>(this);
}
/// <summary>
/// Remove all (0 or 1) items equivalent to a given value.
/// </summary>
/// <param name="item">The value to remove.</param>
[Tested]
public void RemoveAllCopies(T item) { Remove(item); }
/// <summary>
/// Check the integrity of the internal data structures of this collection.
/// Only avaliable in DEBUG builds???
/// </summary>
/// <returns>True if check does not fail.</returns>
[Tested]
public override bool Check()
{
bool retval = true;
if (size > array.Length)
{
Console.WriteLine("Bad size ({0}) > array.Length ({1})", size, array.Length);
return false;
}
for (int i = 0; i < size; i++)
{
if ((object)(array[i]) == null)
{
Console.WriteLine("Bad element: null at index {0}", i);
return false;
}
if (i > 0 && comparer.Compare(array[i], array[i - 1]) <= 0)
{
Console.WriteLine("Inversion at index {0}", i);
retval = false;
}
}
return retval;
}
#endregion
#region IExtensible<T> Members
/// <summary>
///
/// </summary>
/// <value>False since this collection has set semantics</value>
[Tested]
public bool AllowsDuplicates { [Tested]get { return false; } }
/// <summary>
/// By convention this is true for any collection with set semantics.
/// </summary>
/// <value>True if only one representative of a group of equal items
/// is kept in the collection together with the total count.</value>
public virtual bool DuplicatesByCounting { get { return true; } }
/// <summary>
/// Add an item to this collection if possible. If this collection has set
/// semantics, the item will be added if not already in the collection. If
/// bag semantics, the item will always be added.
/// </summary>
/// <param name="item">The item to add.</param>
/// <returns>True if item was added.</returns>
[Tested]
public bool Add(T item)
{
updatecheck();
int ind;
if (binarySearch(item, out ind)) return false;
insert(ind, item);
raiseForAdd(item);
return true;
}
/// <summary>
/// Add an item to this collection if possible.
/// </summary>
/// <param name="item">The item to add.</param>
[Tested]
void SCG.ICollection<T>.Add(T item)
{
Add(item);
}
/// <summary>
/// Add the elements from another collection with a more specialized item type
/// to this collection. Since this
/// collection has set semantics, only items not already in the collection
/// will be added.
/// </summary>
/// <typeparam name="U">The type of items to add</typeparam>
/// <param name="items">The items to add</param>
[Tested]
public void AddAll<U>(SCG.IEnumerable<U> items) where U : T
{
int toadd = EnumerableBase<U>.countItems(items), newsize = array.Length;
while (newsize < size + toadd) { newsize *= 2; }
T[] newarr = new T[newsize];
toadd = 0;
foreach (T item in items) newarr[size + toadd++] = item;
Sorting.IntroSort<T>(newarr, size, toadd, comparer);
int j = 0, i = 0, numAdded = 0;
T lastitem = default(T);
T[] addedItems = new T[toadd];
//The following eliminates duplicates (including duplicates in input)
//while merging the old and new collection
for (int k = size, klimit = size + toadd; k < klimit; k++)
{
while (i < size && comparer.Compare(array[i], newarr[k]) <= 0)
lastitem = newarr[j++] = array[i++];
if (j == 0 || comparer.Compare(lastitem, newarr[k]) < 0)
addedItems[numAdded++] = lastitem = newarr[j++] = newarr[k];
}
while (i < size) newarr[j++] = array[i++];
Array.Clear(newarr, j, size + toadd - j);
size = j;
array = newarr;
raiseForAddAll(addedItems, numAdded);
}
private void raiseForAddAll(T[] addedItems, int numAdded)
{
if ((ActiveEvents & EventTypeEnum.Added) != 0)
for(int i = 0 ;i < numAdded; i += 1)
raiseItemsAdded(addedItems[i], 1);
if (numAdded > 0)
raiseCollectionChanged();
}
#endregion
#region IPriorityQueue<T> Members
/// <summary>
/// Find the current least item of this priority queue.
/// </summary>
/// <returns>The least item.</returns>
[Tested]
public T FindMin()
{
if (size == 0)
throw new NoSuchItemException();
return array[0];
}
/// <summary>
/// Remove the least item from this priority queue.
/// </summary>
/// <returns>The removed item.</returns>
[Tested]
public T DeleteMin()
{
updatecheck();
if (size == 0)
throw new NoSuchItemException();
T retval = array[0];
size--;
Array.Copy(array, 1, array, 0, size);
array[size] = default(T);
raiseForRemove(retval);
return retval;
}
/// <summary>
/// Find the current largest item of this priority queue.
/// </summary>
/// <returns>The largest item.</returns>
[Tested]
public T FindMax()
{
if (size == 0)
throw new NoSuchItemException();
return array[size - 1];
}
/// <summary>
/// Remove the largest item from this priority queue.
/// </summary>
/// <returns>The removed item.</returns>
[Tested]
public T DeleteMax()
{
updatecheck();
if (size == 0)
throw new NoSuchItemException();
T retval = array[size - 1];
size--;
array[size] = default(T);
raiseForRemove(retval);
return retval;
}
/// <summary>
/// The comparer object supplied at creation time for this collection
/// </summary>
/// <value>The comparer</value>
public SCG.IComparer<T> Comparer { get { return comparer; } }
#endregion
#region IIndexed<T> Members
/// <summary>
/// <exception cref="IndexOutOfRangeException"/> if i is negative or
/// >= the size of the collection.
/// </summary>
/// <value>The i'th item of this list.</value>
/// <param name="i">the index to lookup</param>
[Tested]
public T this[int i]
{
[Tested]
get
{
if (i < 0 || i >= size)
throw new IndexOutOfRangeException();
return array[i];
}
}
/// <summary>
///
/// </summary>
/// <value></value>
public virtual Speed IndexingSpeed { get { return Speed.Constant; } }
/// <summary>
/// Searches for an item in the list going forwrds from the start.
/// </summary>
/// <param name="item">Item to search for.</param>
/// <returns>Index of item from start.</returns>
[Tested]
public int IndexOf(T item) { return indexOf(item); }
/// <summary>
/// Searches for an item in the list going backwords from the end.
/// </summary>
/// <param name="item">Item to search for.</param>
/// <returns>Index of of item from the end.</returns>
[Tested]
public int LastIndexOf(T item) { return indexOf(item); }
/// <summary>
/// Remove the item at a specific position of the list.
/// <exception cref="IndexOutOfRangeException"/> if i is negative or
/// >= the size of the collection.
/// </summary>
/// <param name="i">The index of the item to remove.</param>
/// <returns>The removed item.</returns>
[Tested]
public T RemoveAt(int i)
{
if (i < 0 || i >= size)
throw new IndexOutOfRangeException("Index out of range for sequenced collectionvalue");
updatecheck();
T retval = array[i];
size--;
Array.Copy(array, i + 1, array, i, size - i);
array[size] = default(T);
raiseForRemoveAt(i, retval);
return retval;
}
/// <summary>
/// Remove all items in an index interval.
/// <exception cref="IndexOutOfRangeException"/>???.
/// </summary>
/// <param name="start">The index of the first item to remove.</param>
/// <param name="count">The number of items to remove.</param>
[Tested]
public void RemoveInterval(int start, int count)
{
updatecheck();
checkRange(start, count);
Array.Copy(array, start + count, array, start, size - start - count);
size -= count;
Array.Clear(array, size, count);
raiseForRemoveInterval(start, count);
}
private void raiseForRemoveInterval(int start, int count)
{
if (ActiveEvents != 0 && count > 0)
{
raiseCollectionCleared(size == 0, count);
raiseCollectionChanged();
}
}
#endregion
#region IDirectedEnumerable<T> Members
/// <summary>
/// Create a collection containing the same items as this collection, but
/// whose enumerator will enumerate the items backwards. The new collection
/// will become invalid if the original is modified. Method typicaly used as in
/// <code>foreach (T x in coll.Backwards()) {...}</code>
/// </summary>
/// <returns>The backwards collection.</returns>
[Tested]
IDirectedEnumerable<T> IDirectedEnumerable<T>.Backwards()
{ return Backwards(); }
#endregion
#region ICloneable Members
/// <summary>
/// Make a shallow copy of this SortedArray.
/// </summary>
/// <returns></returns>
public virtual object Clone()
{
SortedArray<T> clone = new SortedArray<T>(size, comparer, itemequalityComparer);
clone.AddSorted(this);
return clone;
}
#endregion
}
}
|