CLAM-Development  1.4.0
Fundamental.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "Fundamental.hxx"
23 #include "ProcessingDataPlugin.hxx"
24 
25 
26 namespace CLAM
27 {
28 
29  namespace Hidden
30  {
31  static ProcessingDataPlugin::Registrator<Fundamental> dataRegistrator("sandybrown");
32  }
34  //
35  // Fundamental
36  //
38 
40  {
41  //Initializing minimum set of attributes
42  AddCandidatesFreq();
43  AddCandidatesErr();
44  UpdateData();
45 
46  //Default values
48  SetnCandidates(0);
49  }
50 
51 
52  TData Fundamental::GetFreq(TIndex pos) const //inefficient Get, for efficiency work directly on the buffer
53  {
54 
55  CLAM_DEBUG_ASSERT(HasCandidatesFreq(),"Fundamental::GetFreq(): Fundamental not initialized");
56  if (pos < GetnCandidates())
57  {
58  return GetCandidatesFreq()[pos];
59  }
60  else
61  {
62  return 0.f;//if no candidates were found, 0 is used as the default non valid value
63  }
64  }
65 
66  TData Fundamental::GetErr(TIndex pos) const //inefficient Get, for efficiency work directly on the buffer
67  {
68 
69  CLAM_DEBUG_ASSERT(HasCandidatesFreq(),"Fundamental::GetErr(): Fundamental not initialized");
70  return GetCandidatesErr()[pos];
71  }
72 
73  void Fundamental::SetFreq(TIndex pos,TData freq)//inefficient Set, for efficiency work directly on the buffer
74  {
75 
76  CLAM_DEBUG_ASSERT(HasCandidatesFreq(),"Fundamental::SetFreq(): Fundamental not initialized");
77  CLAM_DEBUG_ASSERT(pos<GetnCandidates(),"Fundamental::SetFreq(): You are trying to set a candidate that does not exist");
78  GetCandidatesFreq()[pos]=freq;
79  }
80 
81  void Fundamental::SetErr(TIndex pos,TData err)//inefficient Set, for efficiency work directly on the buffer
82  {
83 
84  CLAM_DEBUG_ASSERT(HasCandidatesFreq(),"Fundamental::SetErr(): Fundamental not initialized");
85  CLAM_DEBUG_ASSERT(pos<GetnCandidates(),"Fundamental::SetFreq(): You are trying to set a candidate that does not exist");
86  GetCandidatesErr()[pos]=err;
87  }
88 
89 
90  void Fundamental::AddElem (TData freq, TData err)
91  {
92  CLAM_DEBUG_ASSERT(HasCandidatesFreq(),"Fundamental::AddElem(): Fundamental not initialized");
93  // MRJ: Does this assert make sense any longer after Merlijn changes?
94  //CLAM_ASSERT(GetnCandidates()<GetnMaxCandidates(),
95  // "Fundamental::AddElem(): Number of Candidates exceeds maximum");
96  GetCandidatesFreq().AddElem(freq);
97  GetCandidatesErr().AddElem(err);
98  // SetnCandidates(GetnCandidates()+1);
99  }
100 
102  {
103  CLAM_DEBUG_ASSERT(HasCandidatesFreq(),"Fundamental::InsertElem(): Fundamental not initialized");
105  "Fundamental::InsertElem(): Number of Candidates exceeds maximum");
106  GetCandidatesFreq().InsertElem(pos,freq);
107  GetCandidatesErr().InsertElem(pos,err);
108  // SetnCandidates(GetnCandidates()+1);
109  }
110 
112  {
113  CLAM_DEBUG_ASSERT(HasCandidatesFreq(),"Fundamental::DeleteElem(): Fundamental not initialized");
114  CLAM_ASSERT(pos<GetnCandidates()&&pos>=0,
115  "Fundamental::DeleteElem(): Wrong index, element does not exist");
116  GetCandidatesFreq().DeleteElem(pos);
117  GetCandidatesErr().DeleteElem(pos);
118  // SetnCandidates(GetnCandidates()-1);
119  }
120 
122  {
123  // TODO: Is a bubble sort, not so eficient O(N^2)
124  // TODO: Enhancement 1: Do a Quick Sort
125  // TODO: Engancement 2: Sort an index array
126  DataArray & errors = GetCandidatesErr();
127  DataArray & freqs = GetCandidatesFreq();
128  const int nCandidates = GetnCandidates();
129  for (int i=0; i<nCandidates; i++) // Ordering
130  for (int j=i+1; j<nCandidates; j++)
131  {
132  if (freqs[i] <= freqs[j]) continue;
133  std::swap(errors[i],errors[j]);
134  std::swap(freqs[i],freqs[j]);
135  }
136  }
137 
139  {
140  // TODO: Is a bubble sort, not so eficient O(N^2)
141  // TODO: Enhancement 1: Do a Quick Sort
142  // TODO: Engancement 2: Sort an index array
143  DataArray & errors = GetCandidatesErr();
144  DataArray & freqs = GetCandidatesFreq();
145  const int nCandidates = GetnCandidates();
146  for (int i=0; i<nCandidates; i++) // Ordering
147  for (int j=i+1; j<nCandidates; j++)
148  {
149  if (errors[i] <= errors[j]) continue;
150  std::swap(errors[i],errors[j]);
151  std::swap(freqs[i],freqs[j]);
152  }
153  }
154 
155 }
156