Optimizes the free slot list structure by merging adjacent entries. Currently, this function is called after every addition of a new slot value.
()
| 71 | * Currently, this function is called after every addition of a new slot value. |
| 72 | */ |
| 73 | private void optimize() { |
| 74 | if(free.isEmpty()) return; |
| 75 | |
| 76 | // sort all entries by their offset (use native arrays; faster than TreeMap) |
| 77 | final int size = slots; |
| 78 | final LongList offList = new LongList(size); |
| 79 | final IntList sizeList = new IntList(size); |
| 80 | free.forEach((slotSize, list) -> { |
| 81 | final int ll = list.size(); |
| 82 | for(int l = 0; l < ll; l++) { |
| 83 | offList.add(list.get(l)); |
| 84 | sizeList.add(slotSize); |
| 85 | } |
| 86 | }); |
| 87 | if(size != offList.size()) |
| 88 | throw Util.notExpected("Wrong slot count: % vs. %", size, offList.size()); |
| 89 | |
| 90 | final long[] offsets = offList.finish(); |
| 91 | final int[] slotSizes = sizeList.finish(); |
| 92 | final int[] index = Array.createOrder(offsets, true); |
| 93 | |
| 94 | // rebuild map with merged slots |
| 95 | free.clear(); |
| 96 | slots = 0; |
| 97 | long offset = offsets[0]; |
| 98 | int slotSize = slotSizes[index[0]]; |
| 99 | for(int c = 1; c < size; c++) { |
| 100 | final long o = offsets[c]; |
| 101 | final int s = slotSizes[index[c]]; |
| 102 | if(o == offset + slotSize) { |
| 103 | slotSize += s; |
| 104 | } else { |
| 105 | add(slotSize, offset, false); |
| 106 | offset = o; |
| 107 | slotSize = s; |
| 108 | } |
| 109 | } |
| 110 | add(slotSize, offset, false); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public String toString() { |