Showing posts with label stl. Show all posts
Showing posts with label stl. Show all posts

Friday, October 15, 2010

bitsets

The STLs Bitsets are a great way to get flexible and expandable masking and flag logic without using using uints. They print nicely, mask correctly and are very readable compared to inline masking logic.

//complie with g++
#include <iostream>
#include <bitset>

using namespace std;

int main()
{
    const int count=50;
    bitset<count> bits;
    uint32_t      mask;

    bits = 0;
    mask = 0;

    cout << "All off" << endl;
    cout << bits << endl;
    cout << hex << mask << endl;

    //setting
    for(int i=0; i<count; i+=3)
    {
        bits[i] = true;        
        mask |= 1<<i;
    }

    cout << "Every 3rd on" << endl;
    cout << bits << endl;
    cout << hex << mask << endl;

    //masking test
    cout << "Self invert with ^" << endl;
    cout << (bits ^ bits) << endl;
    cout << hex << (mask ^ mask) << endl;

    cout << "Self mask with &" << endl;
    cout << (bits & bits) << endl;
    cout << hex << (mask & mask) << endl;

    //turn on all
    bits = 0; bits.flip();
    mask = -1;

    cout << "All on" << endl;
    cout << bits << endl;
    cout << hex << mask << endl;

    bits = -1; bits.flip();

    cout << "when bits are: " << bits << endl;
    cout << "check any on:  " << (bits != 0) << endl;
    cout << "check any off: " << (~bits != 0) << endl;

    bits = 0;

    cout << "when bits are: " << bits << endl;
    cout << "check any on:  " << (bits != 0) << endl;
    cout << "check any off: " << (~bits != 0) << endl;

    bits.flip();

    cout << "when bits are: " << bits << endl;
    cout << "check any on:  " << (bits != 0) << endl;
    cout << "check any off: " << (~bits != 0) << endl;

    return 0;
}

This outputs the following
All off
00000000000000000000000000000000000000000000000000
0
Every 3rd on
01001001001001001001001001001001001001001001001001
4925b6db
Self invert with ^
00000000000000000000000000000000000000000000000000
0
Self mask with &
01001001001001001001001001001001001001001001001001
4925b6db
All on
11111111111111111111111111111111111111111111111111
ffffffff
when bits are: 11111111111111111100000000000000000000000000000000
check any on:  1
check any off: 1
when bits are: 00000000000000000000000000000000000000000000000000
check any on:  0
check any off: 1
when bits are: 11111111111111111111111111111111111111111111111111
check any on:  1
check any off: 0

Sunday, June 13, 2010

SLT algorithm basics

STL algorithms are very powerful. They have 2 fundamental components.

The Algorithms:
  • These are the outer flow of control functions that direct the action of the operator classes. In general the internals are some form of for loop that passes over a series of STL compatible iterators and performs an action.
  • A list of them is here: http://www.cplusplus.com/reference/algorithm/

The Function/Operator classes
  • These are a class that implement the operator() overloader to provide the listed functionally.
  • Within this group of objects are an important subgroup of wrapper operators. These wrappers are designed to expand or covert the abilities of the other operators and to adapt existing function and class members for use with the STL algorithms.
  • A list of them is here: http://www.cplusplus.com/reference/std/functional/

I think I need to break this down and approach it in smaller chunks and apologies for the template heavy code. It cut down my typing.
#include <iostream>
#include <list>
#include <string>

#include <algorithm>
#include <functional>
#include <iterator>
using namespace std;

class RandN
{
  int n;
public:
  RandN(const int _n) { n = _n; }

  int operator() () const { return rand()%n; }
};

void readInAFIle(string filename)
{
  vector<int> values;
  ifstream file (filename);
  if (file)
    copy(istream_iterator<string> (file), istream_iterator<string>(), back_inserter (values));
}

template<class itor>
itor filterToThreshold(itor start, itor end, int threshold)
{
  //move to the end if... why call did they call it "remove"...
  return remove_if(start, end, bind2nd(greater<int>(), threshold));
}

template<class itor>
bool checkIfPresent(itor start, itor end, int value)
{
  return find(start, end, value) != end;
}

template<class itor>
void createRandSamples(itor start, int size, int limit)
{
  generate_n(start, size, RandN(limit));  
}

template<class itor>
void printOut(itor start, itor end)
{
  // *copy* the data into the output stream
  copy (start, end, ostream_iterator<int>(cout, " ")); 
  cout << endl;
}

template<class itor> 
void doDemo(itor start, itor end, string kind)
{
  cout << endl;
  cout << "Working on a " << kind << "..." << endl;
  printOut(start, end);
  cout << (checkIfPresent(start, end, 5) ? "there is a 5" : "No 5's") << endl;
  
  itor splitPoint = filterToThreshold(start, end, 5);
  int count = distance(start, splitPoint);
  cout << kind << " was filtered to a count of " << count << endl;
  printOut(start, splitPoint);
}

int main()
{
  const int SIZE = 20;
  list<int> valuesList;
  int valuesArray[SIZE];

  //create a rand list of stuff...
  srand(time(NULL));
  createRandSamples(valuesArray, SIZE, 10);
  createRandSamples(back_inserter(valuesList), SIZE, 10);

  doDemo(valuesArray, valuesArray + SIZE, "Array");
  doDemo(valuesList.begin(), valuesList.end(), "List");
}