/usr/lib/ruby/vendor_ruby/hamster/sorted_set.rb is in ruby-hamster 3.0.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | require "hamster/immutable"
require "hamster/enumerable"
module Hamster
# A `SortedSet` is a collection of ordered values with no duplicates. Unlike a
# {Vector}, in which items can appear in any arbitrary order, a `SortedSet` always
# keeps items either in their natural order, or in an order defined by a comparator
# block which is provided at initialization time.
#
# `SortedSet` uses `#<=>` (or its comparator block) to determine which items are
# equivalent. If the comparator indicates that an existing item and a new item are
# equal, any attempt to insert the new item will have no effect.
#
# This means that *all* the items inserted into any one `SortedSet` must all be
# comparable. For example, you cannot put `String`s and `Integer`s in the same
# `SortedSet`. This is unlike {Set}, which can store items of any type, as long
# as they all support `#hash` and `#eql?`.
#
# A `SortedSet` can be created in either of the following ways:
#
# Hamster::SortedSet.new([1, 2, 3]) # any Enumerable can be used to initialize
# Hamster::SortedSet['A', 'B', 'C', 'D']
#
# Or if you want to use a custom ordering:
#
# Hamster::SortedSet.new([1,2,3]) { |a, b| -a <=> -b }
# Hamster::SortedSet.new([1, 2, 3]) { |num| -num }
#
# `SortedSet` can use a 2-parameter block which returns 0, 1, or -1
# as a comparator (like `Array#sort`), *or* use a 1-parameter block to derive sort
# keys (like `Array#sort_by`) which will be compared using `#<=>`.
#
# Like all Hamster collections, `SortedSet`s are immutable. Any operation which you
# might expect to "modify" a `SortedSet` will actually return a new collection and
# leave the existing one unchanged.
#
# `SortedSet` supports the same basic set-theoretic operations as {Set}, including
# {#union}, {#intersection}, {#difference}, and {#exclusion}, as well as {#subset?},
# {#superset?}, and so on. Unlike {Set}, it does not define comparison operators like
# `#>` or `#<` as aliases for the superset/subset predicates. Instead, these comparison
# operators do a item-by-item comparison between the `SortedSet` and another sequential
# collection. (See `Array#<=>` for details.)
#
# Additionally, since `SortedSet`s are ordered, they also support indexed retrieval
# of items using {#at} or {#[]}. Like {Vector},
# negative indices count back from the end of the `SortedSet`.
#
# Getting the {#max} or {#min} item from a `SortedSet`, as defined by its comparator,
# is a constant time operation.
#
class SortedSet
include Immutable
include Enumerable
class << self
# Create a new `SortedSet` populated with the given items. This method does not
# accept a comparator block.
#
# @return [SortedSet]
def [](*items)
new(items)
end
# Return an empty `SortedSet`. If used on a subclass, returns an empty instance
# of that class.
#
# @return [SortedSet]
def empty
@empty ||= self.alloc(PlainAVLNode::EmptyNode)
end
# "Raw" allocation of a new `SortedSet`. Used internally to create a new
# instance quickly after obtaining a modified binary tree.
#
# @return [Set]
# @private
def alloc(node)
result = allocate
result.instance_variable_set(:@node, node)
result
end
end
def initialize(items=[], &block)
items = items.to_a
if block
if block.arity == 1 || block.arity == -1
comparator = lambda { |a,b| block.call(a) <=> block.call(b) }
items = items.sort_by(&block)
else
comparator = block
items = items.sort(&block)
end
@node = AVLNode.from_items(items, comparator)
else
@node = PlainAVLNode.from_items(items.sort)
end
end
# Return `true` if this `SortedSet` contains no items.
#
# @return [Boolean]
def empty?
@node.empty?
end
# Return the number of items in this `SortedSet`.
#
# @example
# Hamster::SortedSet["A", "B", "C"].size # => 3
#
# @return [Integer]
def size
@node.size
end
alias :length :size
# Return a new `SortedSet` with `item` added. If `item` is already in the set,
# return `self`.
#
# @example
# Hamster::SortedSet["Dog", "Lion"].add("Elephant")
# # => Hamster::SortedSet["Dog", "Elephant", "Lion"]
#
# @param item [Object] The object to add
# @return [SortedSet]
def add(item)
catch :present do
node = @node.insert(item)
return self.class.alloc(node)
end
self
end
alias :<< :add
# If `item` is not a member of this `SortedSet`, return a new `SortedSet` with
# `item` added. Otherwise, return `false`.
#
# @example
# Hamster::SortedSet["Dog", "Lion"].add?("Elephant")
# # => Hamster::SortedSet["Dog", "Elephant", "Lion"]
# Hamster::SortedSet["Dog", "Lion"].add?("Lion")
# # => false
#
# @param item [Object] The object to add
# @return [SortedSet, false]
def add?(item)
!include?(item) && add(item)
end
# Return a new `SortedSet` with `item` removed. If `item` is not a member of the set,
# return `self`.
#
# @example
# Hamster::SortedSet["A", "B", "C"].delete("B")
# # => Hamster::SortedSet["A", "C"]
#
# @param item [Object] The object to remove
# @return [SortedSet]
def delete(item)
catch :not_present do
node = @node.delete(item)
if node.empty? && node.natural_order?
return self.class.empty
else
return self.class.alloc(node)
end
end
self
end
# If `item` is a member of this `SortedSet`, return a new `SortedSet` with
# `item` removed. Otherwise, return `false`.
#
# @example
# Hamster::SortedSet["A", "B", "C"].delete?("B")
# # => Hamster::SortedSet["A", "C"]
# Hamster::SortedSet["A", "B", "C"].delete?("Z")
# # => false
#
# @param item [Object] The object to remove
# @return [SortedSet, false]
def delete?(item)
include?(item) && delete(item)
end
# Return a new `SortedSet` with the item at `index` removed. If the given `index`
# does not exist (if it is too high or too low), return `self`.
#
# @example
# Hamster::SortedSet["A", "B", "C", "D"].delete_at(2)
# # => Hamster::SortedSet["A", "B", "D"]
#
# @param index [Integer] The index to remove
# @return [SortedSet]
def delete_at(index)
(item = at(index)) ? delete(item) : self
end
# Retrieve the item at `index`. If there is none (either the provided index
# is too high or too low), return `nil`.
#
# @example
# s = Hamster::SortedSet["A", "B", "C", "D", "E", "F"]
# s.at(2) # => "C"
# s.at(-2) # => "E"
# s.at(6) # => nil
#
# @param index [Integer] The index to retrieve
# @return [Object]
def at(index)
index += @node.size if index < 0
return nil if index >= @node.size || index < 0
@node.at(index)
end
# Retrieve the value at `index` with optional default.
#
# @overload fetch(index)
# Retrieve the value at the given index, or raise an `IndexError` if not
# found.
#
# @param index [Integer] The index to look up
# @raise [IndexError] if index does not exist
# @example
# v = Hamster::SortedSet["A", "B", "C", "D"]
# v.fetch(2) # => "C"
# v.fetch(-1) # => "D"
# v.fetch(4) # => IndexError: index 4 outside of vector bounds
#
# @overload fetch(index) { |index| ... }
# Retrieve the value at the given index, or return the result of yielding
# the block if not found.
#
# @yield Once if the index is not found.
# @yieldparam [Integer] index The index which does not exist
# @yieldreturn [Object] Default value to return
# @param index [Integer] The index to look up
# @example
# v = Hamster::SortedSet["A", "B", "C", "D"]
# v.fetch(2) { |i| i * i } # => "C"
# v.fetch(4) { |i| i * i } # => 16
#
# @overload fetch(index, default)
# Retrieve the value at the given index, or return the provided `default`
# value if not found.
#
# @param index [Integer] The index to look up
# @param default [Object] Object to return if the key is not found
# @example
# v = Hamster::SortedSet["A", "B", "C", "D"]
# v.fetch(2, "Z") # => "C"
# v.fetch(4, "Z") # => "Z"
#
# @return [Object]
def fetch(index, default = (missing_default = true))
if index >= -@node.size && index < @node.size
at(index)
elsif block_given?
yield(index)
elsif !missing_default
default
else
raise IndexError, "index #{index} outside of sorted set bounds"
end
end
# Return specific objects from the `Vector`. All overloads return `nil` if
# the starting index is out of range.
#
# @overload set.slice(index)
# Returns a single object at the given `index`. If `index` is negative,
# count backwards from the end.
#
# @param index [Integer] The index to retrieve. May be negative.
# @return [Object]
# @example
# s = Hamster::SortedSet["A", "B", "C", "D", "E", "F"]
# s[2] # => "C"
# s[-1] # => "F"
# s[6] # => nil
#
# @overload set.slice(index, length)
# Return a subset starting at `index` and continuing for `length`
# elements or until the end of the `SortedSet`, whichever occurs first.
#
# @param start [Integer] The index to start retrieving items from. May be
# negative.
# @param length [Integer] The number of items to retrieve.
# @return [SortedSet]
# @example
# s = Hamster::SortedSet["A", "B", "C", "D", "E", "F"]
# s[2, 3] # => Hamster::SortedSet["C", "D", "E"]
# s[-2, 3] # => Hamster::SortedSet["E", "F"]
# s[20, 1] # => nil
#
# @overload set.slice(index..end)
# Return a subset starting at `index` and continuing to index
# `end` or the end of the `SortedSet`, whichever occurs first.
#
# @param range [Range] The range of indices to retrieve.
# @return [SortedSet]
# @example
# s = Hamster::SortedSet["A", "B", "C", "D", "E", "F"]
# s[2..3] # => Hamster::SortedSet["C", "D"]
# s[-2..100] # => Hamster::SortedSet["E", "F"]
# s[20..21] # => nil
def slice(arg, length = (missing_length = true))
if missing_length
if arg.is_a?(Range)
from, to = arg.begin, arg.end
from += @node.size if from < 0
to += @node.size if to < 0
to += 1 if !arg.exclude_end?
length = to - from
length = 0 if length < 0
subsequence(from, length)
else
at(arg)
end
else
arg += @node.size if arg < 0
subsequence(arg, length)
end
end
alias :[] :slice
# Return a new `SortedSet` with only the elements at the given `indices`.
# If any of the `indices` do not exist, they will be skipped.
#
# @example
# s = Hamster::SortedSet["A", "B", "C", "D", "E", "F"]
# s.values_at(2, 4, 5) # => Hamster::SortedSet["C", "E", "F"]
#
# @param indices [Array] The indices to retrieve and gather into a new `SortedSet`
# @return [SortedSet]
def values_at(*indices)
indices.select! { |i| i >= -@node.size && i < @node.size }
self.class.new(indices.map! { |i| at(i) })
end
# Call the given block once for each item in the set, passing each
# item from first to last successively to the block. If no block is
# provided, returns an `Enumerator`.
#
# @example
# Hamster::SortedSet["A", "B", "C"].each { |e| puts "Element: #{e}" }
#
# Element: A
# Element: B
# Element: C
# # => Hamster::SortedSet["A", "B", "C"]
#
# @yield [item]
# @return [self, Enumerator]
def each(&block)
return @node.to_enum if not block_given?
@node.each(&block)
self
end
# Call the given block once for each item in the set, passing each
# item starting from the last, and counting back to the first, successively to
# the block.
#
# @example
# Hamster::SortedSet["A", "B", "C"].reverse_each { |e| puts "Element: #{e}" }
#
# Element: C
# Element: B
# Element: A
# # => Hamster::SortedSet["A", "B", "C"]
#
# @return [self]
def reverse_each(&block)
return @node.enum_for(:reverse_each) if not block_given?
@node.reverse_each(&block)
self
end
# Return the "lowest" element in this set, as determined by its sort order.
# Or, if a block is provided, use the block as a comparator to find the
# "lowest" element. (See `Enumerable#min`.)
#
# @example
# Hamster::SortedSet["A", "B", "C"].min # => "A"
#
# @return [Object]
# @yield [a, b] Any number of times with different pairs of elements.
def min
block_given? ? super : @node.min
end
# Return the "lowest" element in this set, as determined by its sort order.
# @return [Object]
def first
@node.min
end
# Return the "highest" element in this set, as determined by its sort order.
# Or, if a block is provided, use the block as a comparator to find the
# "highest" element. (See `Enumerable#max`.)
#
# @example
# Hamster::SortedSet["A", "B", "C"].max # => "C"
#
# @yield [a, b] Any number of times with different pairs of elements.
# @return [Object]
def max
block_given? ? super : @node.max
end
# Return the "highest" element in this set, as determined by its sort order.
# @return [Object]
def last
@node.max
end
# Return a new `SortedSet` containing all elements for which the given block returns
# true.
#
# @example
# Hamster::SortedSet["Bird", "Cow", "Elephant"].select { |e| e.size >= 4 }
# # => Hamster::SortedSet["Bird", "Elephant"]
#
# @return [SortedSet]
# @yield [item] Once for each item.
def select
return enum_for(:select) unless block_given?
items_to_delete = []
each { |item| items_to_delete << item unless yield(item) }
derive_new_sorted_set(@node.bulk_delete(items_to_delete))
end
alias :find_all :select
alias :keep_if :select
# Invoke the given block once for each item in the set, and return a new
# `SortedSet` containing the values returned by the block. If no block is
# given, returns an `Enumerator`.
#
# @example
# Hamster::SortedSet[1, 2, 3].map { |e| -(e * e) }
# # => Hamster::SortedSet[-9, -4, -1]
#
# @return [SortedSet, Enumerator]
# @yield [item] Once for each item.
def map
return enum_for(:map) if not block_given?
return self if empty?
self.class.alloc(@node.from_items(super))
end
alias :collect :map
# Return `true` if the given item is present in this `SortedSet`. More precisely,
# return `true` if an object which compares as "equal" using this set's
# comparator is present.
#
# @example
# Hamster::SortedSet["A", "B", "C"].include?("B") # => true
#
# @param item [Object] The object to check for
# @return [Boolean]
def include?(item)
@node.include?(item)
end
alias :member? :include?
# Return a new `SortedSet` with the same items, but a sort order determined
# by the given block.
#
# @example
# Hamster::SortedSet["Bird", "Cow", "Elephant"].sort { |a, b| a.size <=> b.size }
# # => Hamster::SortedSet["Cow", "Bird", "Elephant"]
# Hamster::SortedSet["Bird", "Cow", "Elephant"].sort_by { |e| e.size }
# # => Hamster::SortedSet["Cow", "Bird", "Elephant"]
#
# @return [SortedSet]
def sort(&block)
if block
self.class.new(self.to_a, &block)
else
self.class.new(self.to_a.sort)
end
end
alias :sort_by :sort
# Find the index of a given object or an element that satisfies the given
# block.
#
# @overload find_index(obj)
# Return the index of the first object in this set which is equal to
# `obj`. Rather than using `#==`, we use `#<=>` (or our comparator block)
# for comparisons. This means we can find the index in `O(log N)` time,
# rather than `O(N)`.
# @param obj [Object] The object to search for
# @example
# s = Hamster::SortedSet[2, 4, 6, 8, 10]
# s.find_index(8) # => 3
# @overload find_index
# Return the index of the first object in this sorted set for which the
# block returns to true. This takes `O(N)` time.
# @yield [element] An element in the sorted set
# @yieldreturn [Boolean] True if this is element matches
# @example
# s = Hamster::SortedSet[2, 4, 6, 8, 10]
# s.find_index { |e| e > 7 } # => 3
#
# @return [Integer] The index of the object, or `nil` if not found.
def find_index(obj = (missing_obj = true), &block)
if !missing_obj
# Enumerable provides a default implementation, but this is more efficient
node = @node
index = node.left.size
while !node.empty?
direction = node.direction(obj)
if direction > 0
node = node.right
index += (node.left.size + 1)
elsif direction < 0
node = node.left
index -= (node.right.size + 1)
else
return index
end
end
nil
else
super(&block)
end
end
alias :index :find_index
# Drop the first `n` elements and return the rest in a new `SortedSet`.
#
# @example
# Hamster::SortedSet["A", "B", "C", "D", "E", "F"].drop(2)
# # => Hamster::SortedSet["C", "D", "E", "F"]
#
# @param n [Integer] The number of elements to remove
# @return [SortedSet]
def drop(n)
derive_new_sorted_set(@node.drop(n))
end
# Return only the first `n` elements in a new `SortedSet`.
#
# @example
# Hamster::SortedSet["A", "B", "C", "D", "E", "F"].take(4)
# # => Hamster::SortedSet["A", "B", "C", "D"]
#
# @param n [Integer] The number of elements to retain
# @return [SortedSet]
def take(n)
derive_new_sorted_set(@node.take(n))
end
# Drop elements up to, but not including, the first element for which the
# block returns `nil` or `false`. Gather the remaining elements into a new
# `SortedSet`. If no block is given, an `Enumerator` is returned instead.
#
# @example
# Hamster::SortedSet[2, 4, 6, 7, 8, 9].drop_while { |e| e.even? }
# # => Hamster::SortedSet[7, 8, 9]
#
# @yield [item]
# @return [SortedSet, Enumerator]
def drop_while
return enum_for(:drop_while) if not block_given?
n = 0
each do |item|
break unless yield item
n += 1
end
drop(n)
end
# Gather elements up to, but not including, the first element for which the
# block returns `nil` or `false`, and return them in a new `SortedSet`. If no block
# is given, an `Enumerator` is returned instead.
#
# @example
# Hamster::SortedSet[2, 4, 6, 7, 8, 9].take_while { |e| e.even? }
# # => Hamster::SortedSet[2, 4, 6]
#
# @return [SortedSet, Enumerator]
# @yield [item]
def take_while
return enum_for(:take_while) if not block_given?
n = 0
each do |item|
break unless yield item
n += 1
end
take(n)
end
# Return a new `SortedSet` which contains all the members of both this set and `other`.
# `other` can be any `Enumerable` object.
#
# @example
# Hamster::SortedSet[1, 2] | Hamster::SortedSet[2, 3]
# # => Hamster::SortedSet[1, 2, 3]
#
# @param other [Enumerable] The collection to merge with
# @return [SortedSet]
def union(other)
self.class.alloc(@node.bulk_insert(other))
end
alias :| :union
alias :+ :union
alias :merge :union
# Return a new `SortedSet` which contains all the items which are members of both
# this set and `other`. `other` can be any `Enumerable` object.
#
# @example
# Hamster::SortedSet[1, 2] & Hamster::SortedSet[2, 3]
# # => Hamster::SortedSet[2]
#
# @param other [Enumerable] The collection to intersect with
# @return [SortedSet]
def intersection(other)
self.class.alloc(@node.keep_only(other))
end
alias :& :intersection
# Return a new `SortedSet` with all the items in `other` removed. `other` can be
# any `Enumerable` object.
#
# @example
# Hamster::SortedSet[1, 2] - Hamster::SortedSet[2, 3]
# # => Hamster::SortedSet[1]
#
# @param other [Enumerable] The collection to subtract from this set
# @return [SortedSet]
def difference(other)
self.class.alloc(@node.bulk_delete(other))
end
alias :subtract :difference
alias :- :difference
# Return a new `SortedSet` with all the items which are members of this
# set or of `other`, but not both. `other` can be any `Enumerable` object.
#
# @example
# Hamster::SortedSet[1, 2] ^ Hamster::SortedSet[2, 3]
# # => Hamster::SortedSet[1, 3]
#
# @param other [Enumerable] The collection to take the exclusive disjunction of
# @return [SortedSet]
def exclusion(other)
((self | other) - (self & other))
end
alias :^ :exclusion
# Return `true` if all items in this set are also in `other`.
#
# @example
# Hamster::SortedSet[2, 3].subset?(Hamster::SortedSet[1, 2, 3]) # => true
#
# @param other [Enumerable]
# @return [Boolean]
def subset?(other)
return false if other.size < size
all? { |item| other.include?(item) }
end
# Return `true` if all items in `other` are also in this set.
#
# @example
# Hamster::SortedSet[1, 2, 3].superset?(Hamster::SortedSet[2, 3]) # => true
#
# @param other [Enumerable]
# @return [Boolean]
def superset?(other)
other.subset?(self)
end
# Returns `true` if `other` contains all the items in this set, plus at least
# one item which is not in this set.
#
# @example
# Hamster::SortedSet[2, 3].proper_subset?(Hamster::SortedSet[1, 2, 3]) # => true
# Hamster::SortedSet[1, 2, 3].proper_subset?(Hamster::SortedSet[1, 2, 3]) # => false
#
# @param other [Enumerable]
# @return [Boolean]
def proper_subset?(other)
return false if other.size <= size
all? { |item| other.include?(item) }
end
# Returns `true` if this set contains all the items in `other`, plus at least
# one item which is not in `other`.
#
# @example
# Hamster::SortedSet[1, 2, 3].proper_superset?(Hamster::SortedSet[2, 3]) # => true
# Hamster::SortedSet[1, 2, 3].proper_superset?(Hamster::SortedSet[1, 2, 3]) # => false
#
# @param other [Enumerable]
# @return [Boolean]
def proper_superset?(other)
other.proper_subset?(self)
end
# Return `true` if this set and `other` do not share any items.
#
# @example
# Hamster::SortedSet[1, 2].disjoint?(Hamster::SortedSet[3, 4]) # => true
#
# @param other [Enumerable]
# @return [Boolean]
def disjoint?(other)
if size < other.size
each { |item| return false if other.include?(item) }
else
other.each { |item| return false if include?(item) }
end
true
end
# Return `true` if this set and `other` have at least one item in common.
#
# @example
# Hamster::SortedSet[1, 2].intersect?(Hamster::SortedSet[2, 3]) # => true
#
# @param other [Enumerable]
# @return [Boolean]
def intersect?(other)
!disjoint?(other)
end
alias :group :group_by
alias :classify :group_by
# Select elements greater than a value.
#
# @overload above(item)
# Return a new `SortedSet` containing all items greater than `item`.
# @return [SortedSet]
# @example
# s = Hamster::SortedSet[2, 4, 6, 8, 10]
# s.above(6)
# # => Hamster::SortedSet[8, 10]
#
# @overload above(item)
# @yield [item] Once for each item greater than `item`, in order from
# lowest to highest.
# @return [nil]
# @example
# s = Hamster::SortedSet[2, 4, 6, 8, 10]
# s.above(6) { |e| puts "Element: #{e}" }
#
# Element: 8
# Element: 10
# # => nil
#
# @param item [Object]
def above(item, &block)
if block_given?
@node.each_greater(item, false, &block)
else
self.class.alloc(@node.suffix(item, false))
end
end
# Select elements less than a value.
#
# @overload below(item)
# Return a new `SortedSet` containing all items less than `item`.
# @return [SortedSet]
# @example
# s = Hamster::SortedSet[2, 4, 6, 8, 10]
# s.below(6)
# # => Hamster::SortedSet[2, 4]
#
# @overload below(item)
# @yield [item] Once for each item less than `item`, in order from lowest
# to highest.
# @return [nil]
# @example
# s = Hamster::SortedSet[2, 4, 6, 8, 10]
# s.below(6) { |e| puts "Element: #{e}" }
#
# Element: 2
# Element: 4
# # => nil
#
# @param item [Object]
def below(item, &block)
if block_given?
@node.each_less(item, false, &block)
else
self.class.alloc(@node.prefix(item, false))
end
end
# Select elements greater than or equal to a value.
#
# @overload from(item)
# Return a new `SortedSet` containing all items greater than or equal `item`.
# @return [SortedSet]
# @example
# s = Hamster::SortedSet[2, 4, 6, 8, 10]
# s.from(6)
# # => Hamster::SortedSet[6, 8, 10]
#
# @overload from(item)
# @yield [item] Once for each item greater than or equal to `item`, in
# order from lowest to highest.
# @return [nil]
# @example
# s = Hamster::SortedSet[2, 4, 6, 8, 10]
# s.from(6) { |e| puts "Element: #{e}" }
#
# Element: 6
# Element: 8
# Element: 10
# # => nil
#
# @param item [Object]
def from(item, &block)
if block_given?
@node.each_greater(item, true, &block)
else
self.class.alloc(@node.suffix(item, true))
end
end
# Select elements less than or equal to a value.
#
# @overload up_to(item)
# Return a new `SortedSet` containing all items less than or equal to
# `item`.
#
# @return [SortedSet]
# @example
# s = Hamster::SortedSet[2, 4, 6, 8, 10]
# s.upto(6)
# # => Hamster::SortedSet[2, 4, 6]
#
# @overload up_to(item)
# @yield [item] Once for each item less than or equal to `item`, in order
# from lowest to highest.
# @return [nil]
# @example
# s = Hamster::SortedSet[2, 4, 6, 8, 10]
# s.up_to(6) { |e| puts "Element: #{e}" }
#
# Element: 2
# Element: 4
# Element: 6
# # => nil
#
# @param item [Object]
def up_to(item, &block)
if block_given?
@node.each_less(item, true, &block)
else
self.class.alloc(@node.prefix(item, true))
end
end
# Select elements between two values.
#
# @overload between(from, to)
# Return a new `SortedSet` containing all items less than or equal to
# `to` and greater than or equal to `from`.
#
# @return [SortedSet]
# @example
# s = Hamster::SortedSet[2, 4, 6, 8, 10]
# s.between(5, 8)
# # => Hamster::SortedSet[6, 8]
#
# @overload between(item)
# @yield [item] Once for each item less than or equal to `to` and greater
# than or equal to `from`, in order from lowest to highest.
# @return [nil]
# @example
# s = Hamster::SortedSet[2, 4, 6, 8, 10]
# s.between(5, 8) { |e| puts "Element: #{e}" }
#
# Element: 6
# Element: 8
# # => nil
#
# @param from [Object]
# @param to [Object]
def between(from, to, &block)
if block_given?
@node.each_between(from, to, &block)
else
self.class.alloc(@node.between(from, to))
end
end
# Return a randomly chosen item from this set. If the set is empty, return `nil`.
#
# @example
# Hamster::SortedSet[1, 2, 3, 4, 5].sample # => 2
#
# @return [Object]
def sample
@node.at(rand(@node.size))
end
# Return an empty `SortedSet` instance, of the same class as this one. Useful if you
# have multiple subclasses of `SortedSet` and want to treat them polymorphically.
#
# @return [SortedSet]
def clear
if @node.natural_order?
self.class.empty
else
self.class.alloc(@node.clear)
end
end
# Return true if `other` has the same type and contents as this `SortedSet`.
#
# @param other [Object] The object to compare with
# @return [Boolean]
def eql?(other)
return true if other.equal?(self)
return false if not instance_of?(other.class)
return false if size != other.size
a, b = self.to_enum, other.to_enum
while true
return false if !a.next.eql?(b.next)
end
rescue StopIteration
true
end
# See `Object#hash`.
# @return [Integer]
def hash
reduce(0) { |hash, item| (hash << 5) - hash + item.hash }
end
# @return [::Array]
# @private
def marshal_dump
if @node.natural_order?
to_a
else
raise TypeError, "can't dump SortedSet with custom sort order"
end
end
# @private
def marshal_load(array)
initialize(array)
end
private
def subsequence(from, length)
return nil if from > @node.size || from < 0 || length < 0
length = @node.size - from if @node.size < from + length
if length == 0
if @node.natural_order?
return self.class.empty
else
return self.class.alloc(@node.clear)
end
end
self.class.alloc(@node.slice(from, length))
end
# Return a new `SortedSet` which is derived from this one, using a modified
# {AVLNode}. The new `SortedSet` will retain the existing comparator, if
# there is one.
def derive_new_sorted_set(node)
if node.equal?(@node)
self
elsif node.empty?
clear
else
self.class.alloc(node)
end
end
# @private
class AVLNode
def self.from_items(items, comparator, from = 0, to = items.size-1) # items must be sorted
size = to - from + 1
if size >= 3
middle = (to + from) / 2
AVLNode.new(items[middle], comparator, AVLNode.from_items(items, comparator, from, middle-1), AVLNode.from_items(items, comparator, middle+1, to))
elsif size == 2
empty = AVLNode::Empty.new(comparator)
AVLNode.new(items[from], comparator, empty, AVLNode.new(items[from+1], comparator, empty, empty))
elsif size == 1
empty = AVLNode::Empty.new(comparator)
AVLNode.new(items[from], comparator, empty, empty)
elsif size == 0
AVLNode::Empty.new(comparator)
end
end
def initialize(item, comparator, left, right)
@item, @comparator, @left, @right = item, comparator, left, right
@height = ((right.height > left.height) ? right.height : left.height) + 1
@size = right.size + left.size + 1
end
attr_reader :item, :left, :right, :height, :size
def from_items(items)
AVLNode.from_items(items.sort(&@comparator), @comparator)
end
def natural_order?
false
end
def empty?
false
end
def clear
AVLNode::Empty.new(@comparator)
end
def derive(item, left, right)
AVLNode.new(item, @comparator, left, right)
end
def insert(item)
dir = direction(item)
if dir == 0
throw :present
elsif dir > 0
rebalance_right(@left, @right.insert(item))
else
rebalance_left(@left.insert(item), @right)
end
end
def bulk_insert(items)
return self if items.empty?
if items.size == 1
catch :present do
return insert(items.first)
end
return self
end
left, right = partition(items)
if right.size > left.size
rebalance_right(@left.bulk_insert(left), @right.bulk_insert(right))
else
rebalance_left(@left.bulk_insert(left), @right.bulk_insert(right))
end
end
def delete(item)
dir = direction(item)
if dir == 0
if @right.empty?
return @left # replace this node with its only child
elsif @left.empty?
return @right # likewise
end
if balance > 0
# tree is leaning to the left. replace with highest node on that side
replace_with = @left.max
derive(replace_with, @left.delete(replace_with), @right)
else
# tree is leaning to the right. replace with lowest node on that side
replace_with = @right.min
derive(replace_with, @left, @right.delete(replace_with))
end
elsif dir > 0
rebalance_left(@left, @right.delete(item))
else
rebalance_right(@left.delete(item), @right)
end
end
def bulk_delete(items)
return self if items.empty?
if items.size == 1
catch :not_present do
return delete(items.first)
end
return self
end
left, right, keep_item = [], [], true
items.each do |item|
dir = direction(item)
if dir > 0
right << item
elsif dir < 0
left << item
else
keep_item = false
end
end
left = @left.bulk_delete(left)
right = @right.bulk_delete(right)
finish_removal(keep_item, left, right)
end
def keep_only(items)
return clear if items.empty?
left, right, keep_item = [], [], false
items.each do |item|
dir = direction(item)
if dir > 0
right << item
elsif dir < 0
left << item
else
keep_item = true
end
end
left = @left.keep_only(left)
right = @right.keep_only(right)
finish_removal(keep_item, left, right)
end
def finish_removal(keep_item, left, right)
# deletion of items may have occurred on left and right sides
# now we may also need to delete the current item
if keep_item
rebalance(left, right) # no need to delete the current item
elsif left.empty?
right
elsif right.empty?
left
elsif left.height > right.height
replace_with = left.max
derive(replace_with, left.delete(replace_with), right)
else
replace_with = right.min
derive(replace_with, left, right.delete(replace_with))
end
end
def prefix(item, inclusive)
dir = direction(item)
if dir > 0 || (inclusive && dir == 0)
rebalance_left(@left, @right.prefix(item, inclusive))
else
@left.prefix(item, inclusive)
end
end
def suffix(item, inclusive)
dir = direction(item)
if dir < 0 || (inclusive && dir == 0)
rebalance_right(@left.suffix(item, inclusive), @right)
else
@right.suffix(item, inclusive)
end
end
def between(from, to)
if direction(from) > 0 # all on the right
@right.between(from, to)
elsif direction(to) < 0 # all on the left
@left.between(from, to)
else
left = @left.suffix(from, true)
right = @right.prefix(to, true)
rebalance(left, right)
end
end
def each_less(item, inclusive, &block)
dir = direction(item)
if dir > 0 || (inclusive && dir == 0)
@left.each(&block)
yield @item
@right.each_less(item, inclusive, &block)
else
@left.each_less(item, inclusive, &block)
end
end
def each_greater(item, inclusive, &block)
dir = direction(item)
if dir < 0 || (inclusive && dir == 0)
@left.each_greater(item, inclusive, &block)
yield @item
@right.each(&block)
else
@right.each_greater(item, inclusive, &block)
end
end
def each_between(from, to, &block)
if direction(from) > 0 # all on the right
@right.each_between(from, to, &block)
elsif direction(to) < 0 # all on the left
@left.each_between(from, to, &block)
else
@left.each_greater(from, true, &block)
yield @item
@right.each_less(to, true, &block)
end
end
def each(&block)
@left.each(&block)
yield @item
@right.each(&block)
end
def reverse_each(&block)
@right.reverse_each(&block)
yield @item
@left.reverse_each(&block)
end
def drop(n)
if n >= @size
clear
elsif n <= 0
self
elsif @left.size >= n
rebalance_right(@left.drop(n), @right)
elsif @left.size + 1 == n
@right
else
@right.drop(n - @left.size - 1)
end
end
def take(n)
if n >= @size
self
elsif n <= 0
clear
elsif @left.size >= n
@left.take(n)
else
rebalance_left(@left, @right.take(n - @left.size - 1))
end
end
def include?(item)
dir = direction(item)
if dir == 0
true
elsif dir > 0
@right.include?(item)
else
@left.include?(item)
end
end
def at(index)
if index < @left.size
@left.at(index)
elsif index > @left.size
@right.at(index - @left.size - 1)
else
@item
end
end
def max
@right.empty? ? @item : @right.max
end
def min
@left.empty? ? @item : @left.min
end
def balance
@left.height - @right.height
end
def slice(from, length)
if length <= 0
clear
elsif from + length <= @left.size
@left.slice(from, length)
elsif from > @left.size
@right.slice(from - @left.size - 1, length)
else
left = @left.slice(from, @left.size - from)
right = @right.slice(0, from + length - @left.size - 1)
rebalance(left, right)
end
end
def partition(items)
left, right = [], []
items.each do |item|
dir = direction(item)
if dir > 0
right << item
elsif dir < 0
left << item
end
end
[left, right]
end
def rebalance(left, right)
if left.height > right.height
rebalance_left(left, right)
else
rebalance_right(left, right)
end
end
def rebalance_left(left, right)
# the tree might be unbalanced to the left (paths on the left too long)
balance = left.height - right.height
if balance >= 2
if left.balance > 0
# single right rotation
derive(left.item, left.left, derive(@item, left.right, right))
else
# left rotation, then right
derive(left.right.item, derive(left.item, left.left, left.right.left), derive(@item, left.right.right, right))
end
else
derive(@item, left, right)
end
end
def rebalance_right(left, right)
# the tree might be unbalanced to the right (paths on the right too long)
balance = left.height - right.height
if balance <= -2
if right.balance > 0
# right rotation, then left
derive(right.left.item, derive(@item, left, right.left.left), derive(right.item, right.left.right, right.right))
else
# single left rotation
derive(right.item, derive(@item, left, right.left), right.right)
end
else
derive(@item, left, right)
end
end
def direction(item)
@comparator.call(item, @item)
end
# @private
class Empty
def initialize(comparator); @comparator = comparator; end
def natural_order?; false; end
def left; self; end
def right; self; end
def height; 0; end
def size; 0; end
def min; nil; end
def max; nil; end
def each; end
def reverse_each; end
def at(index); nil; end
def insert(item)
AVLNode.new(item, @comparator, self, self)
end
def bulk_insert(items)
items = items.to_a if !items.is_a?(Array)
AVLNode.from_items(items.sort(&@comparator), @comparator)
end
def bulk_delete(items); self; end
def keep_only(items); self; end
def delete(item); throw :not_present; end
def include?(item); false; end
def prefix(item, inclusive); self; end
def suffix(item, inclusive); self; end
def between(from, to); self; end
def each_greater(item, inclusive); end
def each_less(item, inclusive); end
def each_between(item, inclusive); end
def drop(n); self; end
def take(n); self; end
def empty?; true; end
def slice(from, length); self; end
end
end
# @private
# AVL node which does not use a comparator function; it keeps items sorted
# in their natural order
class PlainAVLNode < AVLNode
def self.from_items(items, from = 0, to = items.size-1) # items must be sorted
size = to - from + 1
if size >= 3
middle = (to + from) / 2
PlainAVLNode.new(items[middle], PlainAVLNode.from_items(items, from, middle-1), PlainAVLNode.from_items(items, middle+1, to))
elsif size == 2
PlainAVLNode.new(items[from], PlainAVLNode::EmptyNode, PlainAVLNode.new(items[from+1], PlainAVLNode::EmptyNode, PlainAVLNode::EmptyNode))
elsif size == 1
PlainAVLNode.new(items[from], PlainAVLNode::EmptyNode, PlainAVLNode::EmptyNode)
elsif size == 0
PlainAVLNode::EmptyNode
end
end
def initialize(item, left, right)
@item, @left, @right = item, left, right
@height = ((right.height > left.height) ? right.height : left.height) + 1
@size = right.size + left.size + 1
end
attr_reader :item, :left, :right, :height, :size
def from_items(items)
PlainAVLNode.from_items(items.sort)
end
def natural_order?
true
end
def clear
PlainAVLNode::EmptyNode
end
def derive(item, left, right)
PlainAVLNode.new(item, left, right)
end
def direction(item)
item <=> @item
end
# @private
class Empty < AVLNode::Empty
def initialize; end
def natural_order?; true; end
def insert(item)
PlainAVLNode.new(item, self, self)
end
def bulk_insert(items)
items = items.to_a if !items.is_a?(Array)
PlainAVLNode.from_items(items.sort)
end
end
EmptyNode = PlainAVLNode::Empty.new
end
end
# The canonical empty `SortedSet`. Returned by `SortedSet[]`
# when invoked with no arguments; also returned by `SortedSet.empty`. Prefer using
# this one rather than creating many empty sorted sets using `SortedSet.new`.
#
# @private
EmptySortedSet = Hamster::SortedSet.empty
end
|