File indexing completed on 2024-04-21 16:33:50

0001 /*
0002     This file is part of the Okteta Core library, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2003 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "addressrangelist.hpp"
0010 
0011 namespace Okteta {
0012 
0013 AddressRangeList::AddressRangeList() = default;
0014 AddressRangeList::AddressRangeList(const AddressRangeList&) = default;
0015 
0016 AddressRangeList::~AddressRangeList() = default;
0017 
0018 void AddressRangeList::addAddressRange(const AddressRange& newAddressRange)
0019 {
0020     if (!newAddressRange.isValid()) {
0021         return;
0022     }
0023 
0024     // we try to insert it by ascending indizes
0025     // if addressRanges are overlapping we combine them
0026     Iterator firstOverlappingIt = begin();
0027     for (; firstOverlappingIt != end(); ++firstOverlappingIt) {
0028         // new addressRange before next addressRange?
0029         if (newAddressRange.endsBefore((*firstOverlappingIt).nextBeforeStart())) {
0030             // put the new before it
0031             insert(firstOverlappingIt, newAddressRange);
0032             return;
0033         }
0034 
0035         // does the next addressRange overlap?
0036         if ((*firstOverlappingIt).isJoinable(newAddressRange)) {
0037             AddressRange joinedAddressRange(newAddressRange);
0038             // Start of the joined addressRanges is the smaller one
0039             joinedAddressRange.extendStartTo((*firstOverlappingIt).start());
0040             // next we search all the overlapping addressRanges and keep the highest end index
0041             int joinedEnd = (*firstOverlappingIt).end();
0042             Iterator lastOverlappingIt = firstOverlappingIt;
0043             for (++lastOverlappingIt; lastOverlappingIt != end(); ++lastOverlappingIt) {
0044                 if (joinedAddressRange.endsBefore((*lastOverlappingIt).nextBeforeStart())) {
0045                     break;
0046                 }
0047                 joinedEnd = (*lastOverlappingIt).end();
0048             }
0049 
0050             // the higher end is the end of the joined addressRange
0051             joinedAddressRange.extendEndTo(joinedEnd);
0052             // remove all overlapping addressRanges
0053             firstOverlappingIt = erase(firstOverlappingIt, lastOverlappingIt);
0054             // and instead insert the joined one
0055             insert(firstOverlappingIt, joinedAddressRange);
0056             return;
0057         }
0058     }
0059 
0060     // all others before the new?
0061     if (firstOverlappingIt == end()) {
0062         // add it at the end
0063         append(newAddressRange);
0064     }
0065 }
0066 
0067 void AddressRangeList::addAddressRangeList(const AddressRangeList& addressRangeList)
0068 {
0069     // TODO: optimize with two parallel iterators
0070     for (const AddressRange& addressRange : addressRangeList) {
0071         addAddressRange(addressRange);
0072     }
0073 }
0074 
0075 }