1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-08-01 08:29:27 +02:00

Use stl container idioms

This commit is contained in:
Paul Licameli 2016-06-30 13:41:08 -04:00
parent 7c07cb3b42
commit 6e772a3569

View File

@ -130,7 +130,7 @@ LabelTrack::LabelTrack(const LabelTrack &orig) :
mClipLen(0.0), mClipLen(0.0),
mIsAdjustingLabel(false) mIsAdjustingLabel(false)
{ {
int len = orig.mLabels.Count(); int len = orig.mLabels.size();
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
const LabelStruct& original = *orig.mLabels[i]; const LabelStruct& original = *orig.mLabels[i];
@ -146,7 +146,7 @@ LabelTrack::LabelTrack(const LabelTrack &orig) :
LabelTrack::~LabelTrack() LabelTrack::~LabelTrack()
{ {
int len = mLabels.Count(); int len = mLabels.size();
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
delete mLabels[i]; delete mLabels[i];
@ -154,7 +154,7 @@ LabelTrack::~LabelTrack()
void LabelTrack::SetOffset(double dOffset) void LabelTrack::SetOffset(double dOffset)
{ {
int len = mLabels.Count(); int len = mLabels.size();
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
{ {
auto &labelStruct = *mLabels[i]; auto &labelStruct = *mLabels[i];
@ -164,7 +164,7 @@ void LabelTrack::SetOffset(double dOffset)
bool LabelTrack::Clear(double b, double e) bool LabelTrack::Clear(double b, double e)
{ {
for (size_t i=0;i<mLabels.GetCount();i++){ for (size_t i = 0 ; i < mLabels.size(); i++){
auto &labelStruct = *mLabels[i]; auto &labelStruct = *mLabels[i];
LabelStruct::TimeRelations relation = LabelStruct::TimeRelations relation =
labelStruct.RegionRelation(b, e, this); labelStruct.RegionRelation(b, e, this);
@ -191,7 +191,7 @@ bool LabelTrack::Clear(double b, double e)
//used when we want to use clear only on the labels //used when we want to use clear only on the labels
bool LabelTrack::SplitDelete(double b, double e) bool LabelTrack::SplitDelete(double b, double e)
{ {
for (size_t i=0;i<mLabels.GetCount();i++) { for (size_t i = 0 ; i < mLabels.size(); i++) {
auto &labelStruct = *mLabels[i]; auto &labelStruct = *mLabels[i];
LabelStruct::TimeRelations relation = LabelStruct::TimeRelations relation =
labelStruct.RegionRelation(b, e, this); labelStruct.RegionRelation(b, e, this);
@ -213,7 +213,7 @@ bool LabelTrack::SplitDelete(double b, double e)
void LabelTrack::ShiftLabelsOnInsert(double length, double pt) void LabelTrack::ShiftLabelsOnInsert(double length, double pt)
{ {
for (unsigned int i=0;i<mLabels.GetCount();i++) { for (unsigned int i = 0; i < mLabels.size(); i++) {
auto &labelStruct = *mLabels[i]; auto &labelStruct = *mLabels[i];
LabelStruct::TimeRelations relation = LabelStruct::TimeRelations relation =
labelStruct.RegionRelation(pt, pt, this); labelStruct.RegionRelation(pt, pt, this);
@ -227,7 +227,7 @@ void LabelTrack::ShiftLabelsOnInsert(double length, double pt)
void LabelTrack::ChangeLabelsOnReverse(double b, double e) void LabelTrack::ChangeLabelsOnReverse(double b, double e)
{ {
for (size_t i=0; i<mLabels.GetCount(); i++) { for (size_t i = 0; i < mLabels.size(); i++) {
auto &labelStruct = *mLabels[i]; auto &labelStruct = *mLabels[i];
if (labelStruct.RegionRelation(b, e, this) == if (labelStruct.RegionRelation(b, e, this) ==
LabelStruct::SURROUNDS_LABEL) LabelStruct::SURROUNDS_LABEL)
@ -243,7 +243,7 @@ void LabelTrack::ChangeLabelsOnReverse(double b, double e)
void LabelTrack::ScaleLabels(double b, double e, double change) void LabelTrack::ScaleLabels(double b, double e, double change)
{ {
for (unsigned int i=0;i<mLabels.GetCount();i++){ for (unsigned int i = 0; i < mLabels.size(); i++){
auto &labelStruct = *mLabels[i]; auto &labelStruct = *mLabels[i];
labelStruct.selectedRegion.setTimes( labelStruct.selectedRegion.setTimes(
AdjustTimeStampOnScale(labelStruct.getT0(), b, e, change), AdjustTimeStampOnScale(labelStruct.getT0(), b, e, change),
@ -271,7 +271,7 @@ double LabelTrack::AdjustTimeStampOnScale(double t, double b, double e, double c
// (If necessary this could be optimised by ignoring labels that occur before a // (If necessary this could be optimised by ignoring labels that occur before a
// specified time, as in most cases they don't need to move.) // specified time, as in most cases they don't need to move.)
void LabelTrack::WarpLabels(const TimeWarper &warper) { void LabelTrack::WarpLabels(const TimeWarper &warper) {
for (int i = 0; i < (int)mLabels.GetCount(); ++i) { for (int i = 0; i < (int)mLabels.size(); ++i) {
auto &labelStruct = *mLabels[i]; auto &labelStruct = *mLabels[i];
labelStruct.selectedRegion.setTimes( labelStruct.selectedRegion.setTimes(
warper.Warp(labelStruct.getT0()), warper.Warp(labelStruct.getT0()),
@ -490,7 +490,7 @@ void LabelTrack::ComputeLayout(const wxRect & r, const ZoomInfo &zoomInfo) const
} }
int nRowsUsed=0; int nRowsUsed=0;
for (i = 0; i < (int)mLabels.Count(); i++) for (i = 0; i < (int)mLabels.size(); i++)
{ {
auto &labelStruct = *mLabels[i]; auto &labelStruct = *mLabels[i];
const int x = zoomInfo.TimeToPosition(labelStruct.getT0(), r.x); const int x = zoomInfo.TimeToPosition(labelStruct.getT0(), r.x);
@ -792,7 +792,7 @@ void LabelTrack::Draw(wxDC & dc, const wxRect & r,
// Get the text widths. // Get the text widths.
// TODO: Make more efficient by only re-computing when a // TODO: Make more efficient by only re-computing when a
// text label title changes. // text label title changes.
for (int i = 0; i < (int)mLabels.Count(); i++) for (int i = 0; i < (int)mLabels.size(); i++)
{ {
auto &labelStruct = *mLabels[i]; auto &labelStruct = *mLabels[i];
dc.GetTextExtent(labelStruct.title, &textWidth, &textHeight); dc.GetTextExtent(labelStruct.title, &textWidth, &textHeight);
@ -812,7 +812,7 @@ void LabelTrack::Draw(wxDC & dc, const wxRect & r,
dc.SetBackgroundMode(wxTRANSPARENT); dc.SetBackgroundMode(wxTRANSPARENT);
dc.SetBrush(AColor::labelTextNormalBrush); dc.SetBrush(AColor::labelTextNormalBrush);
dc.SetPen(AColor::labelSurroundPen); dc.SetPen(AColor::labelSurroundPen);
const int nLabels = (int)mLabels.Count(); const int nLabels = (int)mLabels.size();
int GlyphLeft; int GlyphLeft;
int GlyphRight; int GlyphRight;
// Now we draw the various items in this order, // Now we draw the various items in this order,
@ -1096,9 +1096,7 @@ double LabelTrack::GetOffset() const
double LabelTrack::GetStartTime() const double LabelTrack::GetStartTime() const
{ {
int len = mLabels.Count(); if (mLabels.empty())
if (len == 0)
return 0.0; return 0.0;
else else
return mLabels[0]->getT0(); return mLabels[0]->getT0();
@ -1108,8 +1106,8 @@ double LabelTrack::GetEndTime() const
{ {
//we need to scan through all the labels, because the last //we need to scan through all the labels, because the last
//label might not have the right-most end (if there is overlap). //label might not have the right-most end (if there is overlap).
int len = mLabels.Count(); int len = mLabels.size();
if (len == 0) if (mLabels.empty())
return 0.0; return 0.0;
double end = 0.0; double end = 0.0;
@ -1159,7 +1157,7 @@ int LabelTrack::OverGlyph(int x, int y)
mMouseOverLabelLeft = -1; mMouseOverLabelLeft = -1;
mMouseOverLabelRight = -1; mMouseOverLabelRight = -1;
mbHitCenter = false; mbHitCenter = false;
for (int i = 0; i < (int)mLabels.Count(); i++) for (int i = 0; i < (int)mLabels.size(); i++)
{ {
const auto &labelStruct = *mLabels[i]; const auto &labelStruct = *mLabels[i];
@ -1206,7 +1204,7 @@ int LabelTrack::OverGlyph(int x, int y)
int LabelTrack::OverATextBox(int xx, int yy) const int LabelTrack::OverATextBox(int xx, int yy) const
{ {
for (int nn = (int)mLabels.Count(); nn--;) { for (int nn = (int)mLabels.size(); nn--;) {
const auto &labelStruct = *mLabels[nn]; const auto &labelStruct = *mLabels[nn];
if (OverTextBox(&labelStruct, xx, yy)) if (OverTextBox(&labelStruct, xx, yy))
return nn; return nn;
@ -1828,7 +1826,7 @@ bool LabelTrack::OnKeyDown(SelectedRegion &newSel, wxKeyEvent & event)
mSelIndex++; mSelIndex++;
} }
if (mSelIndex >= 0 && mSelIndex < (int)mLabels.Count()) { if (mSelIndex >= 0 && mSelIndex < (int)mLabels.size()) {
LabelStruct &newLabel = *mLabels[mSelIndex]; LabelStruct &newLabel = *mLabels[mSelIndex];
mCurrentCursorPos = newLabel.title.Length(); mCurrentCursorPos = newLabel.title.Length();
//Set the selection region to be equal to the selection bounds of the tabbed-to label. //Set the selection region to be equal to the selection bounds of the tabbed-to label.
@ -1858,8 +1856,8 @@ bool LabelTrack::OnKeyDown(SelectedRegion &newSel, wxKeyEvent & event)
case WXK_TAB: case WXK_TAB:
case WXK_NUMPAD_TAB: case WXK_NUMPAD_TAB:
if (!mLabels.IsEmpty()) { if (!mLabels.empty()) {
int len = (int) mLabels.Count(); int len = (int) mLabels.size();
if (event.ShiftDown()) { if (event.ShiftDown()) {
mSelIndex = len - 1; mSelIndex = len - 1;
if (newSel.t0() > mLabels[0]->getT0()) { if (newSel.t0() > mLabels[0]->getT0()) {
@ -2104,14 +2102,14 @@ void LabelTrack::Unselect()
bool LabelTrack::IsSelected() const bool LabelTrack::IsSelected() const
{ {
return (mSelIndex >= 0 && mSelIndex < (int)mLabels.Count()); return (mSelIndex >= 0 && mSelIndex < (int)mLabels.size());
} }
/// Export labels including label start and end-times. /// Export labels including label start and end-times.
void LabelTrack::Export(wxTextFile & f) const void LabelTrack::Export(wxTextFile & f) const
{ {
// PRL: to do: export other selection fields // PRL: to do: export other selection fields
for (int i = 0; i < (int)mLabels.Count(); i++) { for (int i = 0; i < (int)mLabels.size(); i++) {
const auto &labelStruct = *mLabels[i]; const auto &labelStruct = *mLabels[i];
f.AddLine(wxString::Format(wxT("%f\t%f\t%s"), f.AddLine(wxString::Format(wxT("%f\t%f\t%s"),
(double)labelStruct.getT0(), (double)labelStruct.getT0(),
@ -2132,8 +2130,8 @@ void LabelTrack::Import(wxTextFile & in)
lines = in.GetLineCount(); lines = in.GetLineCount();
mLabels.Clear(); mLabels.clear();
mLabels.Alloc(lines); mLabels.reserve(lines);
//Currently, we expect a tag file to have two values and a label //Currently, we expect a tag file to have two values and a label
//on each line. If the second token is not a number, we treat //on each line. If the second token is not a number, we treat
@ -2266,8 +2264,8 @@ bool LabelTrack::HandleXMLTag(const wxChar *tag, const wxChar **attrs)
wxLogWarning(wxT("Project shows negative number of labels: %d"), nValue); wxLogWarning(wxT("Project shows negative number of labels: %d"), nValue);
return false; return false;
} }
mLabels.Clear(); mLabels.clear();
mLabels.Alloc(nValue); mLabels.reserve(nValue);
} }
else if (!wxStrcmp(attr, wxT("height")) && else if (!wxStrcmp(attr, wxT("height")) &&
XMLValueChecker::IsGoodInt(strValue) && strValue.ToLong(&nValue)) XMLValueChecker::IsGoodInt(strValue) && strValue.ToLong(&nValue))
@ -2296,7 +2294,7 @@ XMLTagHandler *LabelTrack::HandleXMLChild(const wxChar *tag)
void LabelTrack::WriteXML(XMLWriter &xmlFile) void LabelTrack::WriteXML(XMLWriter &xmlFile)
{ {
int len = mLabels.Count(); int len = mLabels.size();
int i; int i;
xmlFile.StartTag(wxT("labeltrack")); xmlFile.StartTag(wxT("labeltrack"));
@ -2330,10 +2328,10 @@ bool LabelTrack::Load(wxTextFile * in, DirManager * dirManager)
return false; return false;
unsigned int i; unsigned int i;
for (i = 0; i < mLabels.Count(); i++) for (i = 0; i < mLabels.size(); i++)
delete mLabels[i]; delete mLabels[i];
mLabels.Clear(); mLabels.clear();
mLabels.Alloc(len); mLabels.reserve(len);
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
LabelStruct *l = new LabelStruct(); LabelStruct *l = new LabelStruct();
@ -2357,7 +2355,7 @@ bool LabelTrack::Load(wxTextFile * in, DirManager * dirManager)
bool LabelTrack::Save(wxTextFile * out, bool overwrite) bool LabelTrack::Save(wxTextFile * out, bool overwrite)
{ {
out->AddLine(wxT("NumMLabels")); out->AddLine(wxT("NumMLabels"));
int len = mLabels.Count(); int len = mLabels.size();
out->AddLine(wxString::Format(wxT("%d"), len)); out->AddLine(wxString::Format(wxT("%d"), len));
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
@ -2401,7 +2399,7 @@ Track::Holder LabelTrack::Copy(double t0, double t1) const
{ {
auto tmp = std::make_unique<LabelTrack>(GetDirManager()); auto tmp = std::make_unique<LabelTrack>(GetDirManager());
const auto lt = static_cast<LabelTrack*>(tmp.get()); const auto lt = static_cast<LabelTrack*>(tmp.get());
int len = mLabels.Count(); int len = mLabels.size();
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
const LabelStruct &labelStruct = *mLabels[i]; const LabelStruct &labelStruct = *mLabels[i];
@ -2449,14 +2447,14 @@ bool LabelTrack::PasteOver(double t, const Track * src)
if (src->GetKind() != Track::Label) if (src->GetKind() != Track::Label)
return false; return false;
int len = mLabels.Count(); int len = mLabels.size();
int pos = 0; int pos = 0;
while (pos < len && mLabels[pos]->getT0() < t) while (pos < len && mLabels[pos]->getT0() < t)
pos++; pos++;
LabelTrack *sl = (LabelTrack *) src; LabelTrack *sl = (LabelTrack *) src;
for (unsigned int j = 0; j < sl->mLabels.Count(); j++) { for (unsigned int j = 0; j < sl->mLabels.size(); j++) {
const LabelStruct &labelStruct = *sl->mLabels[j]; const LabelStruct &labelStruct = *sl->mLabels[j];
LabelStruct *l = LabelStruct *l =
new LabelStruct(labelStruct.selectedRegion, new LabelStruct(labelStruct.selectedRegion,
@ -2495,7 +2493,7 @@ bool LabelTrack::Repeat(double t0, double t1, int n)
// Insert space for the repetitions // Insert space for the repetitions
ShiftLabelsOnInsert(tLen * n, t1); ShiftLabelsOnInsert(tLen * n, t1);
for (unsigned int i = 0; i < mLabels.GetCount(); i++) for (unsigned int i = 0; i < mLabels.size(); i++)
{ {
LabelStruct::TimeRelations relation = LabelStruct::TimeRelations relation =
mLabels[i]->RegionRelation(t0, t1, this); mLabels[i]->RegionRelation(t0, t1, this);
@ -2515,10 +2513,10 @@ bool LabelTrack::Repeat(double t0, double t1, int n)
label.title); label.title);
// Figure out where to insert // Figure out where to insert
while (pos < mLabels.Count() && while (pos < mLabels.size() &&
mLabels[pos]->getT0() < l->getT0()) mLabels[pos]->getT0() < l->getT0())
pos++; pos++;
mLabels.Insert(l, pos); mLabels.insert(mLabels.begin() + pos, l);
} }
} }
else if (relation == LabelStruct::BEGINS_IN_LABEL) else if (relation == LabelStruct::BEGINS_IN_LABEL)
@ -2536,7 +2534,7 @@ bool LabelTrack::Repeat(double t0, double t1, int n)
bool LabelTrack::Silence(double t0, double t1) bool LabelTrack::Silence(double t0, double t1)
{ {
int len = mLabels.Count(); int len = mLabels.size();
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
LabelStruct::TimeRelations relation = LabelStruct::TimeRelations relation =
@ -2553,7 +2551,7 @@ bool LabelTrack::Silence(double t0, double t1)
// This might not be the right place to insert, but we sort at the end // This might not be the right place to insert, but we sort at the end
++i; ++i;
mLabels.Insert(l, i); mLabels.insert(mLabels.begin() + i, l);
} }
else if (relation == LabelStruct::ENDS_IN_LABEL) else if (relation == LabelStruct::ENDS_IN_LABEL)
{ {
@ -2580,7 +2578,7 @@ bool LabelTrack::Silence(double t0, double t1)
bool LabelTrack::InsertSilence(double t, double len) bool LabelTrack::InsertSilence(double t, double len)
{ {
int numLabels = mLabels.Count(); int numLabels = mLabels.size();
for (int i = 0; i < numLabels; i++) { for (int i = 0; i < numLabels; i++) {
auto &labelStruct = *mLabels[i]; auto &labelStruct = *mLabels[i];
@ -2599,7 +2597,7 @@ bool LabelTrack::InsertSilence(double t, double len)
int LabelTrack::GetNumLabels() const int LabelTrack::GetNumLabels() const
{ {
return mLabels.Count(); return mLabels.size();
} }
const LabelStruct *LabelTrack::GetLabel(int index) const const LabelStruct *LabelTrack::GetLabel(int index) const
@ -2611,7 +2609,7 @@ int LabelTrack::GetLabelIndex(double t, double t1)
{ {
LabelStruct *l; LabelStruct *l;
int len = mLabels.Count(); int len = mLabels.size();
int i; int i;
//We'd have liked to have times in terms of samples, //We'd have liked to have times in terms of samples,
//because then we're doing an intrger comparison. //because then we're doing an intrger comparison.
@ -2638,13 +2636,13 @@ int LabelTrack::AddLabel(const SelectedRegion &selectedRegion,
LabelStruct *l = new LabelStruct(selectedRegion, title); LabelStruct *l = new LabelStruct(selectedRegion, title);
mInitialCursorPos = mCurrentCursorPos = title.length(); mInitialCursorPos = mCurrentCursorPos = title.length();
int len = mLabels.Count(); int len = mLabels.size();
int pos = 0; int pos = 0;
while (pos < len && mLabels[pos]->getT0() < selectedRegion.t0()) while (pos < len && mLabels[pos]->getT0() < selectedRegion.t0())
pos++; pos++;
mLabels.Insert(l, pos); mLabels.insert(mLabels.begin() + pos, l);
mSelIndex = pos; mSelIndex = pos;
@ -2666,9 +2664,9 @@ int LabelTrack::AddLabel(const SelectedRegion &selectedRegion,
void LabelTrack::DeleteLabel(int index) void LabelTrack::DeleteLabel(int index)
{ {
wxASSERT((index < (int)mLabels.GetCount())); wxASSERT((index < (int)mLabels.size()));
delete mLabels[index]; delete mLabels[index];
mLabels.RemoveAt(index); mLabels.erase(mLabels.begin() + index);
// IF we've deleted the selected label // IF we've deleted the selected label
// THEN set no label selected. // THEN set no label selected.
if( mSelIndex== index ) if( mSelIndex== index )
@ -2876,7 +2874,7 @@ wxString LabelTrack::GetTextOfLabels(double t0, double t1) const
bool firstLabel = true; bool firstLabel = true;
wxString retVal; wxString retVal;
for (unsigned int i=0; i < mLabels.GetCount(); ++i) for (unsigned int i=0; i < mLabels.size(); ++i)
{ {
auto &labelStruct = *mLabels[i]; auto &labelStruct = *mLabels[i];
if (labelStruct.getT0() >= t0 && if (labelStruct.getT0() >= t0 &&