automatic commit
[folded-ctf.git] / pi_feature_family.cc
diff --git a/pi_feature_family.cc b/pi_feature_family.cc
new file mode 100644 (file)
index 0000000..ba3a35e
--- /dev/null
@@ -0,0 +1,70 @@
+
+///////////////////////////////////////////////////////////////////////////
+// This program is free software: you can redistribute it and/or modify  //
+// it under the terms of the version 3 of the GNU General Public License //
+// as published by the Free Software Foundation.                         //
+//                                                                       //
+// This program is distributed in the hope that it will be useful, but   //
+// WITHOUT ANY WARRANTY; without even the implied warranty of            //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      //
+// General Public License for more details.                              //
+//                                                                       //
+// You should have received a copy of the GNU General Public License     //
+// along with this program. If not, see <http://www.gnu.org/licenses/>.  //
+//                                                                       //
+// Written by Francois Fleuret, (C) IDIAP                                //
+// Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
+///////////////////////////////////////////////////////////////////////////
+
+#include "pi_feature_family.h"
+
+PiFeatureFamily::PiFeatureFamily() {
+  _nb_features = 0;
+  _pi_features = 0;
+}
+
+PiFeatureFamily::~PiFeatureFamily() {
+  delete[] _pi_features;
+}
+
+void PiFeatureFamily::read(istream *is) {
+  delete[] _pi_features;
+  read_var(is, &_nb_features);
+  _pi_features = new PiFeature[_nb_features];
+  is->read((char *) _pi_features, sizeof(PiFeature) * _nb_features);
+}
+
+void PiFeatureFamily::write(ostream *os) {
+  write_var(os, &_nb_features);
+  os->write((char *) _pi_features, sizeof(PiFeature) * _nb_features);
+}
+
+void PiFeatureFamily::resize(int nb_features) {
+  delete[] _pi_features;
+  _nb_features = nb_features;
+  _pi_features = new PiFeature[_nb_features];
+}
+
+void PiFeatureFamily::randomize(int level) {
+  for(int f = 0; f < _nb_features; f++) _pi_features[f].randomize(level);
+}
+
+void PiFeatureFamily::extract(PiFeatureFamily *pi_feature_family,
+                              bool *used_features, int *new_feature_indexes) {
+  delete[] _pi_features;
+  _nb_features = 0;
+
+  for(int f = 0; f < pi_feature_family->nb_features(); f++)
+    if(used_features[f]) _nb_features++;
+
+  _pi_features = new PiFeature[_nb_features];
+
+  int g = 0;
+
+  for(int f = 0; f < pi_feature_family->nb_features(); f++)
+    if(used_features[f]) {
+      _pi_features[g] = pi_feature_family->_pi_features[f];
+      new_feature_indexes[f] = g;
+      g++;
+    } else new_feature_indexes[f] = -1;
+}