PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
RegionGarbageMap.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\world\format\io\region;
25
27use function end;
28use function ksort;
29use function time;
30use const SORT_NUMERIC;
31
32final class RegionGarbageMap{
33
35 private array $entries = [];
36 private bool $clean = false;
37
41 public function __construct(array $entries){
42 foreach($entries as $entry){
43 $this->entries[$entry->getFirstSector()] = $entry;
44 }
45 }
46
50 public static function buildFromLocationTable(array $locationTable) : self{
52 $usedMap = [];
53 foreach($locationTable as $entry){
54 if($entry === null){
55 continue;
56 }
57 if(isset($usedMap[$entry->getFirstSector()])){
58 throw new AssumptionFailedError("Overlapping entries detected");
59 }
60 $usedMap[$entry->getFirstSector()] = $entry;
61 }
62
63 ksort($usedMap, SORT_NUMERIC);
64
66 $garbageMap = [];
67
69 $prevEntry = null;
70 foreach($usedMap as $firstSector => $entry){
71 $prevEndPlusOne = ($prevEntry !== null ? $prevEntry->getLastSector() + 1 : RegionLoader::FIRST_SECTOR);
72 $currentStart = $entry->getFirstSector();
73 if($prevEndPlusOne < $currentStart){
74 //found a gap in the table
75 $garbageMap[$prevEndPlusOne] = new RegionLocationTableEntry($prevEndPlusOne, $currentStart - $prevEndPlusOne, 0);
76 }elseif($prevEndPlusOne > $currentStart){
77 //current entry starts inside the previous. This would be a bug since RegionLoader should prevent this
78 throw new AssumptionFailedError("Overlapping entries detected");
79 }
80 $prevEntry = $entry;
81 }
82
83 return new self($garbageMap);
84 }
85
90 public function getArray() : array{
91 if(!$this->clean){
92 ksort($this->entries, SORT_NUMERIC);
93
95 $prevIndex = null;
96 foreach($this->entries as $k => $entry){
97 if($prevIndex !== null && $this->entries[$prevIndex]->getLastSector() + 1 === $entry->getFirstSector()){
98 //this SHOULD overwrite the previous index and not appear at the end
99 $this->entries[$prevIndex] = new RegionLocationTableEntry(
100 $this->entries[$prevIndex]->getFirstSector(),
101 $this->entries[$prevIndex]->getSectorCount() + $entry->getSectorCount(),
102 0
103 );
104 unset($this->entries[$k]);
105 }else{
106 $prevIndex = $k;
107 }
108 }
109 $this->clean = true;
110 }
111 return $this->entries;
112 }
113
114 public function add(RegionLocationTableEntry $entry) : void{
115 if(isset($this->entries[$k = $entry->getFirstSector()])){
116 throw new \InvalidArgumentException("Overlapping entry starting at " . $k);
117 }
118 $this->entries[$k] = $entry;
119 $this->clean = false;
120 }
121
122 public function remove(RegionLocationTableEntry $entry) : void{
123 if(isset($this->entries[$k = $entry->getFirstSector()])){
124 //removal doesn't affect ordering and shouldn't affect fragmentation
125 unset($this->entries[$k]);
126 }
127 }
128
129 public function end() : ?RegionLocationTableEntry{
130 $array = $this->getArray();
131 $end = end($array);
132 return $end !== false ? $end : null;
133 }
134
135 public function allocate(int $newSize) : ?RegionLocationTableEntry{
136 foreach($this->getArray() as $start => $candidate){
137 $candidateSize = $candidate->getSectorCount();
138 if($candidateSize < $newSize){
139 continue;
140 }
141
142 $newLocation = new RegionLocationTableEntry($candidate->getFirstSector(), $newSize, time());
143 $this->remove($candidate);
144
145 if($candidateSize > $newSize){ //we're not using the whole area, just take part of it
146 $newGarbageStart = $candidate->getFirstSector() + $newSize;
147 $newGarbageSize = $candidateSize - $newSize;
148 $this->add(new RegionLocationTableEntry($newGarbageStart, $newGarbageSize, 0));
149 }
150 return $newLocation;
151
152 }
153
154 return null;
155 }
156}