CLAM-Development
1.1
|
00001 /* 00002 * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG) 00003 * UNIVERSITAT POMPEU FABRA 00004 * 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 * 00020 */ 00021 00022 #include "Fundamental.hxx" 00023 #include "ProcessingDataPlugin.hxx" 00024 00025 00026 namespace CLAM 00027 { 00028 00029 namespace Hidden 00030 { 00031 static ProcessingDataPlugin::Registrator<Fundamental> dataRegistrator("sandybrown"); 00032 } 00034 // 00035 // Fundamental 00036 // 00038 00039 void Fundamental::DefaultInit() 00040 { 00041 //Initializing minimum set of attributes 00042 AddCandidatesFreq(); 00043 AddCandidatesErr(); 00044 UpdateData(); 00045 00046 //Default values 00047 SetnMaxCandidates(1); 00048 SetnCandidates(0); 00049 } 00050 00051 00052 TData Fundamental::GetFreq(TIndex pos) const //inefficient Get, for efficiency work directly on the buffer 00053 { 00054 00055 CLAM_DEBUG_ASSERT(HasCandidatesFreq(),"Fundamental::GetFreq(): Fundamental not initialized"); 00056 if (pos < GetnCandidates()) 00057 { 00058 return GetCandidatesFreq()[pos]; 00059 } 00060 else 00061 { 00062 return 0.f;//if no candidates were found, 0 is used as the default non valid value 00063 } 00064 } 00065 00066 TData Fundamental::GetErr(TIndex pos) const //inefficient Get, for efficiency work directly on the buffer 00067 { 00068 00069 CLAM_DEBUG_ASSERT(HasCandidatesFreq(),"Fundamental::GetErr(): Fundamental not initialized"); 00070 return GetCandidatesErr()[pos]; 00071 } 00072 00073 void Fundamental::SetFreq(TIndex pos,TData freq)//inefficient Set, for efficiency work directly on the buffer 00074 { 00075 00076 CLAM_DEBUG_ASSERT(HasCandidatesFreq(),"Fundamental::SetFreq(): Fundamental not initialized"); 00077 CLAM_DEBUG_ASSERT(pos<GetnCandidates(),"Fundamental::SetFreq(): You are trying to set a candidate that does not exist"); 00078 GetCandidatesFreq()[pos]=freq; 00079 } 00080 00081 void Fundamental::SetErr(TIndex pos,TData err)//inefficient Set, for efficiency work directly on the buffer 00082 { 00083 00084 CLAM_DEBUG_ASSERT(HasCandidatesFreq(),"Fundamental::SetErr(): Fundamental not initialized"); 00085 CLAM_DEBUG_ASSERT(pos<GetnCandidates(),"Fundamental::SetFreq(): You are trying to set a candidate that does not exist"); 00086 GetCandidatesErr()[pos]=err; 00087 } 00088 00089 00090 void Fundamental::AddElem (TData freq, TData err) 00091 { 00092 CLAM_DEBUG_ASSERT(HasCandidatesFreq(),"Fundamental::AddElem(): Fundamental not initialized"); 00093 // MRJ: Does this assert make sense any longer after Merlijn changes? 00094 //CLAM_ASSERT(GetnCandidates()<GetnMaxCandidates(), 00095 // "Fundamental::AddElem(): Number of Candidates exceeds maximum"); 00096 GetCandidatesFreq().AddElem(freq); 00097 GetCandidatesErr().AddElem(err); 00098 // SetnCandidates(GetnCandidates()+1); 00099 } 00100 00101 void Fundamental::InsertElem (TIndex pos, TData freq, TData err) 00102 { 00103 CLAM_DEBUG_ASSERT(HasCandidatesFreq(),"Fundamental::InsertElem(): Fundamental not initialized"); 00104 CLAM_ASSERT(GetnCandidates()<GetnMaxCandidates(), 00105 "Fundamental::InsertElem(): Number of Candidates exceeds maximum"); 00106 GetCandidatesFreq().InsertElem(pos,freq); 00107 GetCandidatesErr().InsertElem(pos,err); 00108 // SetnCandidates(GetnCandidates()+1); 00109 } 00110 00111 void Fundamental::DeleteElem (TIndex pos) 00112 { 00113 CLAM_DEBUG_ASSERT(HasCandidatesFreq(),"Fundamental::DeleteElem(): Fundamental not initialized"); 00114 CLAM_ASSERT(pos<GetnCandidates()&&pos>=0, 00115 "Fundamental::DeleteElem(): Wrong index, element does not exist"); 00116 GetCandidatesFreq().DeleteElem(pos); 00117 GetCandidatesErr().DeleteElem(pos); 00118 // SetnCandidates(GetnCandidates()-1); 00119 } 00120 00121 void Fundamental::SortByFrequency() 00122 { 00123 // TODO: Is a bubble sort, not so eficient O(N^2) 00124 // TODO: Enhancement 1: Do a Quick Sort 00125 // TODO: Engancement 2: Sort an index array 00126 DataArray & errors = GetCandidatesErr(); 00127 DataArray & freqs = GetCandidatesFreq(); 00128 const int nCandidates = GetnCandidates(); 00129 for (int i=0; i<nCandidates; i++) // Ordering 00130 for (int j=i+1; j<nCandidates; j++) 00131 { 00132 if (freqs[i] <= freqs[j]) continue; 00133 std::swap(errors[i],errors[j]); 00134 std::swap(freqs[i],freqs[j]); 00135 } 00136 } 00137 00138 void Fundamental::SortByError() 00139 { 00140 // TODO: Is a bubble sort, not so eficient O(N^2) 00141 // TODO: Enhancement 1: Do a Quick Sort 00142 // TODO: Engancement 2: Sort an index array 00143 DataArray & errors = GetCandidatesErr(); 00144 DataArray & freqs = GetCandidatesFreq(); 00145 const int nCandidates = GetnCandidates(); 00146 for (int i=0; i<nCandidates; i++) // Ordering 00147 for (int j=i+1; j<nCandidates; j++) 00148 { 00149 if (errors[i] <= errors[j]) continue; 00150 std::swap(errors[i],errors[j]); 00151 std::swap(freqs[i],freqs[j]); 00152 } 00153 } 00154 00155 } 00156