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 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
|
package state
import (
"bytes"
"fmt"
)
// trackGaps is an optional parameter.
//
// If trackGaps is 1, the Set will track maximum gap size recursively,
// enabling the GapIterator.{Prev,Next}LargeEnoughGap functions. In this
// case, Key must be an unsigned integer.
//
// trackGaps must be 0 or 1.
const addrtrackGaps = 0
var _ = uint8(addrtrackGaps << 7) // Will fail if not zero or one.
// dynamicGap is a type that disappears if trackGaps is 0.
type addrdynamicGap [addrtrackGaps]uintptr
// Get returns the value of the gap.
//
// Precondition: trackGaps must be non-zero.
func (d *addrdynamicGap) Get() uintptr {
return d[:][0]
}
// Set sets the value of the gap.
//
// Precondition: trackGaps must be non-zero.
func (d *addrdynamicGap) Set(v uintptr) {
d[:][0] = v
}
const (
// minDegree is the minimum degree of an internal node in a Set B-tree.
//
// - Any non-root node has at least minDegree-1 segments.
//
// - Any non-root internal (non-leaf) node has at least minDegree children.
//
// - The root node may have fewer than minDegree-1 segments, but it may
// only have 0 segments if the tree is empty.
//
// Our implementation requires minDegree >= 3. Higher values of minDegree
// usually improve performance, but increase memory usage for small sets.
addrminDegree = 10
addrmaxDegree = 2 * addrminDegree
)
// A Set is a mapping of segments with non-overlapping Range keys. The zero
// value for a Set is an empty set. Set values are not safely movable nor
// copyable. Set is thread-compatible.
//
// +stateify savable
type addrSet struct {
root addrnode `state:".(*addrSegmentDataSlices)"`
}
// IsEmpty returns true if the set contains no segments.
func (s *addrSet) IsEmpty() bool {
return s.root.nrSegments == 0
}
// IsEmptyRange returns true iff no segments in the set overlap the given
// range. This is semantically equivalent to s.SpanRange(r) == 0, but may be
// more efficient.
func (s *addrSet) IsEmptyRange(r addrRange) bool {
switch {
case r.Length() < 0:
panic(fmt.Sprintf("invalid range %v", r))
case r.Length() == 0:
return true
}
_, gap := s.Find(r.Start)
if !gap.Ok() {
return false
}
return r.End <= gap.End()
}
// Span returns the total size of all segments in the set.
func (s *addrSet) Span() uintptr {
var sz uintptr
for seg := s.FirstSegment(); seg.Ok(); seg = seg.NextSegment() {
sz += seg.Range().Length()
}
return sz
}
// SpanRange returns the total size of the intersection of segments in the set
// with the given range.
func (s *addrSet) SpanRange(r addrRange) uintptr {
switch {
case r.Length() < 0:
panic(fmt.Sprintf("invalid range %v", r))
case r.Length() == 0:
return 0
}
var sz uintptr
for seg := s.LowerBoundSegment(r.Start); seg.Ok() && seg.Start() < r.End; seg = seg.NextSegment() {
sz += seg.Range().Intersect(r).Length()
}
return sz
}
// FirstSegment returns the first segment in the set. If the set is empty,
// FirstSegment returns a terminal iterator.
func (s *addrSet) FirstSegment() addrIterator {
if s.root.nrSegments == 0 {
return addrIterator{}
}
return s.root.firstSegment()
}
// LastSegment returns the last segment in the set. If the set is empty,
// LastSegment returns a terminal iterator.
func (s *addrSet) LastSegment() addrIterator {
if s.root.nrSegments == 0 {
return addrIterator{}
}
return s.root.lastSegment()
}
// FirstGap returns the first gap in the set.
func (s *addrSet) FirstGap() addrGapIterator {
n := &s.root
for n.hasChildren {
n = n.children[0]
}
return addrGapIterator{n, 0}
}
// LastGap returns the last gap in the set.
func (s *addrSet) LastGap() addrGapIterator {
n := &s.root
for n.hasChildren {
n = n.children[n.nrSegments]
}
return addrGapIterator{n, n.nrSegments}
}
// Find returns the segment or gap whose range contains the given key. If a
// segment is found, the returned Iterator is non-terminal and the
// returned GapIterator is terminal. Otherwise, the returned Iterator is
// terminal and the returned GapIterator is non-terminal.
func (s *addrSet) Find(key uintptr) (addrIterator, addrGapIterator) {
n := &s.root
for {
lower := 0
upper := n.nrSegments
for lower < upper {
i := lower + (upper-lower)/2
if r := n.keys[i]; key < r.End {
if key >= r.Start {
return addrIterator{n, i}, addrGapIterator{}
}
upper = i
} else {
lower = i + 1
}
}
i := lower
if !n.hasChildren {
return addrIterator{}, addrGapIterator{n, i}
}
n = n.children[i]
}
}
// FindSegment returns the segment whose range contains the given key. If no
// such segment exists, FindSegment returns a terminal iterator.
func (s *addrSet) FindSegment(key uintptr) addrIterator {
seg, _ := s.Find(key)
return seg
}
// LowerBoundSegment returns the segment with the lowest range that contains a
// key greater than or equal to min. If no such segment exists,
// LowerBoundSegment returns a terminal iterator.
func (s *addrSet) LowerBoundSegment(min uintptr) addrIterator {
seg, gap := s.Find(min)
if seg.Ok() {
return seg
}
return gap.NextSegment()
}
// UpperBoundSegment returns the segment with the highest range that contains a
// key less than or equal to max. If no such segment exists, UpperBoundSegment
// returns a terminal iterator.
func (s *addrSet) UpperBoundSegment(max uintptr) addrIterator {
seg, gap := s.Find(max)
if seg.Ok() {
return seg
}
return gap.PrevSegment()
}
// FindGap returns the gap containing the given key. If no such gap exists
// (i.e. the set contains a segment containing that key), FindGap returns a
// terminal iterator.
func (s *addrSet) FindGap(key uintptr) addrGapIterator {
_, gap := s.Find(key)
return gap
}
// LowerBoundGap returns the gap with the lowest range that is greater than or
// equal to min.
func (s *addrSet) LowerBoundGap(min uintptr) addrGapIterator {
seg, gap := s.Find(min)
if gap.Ok() {
return gap
}
return seg.NextGap()
}
// UpperBoundGap returns the gap with the highest range that is less than or
// equal to max.
func (s *addrSet) UpperBoundGap(max uintptr) addrGapIterator {
seg, gap := s.Find(max)
if gap.Ok() {
return gap
}
return seg.PrevGap()
}
// Add inserts the given segment into the set and returns true. If the new
// segment can be merged with adjacent segments, Add will do so. If the new
// segment would overlap an existing segment, Add returns false. If Add
// succeeds, all existing iterators are invalidated.
func (s *addrSet) Add(r addrRange, val *objectEncodeState) bool {
if r.Length() <= 0 {
panic(fmt.Sprintf("invalid segment range %v", r))
}
gap := s.FindGap(r.Start)
if !gap.Ok() {
return false
}
if r.End > gap.End() {
return false
}
s.Insert(gap, r, val)
return true
}
// AddWithoutMerging inserts the given segment into the set and returns true.
// If it would overlap an existing segment, AddWithoutMerging does nothing and
// returns false. If AddWithoutMerging succeeds, all existing iterators are
// invalidated.
func (s *addrSet) AddWithoutMerging(r addrRange, val *objectEncodeState) bool {
if r.Length() <= 0 {
panic(fmt.Sprintf("invalid segment range %v", r))
}
gap := s.FindGap(r.Start)
if !gap.Ok() {
return false
}
if r.End > gap.End() {
return false
}
s.InsertWithoutMergingUnchecked(gap, r, val)
return true
}
// Insert inserts the given segment into the given gap. If the new segment can
// be merged with adjacent segments, Insert will do so. Insert returns an
// iterator to the segment containing the inserted value (which may have been
// merged with other values). All existing iterators (including gap, but not
// including the returned iterator) are invalidated.
//
// If the gap cannot accommodate the segment, or if r is invalid, Insert panics.
//
// Insert is semantically equivalent to a InsertWithoutMerging followed by a
// Merge, but may be more efficient. Note that there is no unchecked variant of
// Insert since Insert must retrieve and inspect gap's predecessor and
// successor segments regardless.
func (s *addrSet) Insert(gap addrGapIterator, r addrRange, val *objectEncodeState) addrIterator {
if r.Length() <= 0 {
panic(fmt.Sprintf("invalid segment range %v", r))
}
prev, next := gap.PrevSegment(), gap.NextSegment()
if prev.Ok() && prev.End() > r.Start {
panic(fmt.Sprintf("new segment %v overlaps predecessor %v", r, prev.Range()))
}
if next.Ok() && next.Start() < r.End {
panic(fmt.Sprintf("new segment %v overlaps successor %v", r, next.Range()))
}
if prev.Ok() && prev.End() == r.Start {
if mval, ok := (addrSetFunctions{}).Merge(prev.Range(), prev.Value(), r, val); ok {
shrinkMaxGap := addrtrackGaps != 0 && gap.Range().Length() == gap.node.maxGap.Get()
prev.SetEndUnchecked(r.End)
prev.SetValue(mval)
if shrinkMaxGap {
gap.node.updateMaxGapLeaf()
}
if next.Ok() && next.Start() == r.End {
val = mval
if mval, ok := (addrSetFunctions{}).Merge(prev.Range(), val, next.Range(), next.Value()); ok {
prev.SetEndUnchecked(next.End())
prev.SetValue(mval)
return s.Remove(next).PrevSegment()
}
}
return prev
}
}
if next.Ok() && next.Start() == r.End {
if mval, ok := (addrSetFunctions{}).Merge(r, val, next.Range(), next.Value()); ok {
shrinkMaxGap := addrtrackGaps != 0 && gap.Range().Length() == gap.node.maxGap.Get()
next.SetStartUnchecked(r.Start)
next.SetValue(mval)
if shrinkMaxGap {
gap.node.updateMaxGapLeaf()
}
return next
}
}
return s.InsertWithoutMergingUnchecked(gap, r, val)
}
// InsertWithoutMerging inserts the given segment into the given gap and
// returns an iterator to the inserted segment. All existing iterators
// (including gap, but not including the returned iterator) are invalidated.
//
// If the gap cannot accommodate the segment, or if r is invalid,
// InsertWithoutMerging panics.
func (s *addrSet) InsertWithoutMerging(gap addrGapIterator, r addrRange, val *objectEncodeState) addrIterator {
if r.Length() <= 0 {
panic(fmt.Sprintf("invalid segment range %v", r))
}
if gr := gap.Range(); !gr.IsSupersetOf(r) {
panic(fmt.Sprintf("cannot insert segment range %v into gap range %v", r, gr))
}
return s.InsertWithoutMergingUnchecked(gap, r, val)
}
// InsertWithoutMergingUnchecked inserts the given segment into the given gap
// and returns an iterator to the inserted segment. All existing iterators
// (including gap, but not including the returned iterator) are invalidated.
//
// Preconditions:
// * r.Start >= gap.Start().
// * r.End <= gap.End().
func (s *addrSet) InsertWithoutMergingUnchecked(gap addrGapIterator, r addrRange, val *objectEncodeState) addrIterator {
gap = gap.node.rebalanceBeforeInsert(gap)
splitMaxGap := addrtrackGaps != 0 && (gap.node.nrSegments == 0 || gap.Range().Length() == gap.node.maxGap.Get())
copy(gap.node.keys[gap.index+1:], gap.node.keys[gap.index:gap.node.nrSegments])
copy(gap.node.values[gap.index+1:], gap.node.values[gap.index:gap.node.nrSegments])
gap.node.keys[gap.index] = r
gap.node.values[gap.index] = val
gap.node.nrSegments++
if splitMaxGap {
gap.node.updateMaxGapLeaf()
}
return addrIterator{gap.node, gap.index}
}
// Remove removes the given segment and returns an iterator to the vacated gap.
// All existing iterators (including seg, but not including the returned
// iterator) are invalidated.
func (s *addrSet) Remove(seg addrIterator) addrGapIterator {
if seg.node.hasChildren {
victim := seg.PrevSegment()
seg.SetRangeUnchecked(victim.Range())
seg.SetValue(victim.Value())
nextAdjacentNode := seg.NextSegment().node
if addrtrackGaps != 0 {
nextAdjacentNode.updateMaxGapLeaf()
}
return s.Remove(victim).NextGap()
}
copy(seg.node.keys[seg.index:], seg.node.keys[seg.index+1:seg.node.nrSegments])
copy(seg.node.values[seg.index:], seg.node.values[seg.index+1:seg.node.nrSegments])
addrSetFunctions{}.ClearValue(&seg.node.values[seg.node.nrSegments-1])
seg.node.nrSegments--
if addrtrackGaps != 0 {
seg.node.updateMaxGapLeaf()
}
return seg.node.rebalanceAfterRemove(addrGapIterator{seg.node, seg.index})
}
// RemoveAll removes all segments from the set. All existing iterators are
// invalidated.
func (s *addrSet) RemoveAll() {
s.root = addrnode{}
}
// RemoveRange removes all segments in the given range. An iterator to the
// newly formed gap is returned, and all existing iterators are invalidated.
func (s *addrSet) RemoveRange(r addrRange) addrGapIterator {
seg, gap := s.Find(r.Start)
if seg.Ok() {
seg = s.Isolate(seg, r)
gap = s.Remove(seg)
}
for seg = gap.NextSegment(); seg.Ok() && seg.Start() < r.End; seg = gap.NextSegment() {
seg = s.Isolate(seg, r)
gap = s.Remove(seg)
}
return gap
}
// Merge attempts to merge two neighboring segments. If successful, Merge
// returns an iterator to the merged segment, and all existing iterators are
// invalidated. Otherwise, Merge returns a terminal iterator.
//
// If first is not the predecessor of second, Merge panics.
func (s *addrSet) Merge(first, second addrIterator) addrIterator {
if first.NextSegment() != second {
panic(fmt.Sprintf("attempt to merge non-neighboring segments %v, %v", first.Range(), second.Range()))
}
return s.MergeUnchecked(first, second)
}
// MergeUnchecked attempts to merge two neighboring segments. If successful,
// MergeUnchecked returns an iterator to the merged segment, and all existing
// iterators are invalidated. Otherwise, MergeUnchecked returns a terminal
// iterator.
//
// Precondition: first is the predecessor of second: first.NextSegment() ==
// second, first == second.PrevSegment().
func (s *addrSet) MergeUnchecked(first, second addrIterator) addrIterator {
if first.End() == second.Start() {
if mval, ok := (addrSetFunctions{}).Merge(first.Range(), first.Value(), second.Range(), second.Value()); ok {
first.SetEndUnchecked(second.End())
first.SetValue(mval)
return s.Remove(second).PrevSegment()
}
}
return addrIterator{}
}
// MergeAll attempts to merge all adjacent segments in the set. All existing
// iterators are invalidated.
func (s *addrSet) MergeAll() {
seg := s.FirstSegment()
if !seg.Ok() {
return
}
next := seg.NextSegment()
for next.Ok() {
if mseg := s.MergeUnchecked(seg, next); mseg.Ok() {
seg, next = mseg, mseg.NextSegment()
} else {
seg, next = next, next.NextSegment()
}
}
}
// MergeRange attempts to merge all adjacent segments that contain a key in the
// specific range. All existing iterators are invalidated.
func (s *addrSet) MergeRange(r addrRange) {
seg := s.LowerBoundSegment(r.Start)
if !seg.Ok() {
return
}
next := seg.NextSegment()
for next.Ok() && next.Range().Start < r.End {
if mseg := s.MergeUnchecked(seg, next); mseg.Ok() {
seg, next = mseg, mseg.NextSegment()
} else {
seg, next = next, next.NextSegment()
}
}
}
// MergeAdjacent attempts to merge the segment containing r.Start with its
// predecessor, and the segment containing r.End-1 with its successor.
func (s *addrSet) MergeAdjacent(r addrRange) {
first := s.FindSegment(r.Start)
if first.Ok() {
if prev := first.PrevSegment(); prev.Ok() {
s.Merge(prev, first)
}
}
last := s.FindSegment(r.End - 1)
if last.Ok() {
if next := last.NextSegment(); next.Ok() {
s.Merge(last, next)
}
}
}
// Split splits the given segment at the given key and returns iterators to the
// two resulting segments. All existing iterators (including seg, but not
// including the returned iterators) are invalidated.
//
// If the segment cannot be split at split (because split is at the start or
// end of the segment's range, so splitting would produce a segment with zero
// length, or because split falls outside the segment's range altogether),
// Split panics.
func (s *addrSet) Split(seg addrIterator, split uintptr) (addrIterator, addrIterator) {
if !seg.Range().CanSplitAt(split) {
panic(fmt.Sprintf("can't split %v at %v", seg.Range(), split))
}
return s.SplitUnchecked(seg, split)
}
// SplitUnchecked splits the given segment at the given key and returns
// iterators to the two resulting segments. All existing iterators (including
// seg, but not including the returned iterators) are invalidated.
//
// Preconditions: seg.Start() < key < seg.End().
func (s *addrSet) SplitUnchecked(seg addrIterator, split uintptr) (addrIterator, addrIterator) {
val1, val2 := (addrSetFunctions{}).Split(seg.Range(), seg.Value(), split)
end2 := seg.End()
seg.SetEndUnchecked(split)
seg.SetValue(val1)
seg2 := s.InsertWithoutMergingUnchecked(seg.NextGap(), addrRange{split, end2}, val2)
return seg2.PrevSegment(), seg2
}
// SplitAt splits the segment straddling split, if one exists. SplitAt returns
// true if a segment was split and false otherwise. If SplitAt splits a
// segment, all existing iterators are invalidated.
func (s *addrSet) SplitAt(split uintptr) bool {
if seg := s.FindSegment(split); seg.Ok() && seg.Range().CanSplitAt(split) {
s.SplitUnchecked(seg, split)
return true
}
return false
}
// Isolate ensures that the given segment's range does not escape r by
// splitting at r.Start and r.End if necessary, and returns an updated iterator
// to the bounded segment. All existing iterators (including seg, but not
// including the returned iterators) are invalidated.
func (s *addrSet) Isolate(seg addrIterator, r addrRange) addrIterator {
if seg.Range().CanSplitAt(r.Start) {
_, seg = s.SplitUnchecked(seg, r.Start)
}
if seg.Range().CanSplitAt(r.End) {
seg, _ = s.SplitUnchecked(seg, r.End)
}
return seg
}
// ApplyContiguous applies a function to a contiguous range of segments,
// splitting if necessary. The function is applied until the first gap is
// encountered, at which point the gap is returned. If the function is applied
// across the entire range, a terminal gap is returned. All existing iterators
// are invalidated.
//
// N.B. The Iterator must not be invalidated by the function.
func (s *addrSet) ApplyContiguous(r addrRange, fn func(seg addrIterator)) addrGapIterator {
seg, gap := s.Find(r.Start)
if !seg.Ok() {
return gap
}
for {
seg = s.Isolate(seg, r)
fn(seg)
if seg.End() >= r.End {
return addrGapIterator{}
}
gap = seg.NextGap()
if !gap.IsEmpty() {
return gap
}
seg = gap.NextSegment()
if !seg.Ok() {
return addrGapIterator{}
}
}
}
// +stateify savable
type addrnode struct {
// An internal binary tree node looks like:
//
// K
// / \
// Cl Cr
//
// where all keys in the subtree rooted by Cl (the left subtree) are less
// than K (the key of the parent node), and all keys in the subtree rooted
// by Cr (the right subtree) are greater than K.
//
// An internal B-tree node's indexes work out to look like:
//
// K0 K1 K2 ... Kn-1
// / \/ \/ \ ... / \
// C0 C1 C2 C3 ... Cn-1 Cn
//
// where n is nrSegments.
nrSegments int
// parent is a pointer to this node's parent. If this node is root, parent
// is nil.
parent *addrnode
// parentIndex is the index of this node in parent.children.
parentIndex int
// Flag for internal nodes that is technically redundant with "children[0]
// != nil", but is stored in the first cache line. "hasChildren" rather
// than "isLeaf" because false must be the correct value for an empty root.
hasChildren bool
// The longest gap within this node. If the node is a leaf, it's simply the
// maximum gap among all the (nrSegments+1) gaps formed by its nrSegments keys
// including the 0th and nrSegments-th gap possibly shared with its upper-level
// nodes; if it's a non-leaf node, it's the max of all children's maxGap.
maxGap addrdynamicGap
// Nodes store keys and values in separate arrays to maximize locality in
// the common case (scanning keys for lookup).
keys [addrmaxDegree - 1]addrRange
values [addrmaxDegree - 1]*objectEncodeState
children [addrmaxDegree]*addrnode
}
// firstSegment returns the first segment in the subtree rooted by n.
//
// Preconditions: n.nrSegments != 0.
func (n *addrnode) firstSegment() addrIterator {
for n.hasChildren {
n = n.children[0]
}
return addrIterator{n, 0}
}
// lastSegment returns the last segment in the subtree rooted by n.
//
// Preconditions: n.nrSegments != 0.
func (n *addrnode) lastSegment() addrIterator {
for n.hasChildren {
n = n.children[n.nrSegments]
}
return addrIterator{n, n.nrSegments - 1}
}
func (n *addrnode) prevSibling() *addrnode {
if n.parent == nil || n.parentIndex == 0 {
return nil
}
return n.parent.children[n.parentIndex-1]
}
func (n *addrnode) nextSibling() *addrnode {
if n.parent == nil || n.parentIndex == n.parent.nrSegments {
return nil
}
return n.parent.children[n.parentIndex+1]
}
// rebalanceBeforeInsert splits n and its ancestors if they are full, as
// required for insertion, and returns an updated iterator to the position
// represented by gap.
func (n *addrnode) rebalanceBeforeInsert(gap addrGapIterator) addrGapIterator {
if n.nrSegments < addrmaxDegree-1 {
return gap
}
if n.parent != nil {
gap = n.parent.rebalanceBeforeInsert(gap)
}
if n.parent == nil {
left := &addrnode{
nrSegments: addrminDegree - 1,
parent: n,
parentIndex: 0,
hasChildren: n.hasChildren,
}
right := &addrnode{
nrSegments: addrminDegree - 1,
parent: n,
parentIndex: 1,
hasChildren: n.hasChildren,
}
copy(left.keys[:addrminDegree-1], n.keys[:addrminDegree-1])
copy(left.values[:addrminDegree-1], n.values[:addrminDegree-1])
copy(right.keys[:addrminDegree-1], n.keys[addrminDegree:])
copy(right.values[:addrminDegree-1], n.values[addrminDegree:])
n.keys[0], n.values[0] = n.keys[addrminDegree-1], n.values[addrminDegree-1]
addrzeroValueSlice(n.values[1:])
if n.hasChildren {
copy(left.children[:addrminDegree], n.children[:addrminDegree])
copy(right.children[:addrminDegree], n.children[addrminDegree:])
addrzeroNodeSlice(n.children[2:])
for i := 0; i < addrminDegree; i++ {
left.children[i].parent = left
left.children[i].parentIndex = i
right.children[i].parent = right
right.children[i].parentIndex = i
}
}
n.nrSegments = 1
n.hasChildren = true
n.children[0] = left
n.children[1] = right
if addrtrackGaps != 0 {
left.updateMaxGapLocal()
right.updateMaxGapLocal()
}
if gap.node != n {
return gap
}
if gap.index < addrminDegree {
return addrGapIterator{left, gap.index}
}
return addrGapIterator{right, gap.index - addrminDegree}
}
copy(n.parent.keys[n.parentIndex+1:], n.parent.keys[n.parentIndex:n.parent.nrSegments])
copy(n.parent.values[n.parentIndex+1:], n.parent.values[n.parentIndex:n.parent.nrSegments])
n.parent.keys[n.parentIndex], n.parent.values[n.parentIndex] = n.keys[addrminDegree-1], n.values[addrminDegree-1]
copy(n.parent.children[n.parentIndex+2:], n.parent.children[n.parentIndex+1:n.parent.nrSegments+1])
for i := n.parentIndex + 2; i < n.parent.nrSegments+2; i++ {
n.parent.children[i].parentIndex = i
}
sibling := &addrnode{
nrSegments: addrminDegree - 1,
parent: n.parent,
parentIndex: n.parentIndex + 1,
hasChildren: n.hasChildren,
}
n.parent.children[n.parentIndex+1] = sibling
n.parent.nrSegments++
copy(sibling.keys[:addrminDegree-1], n.keys[addrminDegree:])
copy(sibling.values[:addrminDegree-1], n.values[addrminDegree:])
addrzeroValueSlice(n.values[addrminDegree-1:])
if n.hasChildren {
copy(sibling.children[:addrminDegree], n.children[addrminDegree:])
addrzeroNodeSlice(n.children[addrminDegree:])
for i := 0; i < addrminDegree; i++ {
sibling.children[i].parent = sibling
sibling.children[i].parentIndex = i
}
}
n.nrSegments = addrminDegree - 1
if addrtrackGaps != 0 {
n.updateMaxGapLocal()
sibling.updateMaxGapLocal()
}
if gap.node != n {
return gap
}
if gap.index < addrminDegree {
return gap
}
return addrGapIterator{sibling, gap.index - addrminDegree}
}
// rebalanceAfterRemove "unsplits" n and its ancestors if they are deficient
// (contain fewer segments than required by B-tree invariants), as required for
// removal, and returns an updated iterator to the position represented by gap.
//
// Precondition: n is the only node in the tree that may currently violate a
// B-tree invariant.
func (n *addrnode) rebalanceAfterRemove(gap addrGapIterator) addrGapIterator {
for {
if n.nrSegments >= addrminDegree-1 {
return gap
}
if n.parent == nil {
return gap
}
if sibling := n.prevSibling(); sibling != nil && sibling.nrSegments >= addrminDegree {
copy(n.keys[1:], n.keys[:n.nrSegments])
copy(n.values[1:], n.values[:n.nrSegments])
n.keys[0] = n.parent.keys[n.parentIndex-1]
n.values[0] = n.parent.values[n.parentIndex-1]
n.parent.keys[n.parentIndex-1] = sibling.keys[sibling.nrSegments-1]
n.parent.values[n.parentIndex-1] = sibling.values[sibling.nrSegments-1]
addrSetFunctions{}.ClearValue(&sibling.values[sibling.nrSegments-1])
if n.hasChildren {
copy(n.children[1:], n.children[:n.nrSegments+1])
n.children[0] = sibling.children[sibling.nrSegments]
sibling.children[sibling.nrSegments] = nil
n.children[0].parent = n
n.children[0].parentIndex = 0
for i := 1; i < n.nrSegments+2; i++ {
n.children[i].parentIndex = i
}
}
n.nrSegments++
sibling.nrSegments--
if addrtrackGaps != 0 {
n.updateMaxGapLocal()
sibling.updateMaxGapLocal()
}
if gap.node == sibling && gap.index == sibling.nrSegments {
return addrGapIterator{n, 0}
}
if gap.node == n {
return addrGapIterator{n, gap.index + 1}
}
return gap
}
if sibling := n.nextSibling(); sibling != nil && sibling.nrSegments >= addrminDegree {
n.keys[n.nrSegments] = n.parent.keys[n.parentIndex]
n.values[n.nrSegments] = n.parent.values[n.parentIndex]
n.parent.keys[n.parentIndex] = sibling.keys[0]
n.parent.values[n.parentIndex] = sibling.values[0]
copy(sibling.keys[:sibling.nrSegments-1], sibling.keys[1:])
copy(sibling.values[:sibling.nrSegments-1], sibling.values[1:])
addrSetFunctions{}.ClearValue(&sibling.values[sibling.nrSegments-1])
if n.hasChildren {
n.children[n.nrSegments+1] = sibling.children[0]
copy(sibling.children[:sibling.nrSegments], sibling.children[1:])
sibling.children[sibling.nrSegments] = nil
n.children[n.nrSegments+1].parent = n
n.children[n.nrSegments+1].parentIndex = n.nrSegments + 1
for i := 0; i < sibling.nrSegments; i++ {
sibling.children[i].parentIndex = i
}
}
n.nrSegments++
sibling.nrSegments--
if addrtrackGaps != 0 {
n.updateMaxGapLocal()
sibling.updateMaxGapLocal()
}
if gap.node == sibling {
if gap.index == 0 {
return addrGapIterator{n, n.nrSegments}
}
return addrGapIterator{sibling, gap.index - 1}
}
return gap
}
p := n.parent
if p.nrSegments == 1 {
left, right := p.children[0], p.children[1]
p.nrSegments = left.nrSegments + right.nrSegments + 1
p.hasChildren = left.hasChildren
p.keys[left.nrSegments] = p.keys[0]
p.values[left.nrSegments] = p.values[0]
copy(p.keys[:left.nrSegments], left.keys[:left.nrSegments])
copy(p.values[:left.nrSegments], left.values[:left.nrSegments])
copy(p.keys[left.nrSegments+1:], right.keys[:right.nrSegments])
copy(p.values[left.nrSegments+1:], right.values[:right.nrSegments])
if left.hasChildren {
copy(p.children[:left.nrSegments+1], left.children[:left.nrSegments+1])
copy(p.children[left.nrSegments+1:], right.children[:right.nrSegments+1])
for i := 0; i < p.nrSegments+1; i++ {
p.children[i].parent = p
p.children[i].parentIndex = i
}
} else {
p.children[0] = nil
p.children[1] = nil
}
if gap.node == left {
return addrGapIterator{p, gap.index}
}
if gap.node == right {
return addrGapIterator{p, gap.index + left.nrSegments + 1}
}
return gap
}
// Merge n and either sibling, along with the segment separating the
// two, into whichever of the two nodes comes first. This is the
// reverse of the non-root splitting case in
// node.rebalanceBeforeInsert.
var left, right *addrnode
if n.parentIndex > 0 {
left = n.prevSibling()
right = n
} else {
left = n
right = n.nextSibling()
}
if gap.node == right {
gap = addrGapIterator{left, gap.index + left.nrSegments + 1}
}
left.keys[left.nrSegments] = p.keys[left.parentIndex]
left.values[left.nrSegments] = p.values[left.parentIndex]
copy(left.keys[left.nrSegments+1:], right.keys[:right.nrSegments])
copy(left.values[left.nrSegments+1:], right.values[:right.nrSegments])
if left.hasChildren {
copy(left.children[left.nrSegments+1:], right.children[:right.nrSegments+1])
for i := left.nrSegments + 1; i < left.nrSegments+right.nrSegments+2; i++ {
left.children[i].parent = left
left.children[i].parentIndex = i
}
}
left.nrSegments += right.nrSegments + 1
copy(p.keys[left.parentIndex:], p.keys[left.parentIndex+1:p.nrSegments])
copy(p.values[left.parentIndex:], p.values[left.parentIndex+1:p.nrSegments])
addrSetFunctions{}.ClearValue(&p.values[p.nrSegments-1])
copy(p.children[left.parentIndex+1:], p.children[left.parentIndex+2:p.nrSegments+1])
for i := 0; i < p.nrSegments; i++ {
p.children[i].parentIndex = i
}
p.children[p.nrSegments] = nil
p.nrSegments--
if addrtrackGaps != 0 {
left.updateMaxGapLocal()
}
n = p
}
}
// updateMaxGapLeaf updates maxGap bottom-up from the calling leaf until no
// necessary update.
//
// Preconditions: n must be a leaf node, trackGaps must be 1.
func (n *addrnode) updateMaxGapLeaf() {
if n.hasChildren {
panic(fmt.Sprintf("updateMaxGapLeaf should always be called on leaf node: %v", n))
}
max := n.calculateMaxGapLeaf()
if max == n.maxGap.Get() {
return
}
oldMax := n.maxGap.Get()
n.maxGap.Set(max)
if max > oldMax {
for p := n.parent; p != nil; p = p.parent {
if p.maxGap.Get() >= max {
break
}
p.maxGap.Set(max)
}
return
}
for p := n.parent; p != nil; p = p.parent {
if p.maxGap.Get() > oldMax {
break
}
parentNewMax := p.calculateMaxGapInternal()
if p.maxGap.Get() == parentNewMax {
break
}
p.maxGap.Set(parentNewMax)
}
}
// updateMaxGapLocal updates maxGap of the calling node solely with no
// propagation to ancestor nodes.
//
// Precondition: trackGaps must be 1.
func (n *addrnode) updateMaxGapLocal() {
if !n.hasChildren {
n.maxGap.Set(n.calculateMaxGapLeaf())
} else {
n.maxGap.Set(n.calculateMaxGapInternal())
}
}
// calculateMaxGapLeaf iterates the gaps within a leaf node and calculate the
// max.
//
// Preconditions: n must be a leaf node.
func (n *addrnode) calculateMaxGapLeaf() uintptr {
max := addrGapIterator{n, 0}.Range().Length()
for i := 1; i <= n.nrSegments; i++ {
if current := (addrGapIterator{n, i}).Range().Length(); current > max {
max = current
}
}
return max
}
// calculateMaxGapInternal iterates children's maxGap within an internal node n
// and calculate the max.
//
// Preconditions: n must be a non-leaf node.
func (n *addrnode) calculateMaxGapInternal() uintptr {
max := n.children[0].maxGap.Get()
for i := 1; i <= n.nrSegments; i++ {
if current := n.children[i].maxGap.Get(); current > max {
max = current
}
}
return max
}
// searchFirstLargeEnoughGap returns the first gap having at least minSize length
// in the subtree rooted by n. If not found, return a terminal gap iterator.
func (n *addrnode) searchFirstLargeEnoughGap(minSize uintptr) addrGapIterator {
if n.maxGap.Get() < minSize {
return addrGapIterator{}
}
if n.hasChildren {
for i := 0; i <= n.nrSegments; i++ {
if largeEnoughGap := n.children[i].searchFirstLargeEnoughGap(minSize); largeEnoughGap.Ok() {
return largeEnoughGap
}
}
} else {
for i := 0; i <= n.nrSegments; i++ {
currentGap := addrGapIterator{n, i}
if currentGap.Range().Length() >= minSize {
return currentGap
}
}
}
panic(fmt.Sprintf("invalid maxGap in %v", n))
}
// searchLastLargeEnoughGap returns the last gap having at least minSize length
// in the subtree rooted by n. If not found, return a terminal gap iterator.
func (n *addrnode) searchLastLargeEnoughGap(minSize uintptr) addrGapIterator {
if n.maxGap.Get() < minSize {
return addrGapIterator{}
}
if n.hasChildren {
for i := n.nrSegments; i >= 0; i-- {
if largeEnoughGap := n.children[i].searchLastLargeEnoughGap(minSize); largeEnoughGap.Ok() {
return largeEnoughGap
}
}
} else {
for i := n.nrSegments; i >= 0; i-- {
currentGap := addrGapIterator{n, i}
if currentGap.Range().Length() >= minSize {
return currentGap
}
}
}
panic(fmt.Sprintf("invalid maxGap in %v", n))
}
// A Iterator is conceptually one of:
//
// - A pointer to a segment in a set; or
//
// - A terminal iterator, which is a sentinel indicating that the end of
// iteration has been reached.
//
// Iterators are copyable values and are meaningfully equality-comparable. The
// zero value of Iterator is a terminal iterator.
//
// Unless otherwise specified, any mutation of a set invalidates all existing
// iterators into the set.
type addrIterator struct {
// node is the node containing the iterated segment. If the iterator is
// terminal, node is nil.
node *addrnode
// index is the index of the segment in node.keys/values.
index int
}
// Ok returns true if the iterator is not terminal. All other methods are only
// valid for non-terminal iterators.
func (seg addrIterator) Ok() bool {
return seg.node != nil
}
// Range returns the iterated segment's range key.
func (seg addrIterator) Range() addrRange {
return seg.node.keys[seg.index]
}
// Start is equivalent to Range().Start, but should be preferred if only the
// start of the range is needed.
func (seg addrIterator) Start() uintptr {
return seg.node.keys[seg.index].Start
}
// End is equivalent to Range().End, but should be preferred if only the end of
// the range is needed.
func (seg addrIterator) End() uintptr {
return seg.node.keys[seg.index].End
}
// SetRangeUnchecked mutates the iterated segment's range key. This operation
// does not invalidate any iterators.
//
// Preconditions:
// * r.Length() > 0.
// * The new range must not overlap an existing one:
// * If seg.NextSegment().Ok(), then r.end <= seg.NextSegment().Start().
// * If seg.PrevSegment().Ok(), then r.start >= seg.PrevSegment().End().
func (seg addrIterator) SetRangeUnchecked(r addrRange) {
seg.node.keys[seg.index] = r
}
// SetRange mutates the iterated segment's range key. If the new range would
// cause the iterated segment to overlap another segment, or if the new range
// is invalid, SetRange panics. This operation does not invalidate any
// iterators.
func (seg addrIterator) SetRange(r addrRange) {
if r.Length() <= 0 {
panic(fmt.Sprintf("invalid segment range %v", r))
}
if prev := seg.PrevSegment(); prev.Ok() && r.Start < prev.End() {
panic(fmt.Sprintf("new segment range %v overlaps segment range %v", r, prev.Range()))
}
if next := seg.NextSegment(); next.Ok() && r.End > next.Start() {
panic(fmt.Sprintf("new segment range %v overlaps segment range %v", r, next.Range()))
}
seg.SetRangeUnchecked(r)
}
// SetStartUnchecked mutates the iterated segment's start. This operation does
// not invalidate any iterators.
//
// Preconditions: The new start must be valid:
// * start < seg.End()
// * If seg.PrevSegment().Ok(), then start >= seg.PrevSegment().End().
func (seg addrIterator) SetStartUnchecked(start uintptr) {
seg.node.keys[seg.index].Start = start
}
// SetStart mutates the iterated segment's start. If the new start value would
// cause the iterated segment to overlap another segment, or would result in an
// invalid range, SetStart panics. This operation does not invalidate any
// iterators.
func (seg addrIterator) SetStart(start uintptr) {
if start >= seg.End() {
panic(fmt.Sprintf("new start %v would invalidate segment range %v", start, seg.Range()))
}
if prev := seg.PrevSegment(); prev.Ok() && start < prev.End() {
panic(fmt.Sprintf("new start %v would cause segment range %v to overlap segment range %v", start, seg.Range(), prev.Range()))
}
seg.SetStartUnchecked(start)
}
// SetEndUnchecked mutates the iterated segment's end. This operation does not
// invalidate any iterators.
//
// Preconditions: The new end must be valid:
// * end > seg.Start().
// * If seg.NextSegment().Ok(), then end <= seg.NextSegment().Start().
func (seg addrIterator) SetEndUnchecked(end uintptr) {
seg.node.keys[seg.index].End = end
}
// SetEnd mutates the iterated segment's end. If the new end value would cause
// the iterated segment to overlap another segment, or would result in an
// invalid range, SetEnd panics. This operation does not invalidate any
// iterators.
func (seg addrIterator) SetEnd(end uintptr) {
if end <= seg.Start() {
panic(fmt.Sprintf("new end %v would invalidate segment range %v", end, seg.Range()))
}
if next := seg.NextSegment(); next.Ok() && end > next.Start() {
panic(fmt.Sprintf("new end %v would cause segment range %v to overlap segment range %v", end, seg.Range(), next.Range()))
}
seg.SetEndUnchecked(end)
}
// Value returns a copy of the iterated segment's value.
func (seg addrIterator) Value() *objectEncodeState {
return seg.node.values[seg.index]
}
// ValuePtr returns a pointer to the iterated segment's value. The pointer is
// invalidated if the iterator is invalidated. This operation does not
// invalidate any iterators.
func (seg addrIterator) ValuePtr() **objectEncodeState {
return &seg.node.values[seg.index]
}
// SetValue mutates the iterated segment's value. This operation does not
// invalidate any iterators.
func (seg addrIterator) SetValue(val *objectEncodeState) {
seg.node.values[seg.index] = val
}
// PrevSegment returns the iterated segment's predecessor. If there is no
// preceding segment, PrevSegment returns a terminal iterator.
func (seg addrIterator) PrevSegment() addrIterator {
if seg.node.hasChildren {
return seg.node.children[seg.index].lastSegment()
}
if seg.index > 0 {
return addrIterator{seg.node, seg.index - 1}
}
if seg.node.parent == nil {
return addrIterator{}
}
return addrsegmentBeforePosition(seg.node.parent, seg.node.parentIndex)
}
// NextSegment returns the iterated segment's successor. If there is no
// succeeding segment, NextSegment returns a terminal iterator.
func (seg addrIterator) NextSegment() addrIterator {
if seg.node.hasChildren {
return seg.node.children[seg.index+1].firstSegment()
}
if seg.index < seg.node.nrSegments-1 {
return addrIterator{seg.node, seg.index + 1}
}
if seg.node.parent == nil {
return addrIterator{}
}
return addrsegmentAfterPosition(seg.node.parent, seg.node.parentIndex)
}
// PrevGap returns the gap immediately before the iterated segment.
func (seg addrIterator) PrevGap() addrGapIterator {
if seg.node.hasChildren {
return seg.node.children[seg.index].lastSegment().NextGap()
}
return addrGapIterator{seg.node, seg.index}
}
// NextGap returns the gap immediately after the iterated segment.
func (seg addrIterator) NextGap() addrGapIterator {
if seg.node.hasChildren {
return seg.node.children[seg.index+1].firstSegment().PrevGap()
}
return addrGapIterator{seg.node, seg.index + 1}
}
// PrevNonEmpty returns the iterated segment's predecessor if it is adjacent,
// or the gap before the iterated segment otherwise. If seg.Start() ==
// Functions.MinKey(), PrevNonEmpty will return two terminal iterators.
// Otherwise, exactly one of the iterators returned by PrevNonEmpty will be
// non-terminal.
func (seg addrIterator) PrevNonEmpty() (addrIterator, addrGapIterator) {
gap := seg.PrevGap()
if gap.Range().Length() != 0 {
return addrIterator{}, gap
}
return gap.PrevSegment(), addrGapIterator{}
}
// NextNonEmpty returns the iterated segment's successor if it is adjacent, or
// the gap after the iterated segment otherwise. If seg.End() ==
// Functions.MaxKey(), NextNonEmpty will return two terminal iterators.
// Otherwise, exactly one of the iterators returned by NextNonEmpty will be
// non-terminal.
func (seg addrIterator) NextNonEmpty() (addrIterator, addrGapIterator) {
gap := seg.NextGap()
if gap.Range().Length() != 0 {
return addrIterator{}, gap
}
return gap.NextSegment(), addrGapIterator{}
}
// A GapIterator is conceptually one of:
//
// - A pointer to a position between two segments, before the first segment, or
// after the last segment in a set, called a *gap*; or
//
// - A terminal iterator, which is a sentinel indicating that the end of
// iteration has been reached.
//
// Note that the gap between two adjacent segments exists (iterators to it are
// non-terminal), but has a length of zero. GapIterator.IsEmpty returns true
// for such gaps. An empty set contains a single gap, spanning the entire range
// of the set's keys.
//
// GapIterators are copyable values and are meaningfully equality-comparable.
// The zero value of GapIterator is a terminal iterator.
//
// Unless otherwise specified, any mutation of a set invalidates all existing
// iterators into the set.
type addrGapIterator struct {
// The representation of a GapIterator is identical to that of an Iterator,
// except that index corresponds to positions between segments in the same
// way as for node.children (see comment for node.nrSegments).
node *addrnode
index int
}
// Ok returns true if the iterator is not terminal. All other methods are only
// valid for non-terminal iterators.
func (gap addrGapIterator) Ok() bool {
return gap.node != nil
}
// Range returns the range spanned by the iterated gap.
func (gap addrGapIterator) Range() addrRange {
return addrRange{gap.Start(), gap.End()}
}
// Start is equivalent to Range().Start, but should be preferred if only the
// start of the range is needed.
func (gap addrGapIterator) Start() uintptr {
if ps := gap.PrevSegment(); ps.Ok() {
return ps.End()
}
return addrSetFunctions{}.MinKey()
}
// End is equivalent to Range().End, but should be preferred if only the end of
// the range is needed.
func (gap addrGapIterator) End() uintptr {
if ns := gap.NextSegment(); ns.Ok() {
return ns.Start()
}
return addrSetFunctions{}.MaxKey()
}
// IsEmpty returns true if the iterated gap is empty (that is, the "gap" is
// between two adjacent segments.)
func (gap addrGapIterator) IsEmpty() bool {
return gap.Range().Length() == 0
}
// PrevSegment returns the segment immediately before the iterated gap. If no
// such segment exists, PrevSegment returns a terminal iterator.
func (gap addrGapIterator) PrevSegment() addrIterator {
return addrsegmentBeforePosition(gap.node, gap.index)
}
// NextSegment returns the segment immediately after the iterated gap. If no
// such segment exists, NextSegment returns a terminal iterator.
func (gap addrGapIterator) NextSegment() addrIterator {
return addrsegmentAfterPosition(gap.node, gap.index)
}
// PrevGap returns the iterated gap's predecessor. If no such gap exists,
// PrevGap returns a terminal iterator.
func (gap addrGapIterator) PrevGap() addrGapIterator {
seg := gap.PrevSegment()
if !seg.Ok() {
return addrGapIterator{}
}
return seg.PrevGap()
}
// NextGap returns the iterated gap's successor. If no such gap exists, NextGap
// returns a terminal iterator.
func (gap addrGapIterator) NextGap() addrGapIterator {
seg := gap.NextSegment()
if !seg.Ok() {
return addrGapIterator{}
}
return seg.NextGap()
}
// NextLargeEnoughGap returns the iterated gap's first next gap with larger
// length than minSize. If not found, return a terminal gap iterator (does NOT
// include this gap itself).
//
// Precondition: trackGaps must be 1.
func (gap addrGapIterator) NextLargeEnoughGap(minSize uintptr) addrGapIterator {
if addrtrackGaps != 1 {
panic("set is not tracking gaps")
}
if gap.node != nil && gap.node.hasChildren && gap.index == gap.node.nrSegments {
gap.node = gap.NextSegment().node
gap.index = 0
return gap.nextLargeEnoughGapHelper(minSize)
}
return gap.nextLargeEnoughGapHelper(minSize)
}
// nextLargeEnoughGapHelper is the helper function used by NextLargeEnoughGap
// to do the real recursions.
//
// Preconditions: gap is NOT the trailing gap of a non-leaf node.
func (gap addrGapIterator) nextLargeEnoughGapHelper(minSize uintptr) addrGapIterator {
for gap.node != nil &&
(gap.node.maxGap.Get() < minSize || (!gap.node.hasChildren && gap.index == gap.node.nrSegments)) {
gap.node, gap.index = gap.node.parent, gap.node.parentIndex
}
if gap.node == nil {
return addrGapIterator{}
}
gap.index++
for gap.index <= gap.node.nrSegments {
if gap.node.hasChildren {
if largeEnoughGap := gap.node.children[gap.index].searchFirstLargeEnoughGap(minSize); largeEnoughGap.Ok() {
return largeEnoughGap
}
} else {
if gap.Range().Length() >= minSize {
return gap
}
}
gap.index++
}
gap.node, gap.index = gap.node.parent, gap.node.parentIndex
if gap.node != nil && gap.index == gap.node.nrSegments {
gap.node, gap.index = gap.node.parent, gap.node.parentIndex
}
return gap.nextLargeEnoughGapHelper(minSize)
}
// PrevLargeEnoughGap returns the iterated gap's first prev gap with larger or
// equal length than minSize. If not found, return a terminal gap iterator
// (does NOT include this gap itself).
//
// Precondition: trackGaps must be 1.
func (gap addrGapIterator) PrevLargeEnoughGap(minSize uintptr) addrGapIterator {
if addrtrackGaps != 1 {
panic("set is not tracking gaps")
}
if gap.node != nil && gap.node.hasChildren && gap.index == 0 {
gap.node = gap.PrevSegment().node
gap.index = gap.node.nrSegments
return gap.prevLargeEnoughGapHelper(minSize)
}
return gap.prevLargeEnoughGapHelper(minSize)
}
// prevLargeEnoughGapHelper is the helper function used by PrevLargeEnoughGap
// to do the real recursions.
//
// Preconditions: gap is NOT the first gap of a non-leaf node.
func (gap addrGapIterator) prevLargeEnoughGapHelper(minSize uintptr) addrGapIterator {
for gap.node != nil &&
(gap.node.maxGap.Get() < minSize || (!gap.node.hasChildren && gap.index == 0)) {
gap.node, gap.index = gap.node.parent, gap.node.parentIndex
}
if gap.node == nil {
return addrGapIterator{}
}
gap.index--
for gap.index >= 0 {
if gap.node.hasChildren {
if largeEnoughGap := gap.node.children[gap.index].searchLastLargeEnoughGap(minSize); largeEnoughGap.Ok() {
return largeEnoughGap
}
} else {
if gap.Range().Length() >= minSize {
return gap
}
}
gap.index--
}
gap.node, gap.index = gap.node.parent, gap.node.parentIndex
if gap.node != nil && gap.index == 0 {
gap.node, gap.index = gap.node.parent, gap.node.parentIndex
}
return gap.prevLargeEnoughGapHelper(minSize)
}
// segmentBeforePosition returns the predecessor segment of the position given
// by n.children[i], which may or may not contain a child. If no such segment
// exists, segmentBeforePosition returns a terminal iterator.
func addrsegmentBeforePosition(n *addrnode, i int) addrIterator {
for i == 0 {
if n.parent == nil {
return addrIterator{}
}
n, i = n.parent, n.parentIndex
}
return addrIterator{n, i - 1}
}
// segmentAfterPosition returns the successor segment of the position given by
// n.children[i], which may or may not contain a child. If no such segment
// exists, segmentAfterPosition returns a terminal iterator.
func addrsegmentAfterPosition(n *addrnode, i int) addrIterator {
for i == n.nrSegments {
if n.parent == nil {
return addrIterator{}
}
n, i = n.parent, n.parentIndex
}
return addrIterator{n, i}
}
func addrzeroValueSlice(slice []*objectEncodeState) {
for i := range slice {
addrSetFunctions{}.ClearValue(&slice[i])
}
}
func addrzeroNodeSlice(slice []*addrnode) {
for i := range slice {
slice[i] = nil
}
}
// String stringifies a Set for debugging.
func (s *addrSet) String() string {
return s.root.String()
}
// String stringifies a node (and all of its children) for debugging.
func (n *addrnode) String() string {
var buf bytes.Buffer
n.writeDebugString(&buf, "")
return buf.String()
}
func (n *addrnode) writeDebugString(buf *bytes.Buffer, prefix string) {
if n.hasChildren != (n.nrSegments > 0 && n.children[0] != nil) {
buf.WriteString(prefix)
buf.WriteString(fmt.Sprintf("WARNING: inconsistent value of hasChildren: got %v, want %v\n", n.hasChildren, !n.hasChildren))
}
for i := 0; i < n.nrSegments; i++ {
if child := n.children[i]; child != nil {
cprefix := fmt.Sprintf("%s- % 3d ", prefix, i)
if child.parent != n || child.parentIndex != i {
buf.WriteString(cprefix)
buf.WriteString(fmt.Sprintf("WARNING: inconsistent linkage to parent: got (%p, %d), want (%p, %d)\n", child.parent, child.parentIndex, n, i))
}
child.writeDebugString(buf, fmt.Sprintf("%s- % 3d ", prefix, i))
}
buf.WriteString(prefix)
if n.hasChildren {
if addrtrackGaps != 0 {
buf.WriteString(fmt.Sprintf("- % 3d: %v => %v, maxGap: %d\n", i, n.keys[i], n.values[i], n.maxGap.Get()))
} else {
buf.WriteString(fmt.Sprintf("- % 3d: %v => %v\n", i, n.keys[i], n.values[i]))
}
} else {
buf.WriteString(fmt.Sprintf("- % 3d: %v => %v\n", i, n.keys[i], n.values[i]))
}
}
if child := n.children[n.nrSegments]; child != nil {
child.writeDebugString(buf, fmt.Sprintf("%s- % 3d ", prefix, n.nrSegments))
}
}
// SegmentDataSlices represents segments from a set as slices of start, end, and
// values. SegmentDataSlices is primarily used as an intermediate representation
// for save/restore and the layout here is optimized for that.
//
// +stateify savable
type addrSegmentDataSlices struct {
Start []uintptr
End []uintptr
Values []*objectEncodeState
}
// ExportSortedSlices returns a copy of all segments in the given set, in
// ascending key order.
func (s *addrSet) ExportSortedSlices() *addrSegmentDataSlices {
var sds addrSegmentDataSlices
for seg := s.FirstSegment(); seg.Ok(); seg = seg.NextSegment() {
sds.Start = append(sds.Start, seg.Start())
sds.End = append(sds.End, seg.End())
sds.Values = append(sds.Values, seg.Value())
}
sds.Start = sds.Start[:len(sds.Start):len(sds.Start)]
sds.End = sds.End[:len(sds.End):len(sds.End)]
sds.Values = sds.Values[:len(sds.Values):len(sds.Values)]
return &sds
}
// ImportSortedSlices initializes the given set from the given slice.
//
// Preconditions:
// * s must be empty.
// * sds must represent a valid set (the segments in sds must have valid
// lengths that do not overlap).
// * The segments in sds must be sorted in ascending key order.
func (s *addrSet) ImportSortedSlices(sds *addrSegmentDataSlices) error {
if !s.IsEmpty() {
return fmt.Errorf("cannot import into non-empty set %v", s)
}
gap := s.FirstGap()
for i := range sds.Start {
r := addrRange{sds.Start[i], sds.End[i]}
if !gap.Range().IsSupersetOf(r) {
return fmt.Errorf("segment overlaps a preceding segment or is incorrectly sorted: [%d, %d) => %v", sds.Start[i], sds.End[i], sds.Values[i])
}
gap = s.InsertWithoutMerging(gap, r, sds.Values[i]).NextGap()
}
return nil
}
// segmentTestCheck returns an error if s is incorrectly sorted, does not
// contain exactly expectedSegments segments, or contains a segment which
// fails the passed check.
//
// This should be used only for testing, and has been added to this package for
// templating convenience.
func (s *addrSet) segmentTestCheck(expectedSegments int, segFunc func(int, addrRange, *objectEncodeState) error) error {
havePrev := false
prev := uintptr(0)
nrSegments := 0
for seg := s.FirstSegment(); seg.Ok(); seg = seg.NextSegment() {
next := seg.Start()
if havePrev && prev >= next {
return fmt.Errorf("incorrect order: key %d (segment %d) >= key %d (segment %d)", prev, nrSegments-1, next, nrSegments)
}
if segFunc != nil {
if err := segFunc(nrSegments, seg.Range(), seg.Value()); err != nil {
return err
}
}
prev = next
havePrev = true
nrSegments++
}
if nrSegments != expectedSegments {
return fmt.Errorf("incorrect number of segments: got %d, wanted %d", nrSegments, expectedSegments)
}
return nil
}
// countSegments counts the number of segments in the set.
//
// Similar to Check, this should only be used for testing.
func (s *addrSet) countSegments() (segments int) {
for seg := s.FirstSegment(); seg.Ok(); seg = seg.NextSegment() {
segments++
}
return segments
}
func (s *addrSet) saveRoot() *addrSegmentDataSlices {
return s.ExportSortedSlices()
}
func (s *addrSet) loadRoot(sds *addrSegmentDataSlices) {
if err := s.ImportSortedSlices(sds); err != nil {
panic(err)
}
}
|