//  Example 10.5, page 230
//  Schaum's Outline of Fundamental of Computing with C++
//  by John R. Hubbard
//  Copyright McGraw-Hill, 1998

#include <iostream>
#include <vector>
#include <cassert>

/* A vector contains a sequence of elements in contiguous memory with direct access.
 * As an ADT, it generalizes the basic array type.  The C++ Standard Library defines the vector<T>
 * class template in the <vector> file.  Its class interface is the prototype for all the Standard
 * container class templates.  With few exceptions, for each member function of the vector class
 * there is an equivalent member function for each of the other container classes (stack, queue,
 * list, set, map, etc.).
 *
 * The following program uses the vector<T> class template.
 */
using namespace std;

typedef vector<double> vec;
typedef vector<bool> bits;

template <class T>
void copy(vector<T>& v, const T* x, int n) {
    vector<T> w;
    for (int i=0; i<n; i++)
        w.push_back(x[i]);
    v = w;
}
/* The purpose of the projection(v,b) function is to use the bit vector b
 * as a mask to remove selected elements of the vector v.  The resulting
 * vector w is called the projection fo v onto the subspace determined by b.
 */
vec projection(vec v, bits& b) {
    vec::iterator iv = v.begin();
    bits::iterator ib = b.begin();
    while (iv != v.end()) {
        if(*ib) {
            cout << "\n*ib = " << *ib << " so erase " << *iv;
            v.erase(iv);
        }
        else ++iv;
        ++ib;
    }
    cout << endl;
    return v;
}

//vec projection(vec& v, bits& b) {
//    int v_size = v.size();
//    assert(b.size() >= v_size);
//    vec w;
//    for (int i=0; i<v_size; i++)
//        if (b[i]) w.push_back(v[i]);
//    return w;
//}

void print(vec& v) {
    int v_size = v.size();
    for (int i=0; i<v_size; i++)
        cout << v[i] << "  ";
    cout << endl;
}

int freq(SL)

int main() {
    double x[8] = { 22.2, 33.3, 44.4, 55.5, 66.6, 77.7, 88.8, 99.9 };
    vec v;
    copy(v, x, 8);
    bool y[8] = { false, true, false, true, true, true, false, true };
    bits b;
    copy(b, y, 8);
    cout << "\nb=";
    for(int i = 0; i<8;++i) cout << b.at(i) << " ";
    cout << endl;
    vec w = projection(v, b);
    print(v);
    print(w);
}
