Thursday, March 26, 2009

c++ stl - use transform to copy keys from map to set

//Author: Darius Kucinskas (c) 2008-2009
//Email: d[dot]kucinskas[eta]gmail[dot]com
//Blog: http://blog-of-darius.blogspot.com/
//License: GPL

#include <iostream>
#include <map>
#include <set>
#include <iterator>

typedef std::map<std::string, int> MyMap;

// also known as select1st in SGI STL implementation
template<typename T_PAIR>
struct GetKey: public std::unary_function<T_PAIR, typename T_PAIR::first_type>
{
   const typename T_PAIR::first_type& operator()(const T_PAIR& item) const
   {
      return item.first;
   }
};

int main(int argc, char** argv)
{
   MyMap m1,m2;

   m1["a"] = 1;
   m1["b"] = 2;
   m2["c"] = 3;
   m2["b"] = 3;

   std::set<std::string> s;
   std::transform(m1.begin(), m1.end(), std::inserter(s, s.begin()), GetKey<MyMap::value_type>());
   std::transform(m2.begin(), m2.end(), std::inserter(s, s.begin()), GetKey<MyMap::value_type>());
   std::copy(s.begin(), s.end(), std::ostream_iterator<std::string>(std::cout, " "));
   std::cout << std::endl;
   return 0;
}

Friday, March 20, 2009

c++ stl - fast way to check if map contains value

//Author: Darius Kucinskas (c) 2008-2009
//Email: d[dot]kucinskas[eta]gmail[dot]com
//Blog: http://blog-of-darius.blogspot.com/
//License: GPL

#include <iostream>
#include <map>
#include <string>
#include <functional>

typedef std::pair<std::string, int> MyPair;
typedef std::map<std::string, int> TestMap;

struct MapFind : public std::binary_function< MyPair, int, bool >
{
     bool operator()(const MyPair& p, int value) const
     {
         return p.second == value;
     }
};

int main(int argc, char** argv)
{
    TestMap testMap;
    testMap["a"] = 1;
    testMap["b"] = 2;
    testMap["c"] = 3;

    TestMap::iterator it;
    it = std::find_if(testMap.begin(), testMap.end(), std::bind2nd(MapFind(), 2));
    if (it != testMap.end())
    {
        std::cout  << " Found ! " 
                    << it->first << " " 
                    << it->second << std::endl;
    }
    else
    {
        std::cout << " Not Found ! " << std::endl;
    }

    return 0;
}

The kernel gets a new mascot

The kernel gets a new mascot

Thursday, March 19, 2009

Wednesday, March 18, 2009

Releasing early and often

I think it's always a good idea to learn from someone else mistakes

Mikrosoft :D ?

c++ stl - check if vector contains item

std::find is solution!

Whenever I want to check if vector contains satin item my preferred way is to use std::find.
//Author: Darius Kucinskas (c) 2008-2009
//Email: d[dot]kucinskas[eta]gmail[dot]com
//Blog: http://blog-of-darius.blogspot.com/
//License: GPL

#include <algorithm>
#include <vector>
#include <iostream>
#include <iterator>
#include <functional>

class Person
{
public:
    Person();
    Person(int a, std::string n);
    Person(const Person& src);
    
    bool Person::operator==(const Person& src);

    int age;
    std::string name;
};

Person::Person()
: age(0)
, name("")
{};

Person::Person(int a, std::string n)
:age(a)
,name(n)
{};

Person::Person(const Person& src)
{
    age = src.age;
    name = src.name;
};

bool Person::operator==(const Person& src)
{
    if (name != src.name) return false;
    if (age != src.age) return false;
    return true;
}

int main(int count, char** args)
{
std::vector<Person> personVec;
Person p1(10, "Person1");
Person p2(12, "Person2");
Person p3(12, "Person3");

personVec.push_back(p1);
personVec.push_back(p2);
personVec.push_back(p3);

// use of find
Person f(12, "Person2"), f2(13, "Person1000");
std::vector<Person>::iterator it;

std::cout << "Find Person2" << std::endl;
it = std::find(personVec.begin(), personVec.end(), f);

if (it == personVec.end()){
    std::cout << " Person not found! " << std::endl;
}
std::cout << " Found person: " << it->name 
           << ":" << it->age 
           << " vector index: " << it - personVec.begin()
           << std::endl;

std::cout << "Find Person1000" << std::endl;
it = std::find(personVec.begin(), personVec.end(), f2);

if (it == personVec.end()){
    std::cout << " Person not found! " << std::endl;
}
std::cout << " Found person: " << it->name 
           << ":"<< it->age 
           << " vector index: " << it - personVec.begin()
           << std::endl;
 
}

Friday, March 13, 2009

Oracle : make column null able without dropping table

ALTER TABLE RULE
  MODIFY (WORKFLOW NULL)  

Oracle : create table with auto increment primary key

How you can create auto increment primary key in Oracle? Answer is you will need sequence and trigger. Example:
CREATE TABLE RULE(
  ID NUMBER(10) NOT NULL,
  WORKFLOW NVARCHAR2(255),
  STATE NVARCHAR2(255) NOT NULL,
  OPERATION NVARCHAR2(255) NOT NULL,
  
  CONSTRAINT RULE_PK PRIMARY KEY (ID),
)
/

CREATE SEQUENCE RULE_SEQ
START WITH 1 
INCREMENT BY 1
NOCACHE;
/

CREATE OR REPLACE TRIGGER RULE_SEQ_TRI
  BEFORE INSERT ON RULE
  FOR EACH ROW
BEGIN
  SELECT RULE_SEQ.NEXTVAL
  INTO   :NEW.ID
  FROM   DUAL;
END RULE_SEQ_TRI;
/

Thursday, March 12, 2009

strlen implementation

I was reading Hacker news and found one very interesting web page link: Glibc's strlen implementation: Probably not what you'd guess. Out of curiosity I have compared Glibc’s strlen implementation with two most simple implementation’s. Here are benchmark results (my4bytes_strlen is still so far away from Glibc’s strlen in term of performance): code:
size_t my_strlen(const char* str)
{
    const char* ptr = str;
    
    size_t size = 0;
    while(*ptr != '\0')
    {
        ptr++, size++;
    }
    return size;
}
    
size_t my4bytes_strlen(const char* str)
{
    const int32_t* ptr = (int32_t*)str;
    size_t size = 0;
    for (;;){
        if (!(*ptr & 0x000000ff)) return size;
        if (!(*ptr & 0x0000ff00)) return size + 1;
        if (!(*ptr & 0x00ff0000)) return size + 2;
        if (!(*ptr & 0xff000000)) return size + 3;

        size += 4;
        ptr++;
    }
}

int main(char* argv[], int argc)
{
    const char* str = "test this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long stringtest this long string";

    Timer t1, t2, t3, t4;
    
    t1.start();
    size_t len = my_strlen(str);
    t1.stop();

    t2.start();
    size_t len1 = glibc_strlen(str);
    t2.stop();
    
    t3.start();
    size_t len3 = strlen(str);
    t3.stop();

    t4.start();
    size_t len4 = my4bytes_strlen(str);
    t4.stop();

    std::cout << " my_strlen size: " << len << " time: " << t1.getElapsedTimeInMilliSec() << std::endl
        << " my4bytes_strlen size: " << len4 << " time: " << t4.getElapsedTimeInMilliSec() << std::endl
        << " glibc size:  " << len1 << " time: " << t2.getElapsedTimeInMilliSec() << std::endl
        << " strlen:  " << len3 << " time: " << t3.getElapsedTimeInMilliSec() << std::endl;

return 0;
}
Time count utilities from http://www.songho.ca/misc/timer/timer.html

Friday, March 6, 2009

c++ stl - assigns value to every element of collection with fill

This time just a little tip about std::fill
//Author: Darius Kucinskas (c) 2008-2009
//Email: d[dot]kucinskas[eta]gmail[dot]com
//Blog: http://blog-of-darius.blogspot.com/
//License: GPL

#include <iostream>
#include <vector>
#include <iterator>

int main(){
    std::vector<int> vec;
    vec.resize(100);


    std::fill(vec.begin(), vec.end(), 123);
    std::copy(vec.begin(), vec.end(), 
      std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;
return 0; 
}

Tuesday, March 3, 2009

c++ stl - for_each, custom function and custom sort

//Author: Darius Kucinskas (c) 2008-2009
//Email: d[dot]kucinskas[eta]gmail[dot]com
//Blog: http://blog-of-darius.blogspot.com/
//License: GPL

#include <algorithm>
#include <vector>
#include <iostream>
#include <iterator>
#include <functional>

class Person
{
public:
  Person();
  Person(int a, std::string n);
  Person(const Person& src);

  int age;
  std::string name;
};

Person::Person()
: age(0)
, name("")
{};

Person::Person(int a, std::string n)
: age(a)
, name(n)
{};

Person::Person(const Person& src)
{
  age = src.age;
  name = src.name;
};

struct PersonPrint : public std::unary_function<Person, void>
{
  void operator() (Person p)
  {
    std::cout << " Person age: " << p.age << " name: " << p.name << std::endl;
  }
};

struct PersonGreater : public std::binary_function<Person, Person, bool>
{
  bool operator()(const Person& p1, const Person p2)
  {
    if (p1.age > p2.age) return true;
    if (p1.name.compare(p2.name) > 0) return true;
    return false;
  }
};

int main(int count, char** args)
{
  // for_each and custom unary function
  std::vector<Person> personVec;
  Person p1(10, "Person1");
  Person p2(12, "Person2");
  Person p3(12, "Person3");

  personVec.push_back(p1);
  personVec.push_back(p2);
  personVec.push_back(p3);
  
  std::cout << "before sort: " << std::endl;
  std::for_each(personVec.begin(), personVec.end(), PersonPrint());
  std::sort(personVec.begin(), personVec.end(), PersonGreater());
  std::cout << "after: " << std::endl;
  std::for_each(personVec.begin(), personVec.end(), PersonPrint());
}

Qt 4.5, Qt Creator Has Been Released

Read about it here!

c++ stl - std vector sort (reverse sort)

Example how to sort vector in revers order (C++)
//Author: Darius Kucinskas (c) 2008-2009
//Email: d[dot]kucinskas[eta]gmail[dot]com
//Blog: http://blog-of-darius.blogspot.com/
//License: GPL

#include <algorithm>
#include <vector>
#include <iostream>
#include <iterator>
#include <functional>

int main(int count, char** args)
{
  std::vector<int> v1;
  for(int i=0; i < 10; i++)
  {
    v1.push_back(i);
  }
  
  //sort
  std::cout << "sort" << std::endl;
  std::sort(v1.begin(), v1.end(), std::greater<int>());
  std::copy(v1.begin(), v1.end(), std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl;
  
  return 0; 
}
UPDATE Read here how you can sort vector with lambdas in c++0x.

Monday, March 2, 2009