key = $key; $this->name = $name; $this->type = $type; $this->startAddress = $startAddress; $this->size = $size; $this->pageSize = $pageSize; $this->access = $access; $this->sections = $sections; } public function getSection(string $sectionId): ?MemorySegmentSection { foreach ($this->sections as $section) { if ($section->key !== $sectionId) { continue; } return $section; } return null; } public function contains(MemorySegment $other): bool { $endAddress = !is_null($this->startAddress) && !is_null($this->size) ? ($this->startAddress + $this->size - 1) : null; $otherEndAddress = !is_null($other->startAddress) && !is_null($other->size) ? ($other->startAddress + $other->size - 1) : null; return $this->startAddress !== null && $endAddress !== null && $other->startAddress !== null && $otherEndAddress !== null && $this->startAddress <= $other->startAddress && $endAddress >= $otherEndAddress ; } public function intersectsWith(MemorySegment $other): bool { $endAddress = !is_null($this->startAddress) && !is_null($this->size) ? ($this->startAddress + $this->size - 1) : null; $otherEndAddress = !is_null($other->startAddress) && !is_null($other->size) ? ($other->startAddress + $other->size - 1) : null; return $this->startAddress !== null && $endAddress !== null && $other->startAddress !== null && $otherEndAddress !== null && ( ($other->startAddress <= $this->startAddress && $otherEndAddress >= $this->startAddress) || ($other->startAddress >= $this->startAddress && $other->startAddress <= $endAddress) ) ; } public function totalSectionSize(): int { return array_sum( array_map( fn (MemorySegmentSection $section): int => (int) $section->size, $this->sections ) ); } }