Avoid unnecessary detaching of containers

If only a constant iterator is needed, make the container const if
possible, otherwise use cbegin() and cend() to get a constant iterator
without detaching the container.

These fixes are needed so that `auto` can deduce the correct
iterator type.
This commit is contained in:
Urs Fleisch
2023-07-15 20:53:49 +02:00
parent b273505c28
commit 63922f2676
42 changed files with 218 additions and 116 deletions

View File

@ -95,7 +95,7 @@ ByteVector Ogg::File::packet(unsigned int i)
// Look for the first page in which the requested packet starts.
List<Page *>::ConstIterator it = d->pages.begin();
List<Page *>::ConstIterator it = d->pages.cbegin();
while((*it)->containsPacket(i) == Page::DoesNotContainPacket)
++it;
@ -160,7 +160,7 @@ bool Ogg::File::save()
}
Map<unsigned int, ByteVector>::ConstIterator it;
for(it = d->dirtyPackets.begin(); it != d->dirtyPackets.end(); ++it)
for(it = d->dirtyPackets.cbegin(); it != d->dirtyPackets.cend(); ++it)
writePacket(it->first, it->second);
d->dirtyPackets.clear();
@ -236,7 +236,7 @@ void Ogg::File::writePacket(unsigned int i, const ByteVector &packet)
// Look for the pages where the requested packet should belong to.
List<Page *>::ConstIterator it = d->pages.begin();
List<Page *>::ConstIterator it = d->pages.cbegin();
while((*it)->containsPacket(i) == Page::DoesNotContainPacket)
++it;

View File

@ -211,7 +211,7 @@ ByteVectorList Ogg::Page::packets() const
d->file->seek(d->fileOffset + d->header.size());
List<int> packetSizes = d->header.packetSizes();
const List<int> packetSizes = d->header.packetSizes();
List<int>::ConstIterator it = packetSizes.begin();
for(; it != packetSizes.end(); ++it)
@ -243,8 +243,8 @@ ByteVector Ogg::Page::render() const
debug("Ogg::Page::render() -- this page is empty!");
}
else {
ByteVectorList::ConstIterator it = d->packets.begin();
for(; it != d->packets.end(); ++it)
ByteVectorList::ConstIterator it = d->packets.cbegin();
for(; it != d->packets.cend(); ++it)
data.append(*it);
}

View File

@ -295,7 +295,7 @@ ByteVector Ogg::PageHeader::lacingValues() const
{
ByteVector data;
for(List<int>::ConstIterator it = d->packetSizes.begin(); it != d->packetSizes.end(); ++it) {
for(List<int>::ConstIterator it = d->packetSizes.cbegin(); it != d->packetSizes.cend(); ++it) {
// The size of a packet in an Ogg page is indicated by a series of "lacing
// values" where the sum of the values is the packet size in bytes. Each of
@ -304,7 +304,7 @@ ByteVector Ogg::PageHeader::lacingValues() const
data.resize(data.size() + (*it / 255), '\xff');
if(it != --d->packetSizes.end() || d->lastPacketCompleted)
if(it != --d->packetSizes.cend() || d->lastPacketCompleted)
data.append(static_cast<unsigned char>(*it % 255));
}

View File

@ -39,7 +39,7 @@ namespace
typedef List<FLAC::Picture *> PictureList;
typedef PictureList::Iterator PictureIterator;
typedef PictureList::Iterator PictureConstIterator;
typedef PictureList::ConstIterator PictureConstIterator;
} // namespace
class Ogg::XiphComment::XiphCommentPrivate
@ -191,7 +191,7 @@ void Ogg::XiphComment::setTrack(unsigned int i)
bool Ogg::XiphComment::isEmpty() const
{
for(FieldConstIterator it = d->fieldListMap.begin(); it != d->fieldListMap.end(); ++it) {
for(FieldConstIterator it = d->fieldListMap.cbegin(); it != d->fieldListMap.cend(); ++it) {
if(!(*it).second.isEmpty())
return false;
}
@ -203,7 +203,7 @@ unsigned int Ogg::XiphComment::fieldCount() const
{
unsigned int count = 0;
for(FieldConstIterator it = d->fieldListMap.begin(); it != d->fieldListMap.end(); ++it)
for(FieldConstIterator it = d->fieldListMap.cbegin(); it != d->fieldListMap.cend(); ++it)
count += (*it).second.size();
count += d->pictureList.size();
@ -225,11 +225,11 @@ PropertyMap Ogg::XiphComment::setProperties(const PropertyMap &properties)
{
// check which keys are to be deleted
StringList toRemove;
for(FieldConstIterator it = d->fieldListMap.begin(); it != d->fieldListMap.end(); ++it)
for(FieldConstIterator it = d->fieldListMap.cbegin(); it != d->fieldListMap.cend(); ++it)
if (!properties.contains(it->first))
toRemove.append(it->first);
for(StringList::ConstIterator it = toRemove.begin(); it != toRemove.end(); ++it)
for(StringList::ConstIterator it = toRemove.cbegin(); it != toRemove.cend(); ++it)
removeFields(*it);
// now go through keys in \a properties and check that the values match those in the xiph comment
@ -366,13 +366,13 @@ ByteVector Ogg::XiphComment::render(bool addFramingBit) const
// std::pair<String, StringList> where the first String is the field name and
// the StringList is the values associated with that field.
FieldListMap::ConstIterator it = d->fieldListMap.begin();
for(; it != d->fieldListMap.end(); ++it) {
FieldListMap::ConstIterator it = d->fieldListMap.cbegin();
for(; it != d->fieldListMap.cend(); ++it) {
// And now iterate over the values of the current list.
String fieldName = (*it).first;
StringList values = (*it).second;
const StringList values = (*it).second;
StringList::ConstIterator valuesIt = values.begin();
for(; valuesIt != values.end(); ++valuesIt) {
@ -385,7 +385,7 @@ ByteVector Ogg::XiphComment::render(bool addFramingBit) const
}
}
for(PictureConstIterator it = d->pictureList.begin(); it != d->pictureList.end(); ++it) {
for(PictureConstIterator it = d->pictureList.cbegin(); it != d->pictureList.cend(); ++it) {
ByteVector picture = (*it)->render().toBase64();
data.append(ByteVector::fromUInt(picture.size() + 23, false));
data.append("METADATA_BLOCK_PICTURE=");