changeset 456:0ef029232213 api-inversion

Baby steps with index.cpp audioDB::index_make_shingle uses almost no shared state. Make it use none at all, and then remove it from the audioDB class.
author mas01cr
date Wed, 24 Dec 2008 10:57:27 +0000
parents 93ce12fe2f76
children 823bca1e10f5
files audioDB.h index.cpp
diffstat 2 files changed, 74 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/audioDB.h	Wed Dec 24 10:57:23 2008 +0000
+++ b/audioDB.h	Wed Dec 24 10:57:27 2008 +0000
@@ -375,7 +375,6 @@
 
 
   // LSH vector<> containers for one in-core copy of a set of feature vectors
-  vector<float>::iterator vi; // feature vector iterator
   vector<vector<float> > *vv;  // one-track's worth data
 
   // LSH indexing and retrieval methods  
@@ -384,7 +383,6 @@
   void index_insert_tracks(Uns32T start_track, Uns32T end_track, double** fvpp, double** sNormpp,double** snPtrp, double** sPowerp, double** spPtrp);
   int index_insert_track(Uns32T trackID, double** fvpp, double** snpp, double** sppp);
   Uns32T index_insert_shingles(vector<vector<float> >*, Uns32T trackID, double* spp);
-  void index_make_shingle(vector<vector<float> >*, Uns32T idx, double* fvp, Uns32T dim, Uns32T seqLen);
   int index_norm_shingles(vector<vector<float> >*, double* snp, double* spp);
   int index_query_loop(adb_t *adb, adb_query_spec_t *spec);
   vector<vector<float> >* index_initialize_shingles(Uns32T sz);
--- a/index.cpp	Wed Dec 24 10:57:23 2008 +0000
+++ b/index.cpp	Wed Dec 24 10:57:27 2008 +0000
@@ -122,6 +122,78 @@
 }
 
 
+/********************* LSH shingle construction ***************************/
+
+// Construct shingles out of a feature matrix
+// inputs:
+// idx is vector index in feature matrix
+// fvp is base feature matrix pointer double* [numVecs x dbH->dim]
+//
+// pre-conditions: 
+// dbH->dim 
+// sequenceLength
+// idx < numVectors - sequenceLength + 1
+//
+// post-conditions:
+// (*vv)[idx] contains a shingle with dbH->dim*sequenceLength float values
+
+static void audiodb_index_make_shingle(vector<vector<float> >* vv, Uns32T idx, double* fvp, Uns32T dim, Uns32T seqLen){
+  assert(idx<(*vv).size());
+  vector<float>::iterator ve = (*vv)[idx].end();
+  vector<float>::iterator vi = (*vv)[idx].begin();
+  // First feature vector in shingle
+  if(idx == 0) {
+    while(vi!=ve) {
+      *vi++ = (float)(*fvp++);
+    }
+  } else {
+    // Not first feature vector in shingle
+    vector<float>::iterator ui=(*vv)[idx-1].begin() + dim;
+    // Previous seqLen-1 dim-vectors
+    while(vi!=ve-dim) {
+      *vi++ = *ui++;
+    }
+    // Move data pointer to next feature vector
+    fvp += ( seqLen + idx - 1 ) * dim ;
+    // New d-vector
+    while(vi!=ve) {
+      *vi++ = (float)(*fvp++);
+    }
+  }
+}
+
+// norm shingles
+// in-place norming, no deletions
+// If using power, return number of shingles above power threshold
+int audioDB::index_norm_shingles(vector<vector<float> >* vv, double* snp, double* spp){  
+  int z = 0; // number of above-threshold shingles
+  float l2norm;
+  double power;
+  float oneOverRadius = 1./(float)sqrt(radius); // Passed radius is really radius^2
+  float oneOverSqrtl2NormDivRad = oneOverRadius;
+  if(!spp)
+    error("LSH indexing and query requires a power feature using -w or -W");
+  Uns32T shingleSize = sequenceLength*dbH->dim;
+  for(Uns32T a=0; a<(*vv).size(); a++){
+    l2norm = (float)(*snp++);
+    if(audioDB::normalizedDistance)
+      oneOverSqrtl2NormDivRad = (1./l2norm)*oneOverRadius;
+    
+    for(Uns32T b=0; b < shingleSize ; b++)
+      (*vv)[a][b]*=oneOverSqrtl2NormDivRad;
+
+    power = *spp++;
+    if(use_absolute_threshold){
+      if ( power >= absolute_threshold )
+	z++;
+    }
+    else
+      z++;	
+  }
+  return z;
+}
+  
+
 /************************ LSH indexing ***********************************/
 void audioDB::index_index_db(const char* dbName){
   char* newIndexName;
@@ -380,7 +452,7 @@
     vv = index_initialize_shingles(numVecs);
     
     for( Uns32T pointID = 0 ; pointID < numVecs; pointID++ )
-      index_make_shingle(vv, pointID, *fvpp, dbH->dim, sequenceLength);
+      audiodb_index_make_shingle(vv, pointID, *fvpp, dbH->dim, sequenceLength);
     
     numVecsAboveThreshold = index_norm_shingles(vv, *snpp, *sppp);
     collisionCount = index_insert_shingles(vv, trackID, *sppp);
@@ -417,76 +489,6 @@
   return collisionCount;
 }
 
-/********************* LSH shingle construction ***************************/
-
-// Construct shingles out of a feature matrix
-// inputs:
-// idx is vector index in feature matrix
-// fvp is base feature matrix pointer double* [numVecs x dbH->dim]
-//
-// pre-conditions: 
-// dbH->dim 
-// sequenceLength
-// idx < numVectors - sequenceLength + 1
-//
-// post-conditions:
-// (*vv)[idx] contains a shingle with dbH->dim*sequenceLength float values
-
-void audioDB::index_make_shingle(vector<vector<float> >* vv, Uns32T idx, double* fvp, Uns32T dim, Uns32T seqLen){
-  assert(idx<(*vv).size());
-  vector<float>::iterator ve = (*vv)[idx].end();
-  vi=(*vv)[idx].begin();        // shingle iterator  
-  // First feature vector in shingle
-  if(idx==0){
-    while(vi!=ve)
-      *vi++ = (float)(*fvp++);
-  }
-  // Not first feature vector in shingle
-  else{
-    vector<float>::iterator ui=(*vv)[idx-1].begin() + dim; // previous shingle iterator
-    // Previous seqLen-1 dim-vectors
-    while(vi!=ve-dim)
-      *vi++=*ui++;
-    // Move data pointer to next feature vector
-    fvp += ( seqLen + idx - 1 ) * dim ;
-    // New d-vector
-    while(vi!=ve)
-      *vi++ = (float)(*fvp++);
-  }
-}
-
-// norm shingles
-// in-place norming, no deletions
-// If using power, return number of shingles above power threshold
-int audioDB::index_norm_shingles(vector<vector<float> >* vv, double* snp, double* spp){  
-  int z = 0; // number of above-threshold shingles
-  float l2norm;
-  double power;
-  float oneOverRadius = 1./(float)sqrt(radius); // Passed radius is really radius^2
-  float oneOverSqrtl2NormDivRad = oneOverRadius;
-  if(!spp)
-    error("LSH indexing and query requires a power feature using -w or -W");
-  Uns32T shingleSize = sequenceLength*dbH->dim;
-  for(Uns32T a=0; a<(*vv).size(); a++){
-    l2norm = (float)(*snp++);
-    if(audioDB::normalizedDistance)
-      oneOverSqrtl2NormDivRad = (1./l2norm)*oneOverRadius;
-    
-    for(Uns32T b=0; b < shingleSize ; b++)
-      (*vv)[a][b]*=oneOverSqrtl2NormDivRad;
-
-    power = *spp++;
-    if(use_absolute_threshold){
-      if ( power >= absolute_threshold )
-	z++;
-    }
-    else
-      z++;	
-  }
-  return z;
-}
-  
-
 /*********************** LSH retrieval ****************************/
 
 
@@ -609,7 +611,7 @@
 
   // Construct shingles from query features  
   for( Uns32T pointID = 0 ; pointID < Nq ; pointID++ ) 
-    index_make_shingle(vv, pointID, query, dbH->dim, sequenceLength);
+    audiodb_index_make_shingle(vv, pointID, query, dbH->dim, sequenceLength);
   
   // Normalize query vectors
   Uns32T numVecsAboveThreshold = index_norm_shingles( vv, qpointers.l2norm, qpointers.power );