How to modify/partially remove a range from a BTreeMap?

后端 未结 1 1904
孤街浪徒
孤街浪徒 2021-01-20 21:57

I\'m trying to build a RangeSet out of a BTreeMap (where the keys are lower bounds and the values are upper bounds). This works quite well as long

相关标签:
1条回答
  • 2021-01-20 22:36

    You need to do this in two passes, collecting the keys you want to delete, and then iterate over that list and call remove.

    Note that since while iterating over a tree you get references, you'll have to clone/copy the key because you'll have a reference to a key inside the map, which means you cannot borrow the map as mutable.

    The reason for this is largely because the memory and ordering semantics get a bit wonky while deleting entries in the middle of an iteration. It's pretty difficult to delete entries in a tree while iterating over it. This is exacerbated by it being a map, which means the keys are ordered in some way in the tree, meaning that nodes may end up rotating their children, so you may have previously visited your left child and now suddenly that's your current node, or right child. It's difficult to keep track of where you are.

    In addition, B-Tree nodes are lists of subnodes that have dynamics for when nodes need to merge and split, it's even more of a headache to do that in the middle of an iteration.

    Some of this is due to manual memory semanics. The keys and values are stored within the nodes, not on the heap, so memory is going to be bouncing all over the place and making sure it's not invalidated is non-trivial. Especially if the user was collecting references to entries in the tree elsewhere in the loop.

    Edit: And, regardless of possibility, the fact of the matter is that an iterator borrows the Map, which means you can't borrow it again to delete things. This might be different if the iterator returned something like an Entry, but as far as I know no iterator that does this exists.

    0 讨论(0)
提交回复
热议问题