lundi 1 juillet 2013

Don't forget to 'EncodeURL'

In  http://tinyurl.com/qa3nmtv I presented a small script/exe I made to quickly convert selected path (folder or file) from Explorer to a string in the clipboardwhich can be 'paste' in a browser editor as an URL to a local resssource.

In fact I forgot something important in my script. You may hate or love it but Windows allow 'spaces' in path. So I have to convert SPACE to %20, to build a correct URL.

I updated my script this morning:

https://github.com/alexbuisson/Project/tree/master/UsefullScript

Just pull Path2URL.exe, and configure your StExBar like in the following Snapshot.





Visual Studio 2008Randomly crashing.


If you are using VS'08 and a lot of plugin you may experience some random crash when using "Intellisense" functionality (performing a right click in the source code) or when switching from Debug to Release configuration.

 I may be really difficult  for some to figure from where and for why the crash/freeze occur.

In fact I meet that issue and I finally attached another Visual Studio instance with a debugger and realize that VS was hanging somewhere in the FEACP.DLL

That DLL is the engine of Intellisense.

If you have that kind of problem and that you want double check or experiment VS'08 without Intellisense running behind, you just have to rename that dll in your VS'08 setup folder and restart VS.

It's work fine but without Intellisense, you really need additional plugin to provide, some source browsing capabilities ("Find all references", etc...)


Build a script/tool to make your life easier.

Hi,
Just wanted to share some work done to simplify the everyday life of my teammates.

One week ago, one of my colleague had a lot of file path to share through our CRM system and in fact he found that was very boring because he had to copy all network path and apply a transformation on every string. To transform network file or folder path from their UNC or Drive path (mounted drive) representation to a file/folder URL style that can be use to access local system storage through a browser.

To do that he had to 'manually' switch all '\' (windows path separator) to '/' and concatenate 'file://'. I understood it could become a pain to do that kind of boring task, so I developed a simple script/executable to do that for one or multiple selected file or folder with only 1 click from Windows Explorer.

My Teammates and I already used StExBar to improve our Windows Explorer experience. It's a highly customizable tools in which you can add your own custom command, with button and/or hot-key.


As the function we need has only to run under windows, I choose a windows only tool called AutoHotKey to build a script performing multiple path selection, conversion to URL style and injection into the clipboard.

AutoHotKey provide every things I needed to develop the script:
  • File parsing 
    • StExBar selection (single or multiple) are store in a temp text file that can be pass to any cmd or application through "%selafile"
  • Clipboard
    • From AHK (AutoHotKey) point of view the winows clipboard is just a variable.
  • String parsing, Regex Matching etc...
    • To detect Drive letter
    • To detect UNC Path
    • To replace '\' and concatenate the 'file://' prefix.


link := "file:///"
pathrep := ""

CR := Chr(13)
LF := Chr(10)

GetUNCPath(letter)
{
  localName = %letter%
  length := 1000
  VarSetCapacity(remoteName, length)
  result := DllCall("Mpr\WNetGetConnection"
      , "Str", localName
      , "Str", remoteName
      , "UInt *", length)
  if (ErrorLevel <> 0 || result != 0)
  {
     return ""
  }
  else
  {
;     MsgBox, %remoteName% (%length%) -- result: %result%
    return remotename ; end function
  }
}

Loop Read, %1%
{
 p := A_LoopReadLine
 ;MsgBox %p%
  if ( RegExMatch( p , "[A-Za-z][:]") ) ; match a regex for drive letter "^:"
  {
    unc := GetUNCPath( SubStr( p, 1, 2) )
    if ( unc <> "" )
    {
      withoutDriveLetter := SubStr( p, 3, strLen(p)-2 )
      pathrep = %link%%unc%%withoutDriveLetter%%CR%%LF%%pathrep%
    }
  }
  else ; should already be an unc (check to be sure)
  {
    if ( RegExMatch( p , "\\\\") ) 
    {
      pathrep = %link%%p%%CR%%LF%%pathrep%
    }
    else
    {
      ; Msgbox "ignored " %p%
    }
  }
   
}

StringReplace, pathrep, pathrep, \, /, All
; MsgBox %pathrep% 
clipboard =  %pathrep% 

The main advantage of using AHK script is that it can be build and provide as a binary file without any dependency. And the integration of the resulting executable as an StExBar Command is really simple like you can see in the following snapshot:


That script is publicly available on my GitHub repo



mardi 18 juin 2013

About the importance of unit-test defintion

Last week, a friend and I submitted some piece of code to solve an on-line coding challenge. It was a 'french' challenge but I will try to explain the problem and present the data vector used to test submitted function. We will show that their data vector definition is incorrect and doesn't detect if a function can really solve to problem !

First, the problem:

As developer and team member of a High-Tech Company, your manager ask you to register and join the maximum of development and tech-event you can.
So, you list all the incoming event and some are in the same time.
But you still need to be in the "maximum" of event.

So nothing magic... you cannot be in 2 different place the same day.
For each event, you only know when it start and its duration (days).

Second, the test data they use:

They propose 3 different test to check the function output:



Simple

2013-01-12;5
2013-01-01;13
2013-01-16;20

function should return 3





Complex

2014-04-28;4
2014-05-01;7
2014-05-03;7
2014-05-09;5
2014-05-11;11
2014-05-16;3
2014-05-20;1

function should return 4




With a leap year

2012-02-20;10
2012-03-01;2
2012-03-12;2
2012-03-14;2
2012-03-15;2
2012-03-20;13

function should return 5


And now where the defined test failed ...

My co-worker and I had 2 different favorite language (Python for him and C++ for me) and 2 different solution.
In fact he use a real exhaustive exploration using the 'permutation' function to find the 'maximum number of event', but it lead to a high complexity if the number of event grows. On my side, I preferred a linear complexity and I used a common 'greedy' solver. But in fact case, it's well known that it can not find the 'optimal' solution.

My simple C++ implementation use an t_event class define by:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class event_t 
{
public:
 event_t(const std::string& _date)
 {
  memset(&_start_date, 0, sizeof(struct tm));

  sscanf(_date.c_str(), "%d-%d-%d;%d", &_start_date.tm_year, &_start_date.tm_mon, &_start_date.tm_mday, &_duration);
  _start_date.tm_year -= 1900;
  _start_date.tm_mon -= 1;

  _start_pt = mktime(&_start_date);
  _end_pt = _start_pt + _duration * 24 * 60 * 60;

  memset(&_end_date, 0, sizeof(struct tm));
  memcpy(&_end_date, gmtime(&_end_pt), sizeof(struct tm));
 }

 event_t(const event_t& e)
 {
  _duration = e._duration;
  _start_date = e._start_date;
  _start_pt = e._start_pt;
  _end_pt = e._end_pt;
  _end_date = e._end_date;
 }

 bool operator < (const event_t& r)
 {
  return this->_end_pt < r._end_pt;
 }

 friend std::ostream& operator<< (std::ostream& stream, const event_t& evt);

public:
  struct tm _start_date;
  unsigned int _duration;
  struct tm _end_date;

  time_t _start_pt;
  time_t  _end_pt;
} ;

And use the following function to build a planning:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void CreatePlanning( std::vector<event_t> &_list )
{
 std::ostream_iterator< event_t > dump_events( std::cout, "" );

 std::sort(_list.begin(), _list.end()); 
 std::cout << " Start \t End \t Duration \n", 
  std::copy(_list.begin(),_list.end(), dump_events);

 std::vector<event_t> _planning;
 _planning.push_back(_list[0]);
 unsigned int j = 0;
 for (unsigned int i = 1 ; i < _list.size() ; ++i)
 {
  if(_list[i]._start_pt >= _list[j]._end_pt)
  { 
   _planning.push_back(_list[i]);
   j = i;
  }
 }

 std::cout << "Nb event planned : " << _planning.size() << std::endl;
 std::cout << " Start \t End \t Duration \n", 
  std::copy(_planning.begin(),_planning.end(), dump_events);
}

That code is ok regarding the expected result of the unit-test, but as i said before the price for the lower complexity is that it can lead to a no optimal solution.
The heuristic I use can be summarize, start ASAP and restart ASAP again and again ... Simple and efficient, I'm a greedy person !!!

mardi 28 mai 2013

Const-Correctness .... with example.

Some of my friend have trouble with the terrible "const" keyword this morning
They have a class 'test', an instance of that class and they want to pass it through a function as 'const test*' parameter.

The question was: what 'const classname*' means ? and by the way what is the difference with 'const classname* const' ?

To answer, let create a 'test' class defined with 'const' and not const function:


class test
{
public:
test(){}
~test(){}

int DoConstOp() const { return 0;}
int DoNotConstOp() { return 0;}

};


And now we create different version of a method call 'process_blahblah' in which we will call the 2 test's function. Those method will take a object of type 'test' but with 4 different signature:

  • const test&
  • const test*
  • const test* const
  • test* const

And call the 4 method from a main:

process_as_const_ref(t);
process_as_const_ptr(&t);
process_as_const_ptr_const(&t);
process_as_ptr_const(&t);


And now have a look at their implementation. I put under comment the lines that doesn't compile (under VS'12 at least... but result looks correct regarding the C++ standard !) + error message.



void process_as_const_ref(const test& t)
{
t.DoConstOp();
//t.DoNotConstOp();//Cannot build: cannot convert 'this' pointer from 'const test' to 'test &'
}

void process_as_const_ptr(const test* t)
{
t->DoConstOp();
//t->DoNotConstOp(); //Cannot build: cannot convert 'this' pointer from 'const test' to 'test &'
(const_cast<test*>(t))->DoNotConstOp(); //trick
}

void process_as_const_ptr_const(const test* const t)
{
t->DoConstOp();
//t->DoNotConstOp();//Cannot build: cannot convert 'this' pointer from 'const test' to 'test &'
(const_cast<test* const>(t))->DoNotConstOp(); //trick remove the const on the class not on pointer
//t++;//but this you can't do : 't' : you cannot assign to a variable that is const
}

void process_as_ptr_const(test* const t)
{
t->DoConstOp();
t->DoNotConstOp();
//t++;//but this you can't do : 't' : you cannot assign to a variable that is const
}


So the answer is:
  • const test& or const test* declare the the test instance you use as const, thats why you cannot non-const method.
  • and with const test* const or test* const, the 2nd const keyword declare the pointer as const, it means that you cannot do any modification of the pointer value (ptr++ ptr = another ptr ...etc ...).
Note that using the "test* const" syntax may be confusing and useless as by default the pointer you get is a "copy" of the pointer you have in the caller !


vendredi 24 mai 2013

HTML/CSS for fixed background image and a nice scrolling effect

As I was looking some news about the next XBox on http://www.wired.com/gadgetlab/2013/05/xbox-one/ I found the web page design and scrolling effect really nice. And I'm wondering if that kind of effect required JS or if it only use CSS. Note that I'm not a web developer, but just as usual curious of "how it works ?" ...

In fact after inspecting the page (thank you to the Firefox Inspector ! a nice functionality) I found that a small piece of CSS is enough and than the main trick is in the size of the image !!!!

Look at the following image they use http://www.wired.com/images_blogs/gadgetlab/2013/05/0521-tall-controller-v1.jpg. It's a 1050x2000 pixels image.

Below I tested the insertion on their image (Thank you Microsoft ...) to do the same in the middle of a fake text ....


#container div.fixedbackgroundimg {
position: relative;
overflow: visible;
}
.fixedbackgroundimg.example {
background: url("http://www.wired.com/images_blogs/gadgetlab/2013/05/0521-tall-controller-v1.jpg") repeat-y fixed 50% center transparent !important;
}

<div class="fixedbackgroundimg example" data-type="background" data-speed="5">
<h2 class=""> test </h2>
<img src="http://www.wired.com/images_blogs/gadgetlab/2013/05/0521-tall-controller-v1.jpg" style="visibility: hidden; height: 500px" data-lazy-loaded="true"></img>
</div>


The trick is to create a div containing an hidden image, or with a fixed width and height, and in the style of that div, you specify a background image bigger than the div and centered inside !

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus




Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus


Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

jeudi 23 mai 2013

Mastermind : build the k-combination of n colors(with repetitions)

In my 'Mastermind serie', I wrote a small C++ code that i would template later to create the list of the 4-combination of 6 peg (with repetitions) as we already know that for the classic mastermind game:
  • we have 6 colors,
  • the code is 4 peg long,
  • so the total number of combination is 6^4 = 1296

To build that list, I apply the recursion/induction method and obtain finally a short and cool code :)
 Recall that our colors are describe by letters from 'A' to 'F'.


const color_code_map_t color_code = map_list_of (color_e::white, 'A')(color_e::blue, 'B')(color_e::green, 'C')(color_e::yellow, 'D')(color_e::orange, 'E')(color_e::red, 'F') ;


I build the list through the following code:

vector<char> elements;
std::for_each(color_code.begin(), color_code.end(), [&elements](const color_code_map_t::value_type& p) { elements.push_back(p.second); });
auto combi_found = combinations_with_repetitions(elements, TheHiddenCode.size());
std::cout << "total combination you can try = " << combi_found.size() << std::endl;



and in the 2 following function, I use the recursion/induction approach:


void combinations_with_repetitions_recursion(deque< vector<char> >& combi, const vector<char> &elems,
unsigned long req_len,
vector<unsigned long> &pos,
unsigned long depth)
{
// Have we selected the number of required elements?
if (depth >= req_len) {
vector<char> depth_r_combi;
for (unsigned long ii = 0; ii < pos.size(); ++ii){
depth_r_combi += elems[pos[ii]];
}
//copy(depth_r_combi.begin(), depth_r_combi.end(), ostream_iterator<char>(cout, " ")), cout << endl;
combi.push_back(depth_r_combi);
return;
}

// Try to select new elements to the right of the last selected one.
for (unsigned long ii = 0; ii < elems.size(); ++ii) {
pos[depth] = ii;
combinations_with_repetitions_recursion(combi, elems, req_len, pos, depth + 1);
}
return;
}

deque<vector<char>> combinations_with_repetitions(const vector<char> &elems, unsigned long r)
{
if(r > elems.size()){
throw std::logic_error("combination length (r) cannot be greater than the elements list (n)");
}

vector<unsigned long> positions(r, 0);
deque<vector<char>> combi;
combinations_with_repetitions_recursion(combi, elems, r, positions, 0);

return combi;
}


the 'depth' parameters is the key to stop the recursion and drill-up all the results. Based on those function we can easily create function to build permutations, combination(without repetitions), and ....

mercredi 15 mai 2013

Mastermind, a small evolution to map with the true rules!

In my previous post about the Mastermind game, I didn't implement the real rules and my daughter said that I should use 'W'hite & 'B'lack peg as in the real game to say if a color is correctly placed, misplaced and empty if there is nothing to say !

So I added a short description of the rules, color code, number of allowed try, etc ....


cout << "Code of " << TheHiddenCode.size() << " peg" << endl;
cout << "Number of try to guess " << nb_line << endl;
cout << "Color are ", for_each(color_code.begin(), color_code.end(), [](const color_t& c){cout << c.second << " " ;}), cout << endl;


In the main loop, the 'answer' is now a vector of char to store our 'W' and 'B' peg!...


while( exact < TheHiddenCode.size() && nb_try < nb_line) {
cout << "\n\nguess--> ", cin >> guess;
if(guess.size() == 4) {
exact = color = 0;
transform( begin(TheHiddenCode), end(TheHiddenCode),
begin(guess), begin(answer),
Count( TheHiddenCode, color, exact ) );
cout << "Answer => " << "Incorrect place " << color << " Correct place " << exact << " : " ,
copy(answer.begin(), answer.end(), ostream_iterator<char>(cout, " ")),
cout << endl;
nb_try++;
} else {
cout << "incorrect number peg on the line" << endl;
}
}


And finally I updated my 'Count' class to check if a peg in the guess, if well or misplaced ....


struct Count {
Count( hidden_code_t code, int& incorrect_place, int& exact )
: incorrect_place_(incorrect_place), exact_(exact=0), code_(code) { }
char operator()( color_t c, char g ){
bool correct_place = (c.second == toupper(g));
if(correct_place) { //Argh, conditionnal are ugly !
exact_++;
return 'W';
} else {
//does that color be somewhere else in the code...
auto f = std::find_if(begin(code_), end(code_), [&g](const color_t& tobecheck_) { return (tobecheck_.second == toupper(g));} );
auto misplaced = (f != code_.end());
incorrect_place_ += (int) (misplaced);
if(misplaced)
return 'B';
else
return ' ';
}

}
~Count(){
}

int &incorrect_place_, &exact_;
hidden_code_t code_;
};


Based on that work, I will present later an API and Async Engine.

vendredi 26 avril 2013

Mastermind: Search the code by yourself....

Hey good news after only 1hour more, i got a playable version with around 100 line of code....
I have more work to display the color code: A,B,C,D,E,F, to configure the program for a given number of try and to compute, but it's a correct improvement !


#include <string>
#include <map>
#include <algorithm>
#include <time.h>
#include <iostream>
#include <sstream>
using namespace std;

#include "boost/assign.hpp"
using namespace boost::assign;

enum color_e { white = 0, blue, green, yellow, orange, red };
typedef map<color_e, char> color_code_map_t;
typedef pair<color_e, char> color_t;
typedef vector<color_t> guess_code_t;
typedef vector<color_t> hidden_code_t;

const color_code_map_t color_code = map_list_of (color_e::white, 'A')(color_e::blue, 'B')(color_e::green, 'C')(color_e::yellow, 'D')(color_e::orange, 'E')(color_e::red, 'F') ;
const auto comb_length = 4;
const auto nb_line = 8;
hidden_code_t TheHiddenCode(comb_length);
vector<bool> answer(comb_length);
//how many of 4(k) peg can make from 6(N) ==> N^k
string guess;

struct acc_char {
void operator () (color_t c) {
acc_ += c.second;
}
string get() {return acc_;}
string acc_;
};

template<typename T> string colorCodeString(const T& m) {
bool is_vector_of_peg = std::is_same<T, guess_code_t>::value || std::is_same<T, hidden_code_t>::value ;
if(!is_vector_of_peg)
return ""; //or throw ?
acc_char acc;
return std::for_each(begin(m), end(m), acc).get();
}
color_t ChooseRandomPeg() { return *color_code.find((color_e)(rand() % color_code.size())); }

struct Count {
Count( int& incorrect_place, int& exact )
: incorrect_place_(incorrect_place), exact_(exact=0) { }
bool operator()( color_t c, char g ){
exact_ += c.second == toupper(g);
return (c.second == toupper(g));
}
~Count(){
}

int &incorrect_place_, &exact_;
};

class WinnerOrLoser {
public:
WinnerOrLoser(int exact, int code_length, int nb_try, int nb_line) : state_(false), msg_("not found") {
state_ = (exact == code_length);
stringstream builder;
if(state_) {
builder << "found in " << nb_try << "/" << nb_line;
msg_ = builder.str();
}
}
~WinnerOrLoser(){}
bool getState() {return state_;}
string getMsg() {return msg_;}
private:
bool state_;
string msg_;
};

int main() {

int color, exact = color = 0;

srand( time(0) ),
generate( TheHiddenCode.begin(), TheHiddenCode.end(), ChooseRandomPeg );

int nb_try = 0;
while( exact < TheHiddenCode.size() && nb_try < nb_line) {
cout << "\n\nguess--> ", cin >> guess;
if(guess.size() == 4) {
transform( begin(TheHiddenCode), end(TheHiddenCode),
begin(guess), begin(answer),
Count( color, exact ) );
cout << color << ' ' << exact << " : " ,
for_each(answer.begin(), answer.end(), [](bool pegExact){ cout << ((pegExact) ? "T" : "F") ;}),
cout << endl;
nb_try++;
} else {
cout << "incorrect number peg on the line" << endl;
}
}
cout << "Hidden code " << colorCodeString(TheHiddenCode) << " " << WinnerOrLoser(exact, TheHiddenCode.size(), nb_try, nb_line).getMsg() << "!\n";
}


Mastermind: a starting point...

As I start to build an algorithm to compete in an on-line Mastermind tournament, I build the following code to generate random hidden code of 4 peg from a list of 6 color (6^4 possibilities...).

The next step will be the interactive guess from a user (stdin), check to put the white and black mark, and finally the state win or lose.... (let see in future post)...


#include <string>
#include <map>
#include <algorithm>
#include <time.h>
#include <iostream>
using namespace std;

#include "boost/assign.hpp"
using namespace boost::assign;

enum color_e { white = 0, blue, green, yellow, orange, red };
typedef map<color_e, char> color_code_map_t;
typedef pair<color_e, char> color_t;
typedef vector<color_t> gess_code_t;
typedef vector<color_t> hidden_code_t;

const color_code_map_t color_code = map_list_of (color_e::white, 'A')(color_e::blue, 'B')(color_e::green, 'C')(color_e::yellow, 'D')(color_e::orange, 'E')(color_e::red, 'F') ;
const auto comb_length = 4;
const auto nb_line = 8;
hidden_code_t TheHiddenCode(comb_length);

struct acc_char {
void operator () (color_t c) {
acc_ += c.second;
}
string get() {return acc_;}
string acc_;
};

template<typename T> string colorCodeString(const T& m) {
bool is_vector_of_peg = std::is_same<T, gess_code_t>::value || std::is_same<T, hidden_code_t>::value ;
if(!is_vector_of_peg)
return ""; //or throw ?
acc_char acc;
return std::for_each(begin(m), end(m), acc).get();
}
color_t ChooseRandomPeg() { return *color_code.find((color_e)(rand() % color_code.size())); }



And a small main to check the random 'hidden code' generation:


int main() {
do
{
srand( time(0) ),
generate( TheHiddenCode.begin(), TheHiddenCode.end(), ChooseRandomPeg );
std::cout << colorCodeString(TheHiddenCode);
} while (getchar() != EOF);
return 0;
}

mercredi 24 avril 2013

Use Iterator to decouple your algorithm from your container...

Today, I had to implement a function to check error between data contains in different kind of container. Typically the data type can be int, float or double, store in std::vector or boost::numeric::ublas vector and matrix.

It's here that template and iterator pattern have to be use to decouple the algorithm/function implementation of the container implementation.

template<class InputIteratorT1, typename InputIteratorT2> void CheckEqualForGivenAccuracy( InputIteratorT1 first_left, InputIteratorT1 last_left, InputIteratorT2 first_right, InputIteratorT2 last_right, double tstEspilon = (double)std::numeric_limits<typename InputIteratorT1::value_type>::epsilon(), std::ostream& stream = null_ostream )

{

  CHECK(std::distance(first_left, last_left) && std::distance(first_right, last_right));

  stream << std::string(__FUNCTION__) << " of " << std::distance(first_left, last_left) << " elements with epsilon = " << tstEspilon << std::endl;

 

  unsigned int nb_error = 0;

  double sum_abs_diff = 0.0;

  double max_diff = 0.0;

  double min_diff = 0.0;

  while(first_left != last_left)

  {

    if(fabs((double)*first_left - (double)*first_right) > tstEspilon)

    {

      nb_error++;

      double diff = (double)*first_left - (double)*first_right;

      sum_abs_diff += std::abs<double>(diff);

      max_diff = std::max(max_diff, diff);

      min_diff = std::min(min_diff, diff);     

    }

    first_left++; first_right++;

  }

  if(nb_error > 0)

  {

    stream << std::string(__FUNCTION__) << " error= " << nb_error << " sum_abs_diff = " << sum_abs_diff << " max_diff = " << max_diff << " min_diff = " << min_diff << std::endl;

  }

 

  CHECK(nb_error == 0);

}


That implementation is range-based, the 2 data type can be different ((internally a promotion to double is made), and we have 2 optional parameters, one to specify the error limit and the second to log message. By default the error limit use the standard epsilon value from the numeric type_trait class, and the ostream my implementation of a /dev/null output.

mardi 23 avril 2013

I like Javascript .....

I'm not a Javascript expert, but the thing I really like with Javascript and our modern Web-Browser is that they allow you to customize your web exeprience....

Today the 'most important news' for me was that the 1st Thor 2 trailer was release, but while clicking on the link i had in my RSS feed, I got a "da.feedsportal.com" before going on my real target !

Hopefully there is link where you can click on the skip that 'ad'.
So now just let Javascript perform the click for you...

Using greasemonkey (Firefox & Chrome) and the following script, you can skip those 'ad' ...

// ==UserScript==
// @name        Skip feedsportal Add
// @namespace   http://da.feedsportal.com/*
// @match       http://da.feedsportal.com/*
// @grant       none
// @version     1
// ==/UserScript==
(function ()
{ 
    var articleLink = document.getElementsByTagName("a")[1];
    location.href = articleLink;
})();
// ==UserScript==
// @name        Skip feedsportal Add
// @namespace   http://da.feedsportal.com/*
// @match       http://da.feedsportal.com/*
// @grant       none
// @version     1
// ==/UserScript==
(function ()
{ 
    var articleLink = document.getElementsByTagName("a")[1];
    location.href = articleLink;
})();
// ==UserScript==
// @name        Skip feedsportal Add
// @namespace   http://da.feedsportal.com/*
// @match       http://da.feedsportal.com/*
// @grant       none
// @version     1
// ==/UserScript==
(function ()
{ 
    var articleLink = document.getElementsByTagName("a")[1];
    location.href = articleLink;
})();
 // ==UserScript==
// @name        Skip feedsportal Add
// @namespace   http://da.feedsportal.com/*
// @match       http://da.feedsportal.com/*
// @grant       none
// @version     1
// ==/UserScript==
(function ()
{
    var articleLink = document.getElementsByTagName("a")[1];
    location.href = articleLink;
})();

// ==UserScript==
// @name        Skip feedsportal Add
// @namespace   http://da.feedsportal.com/*
// @match       http://da.feedsportal.com/*
// @grant       none
// @version     1
// ==/UserScript==
(function ()
{ 
    var articleLink = document.getElementsByTagName("a")[1];
    location.href = articleLink;
})();
// ==UserScript==
// @name        Skip feedsportal Add
// @namespace   http://da.feedsportal.com/*
// @match       http://da.feedsportal.com/*
// @grant       none
// @version     1
// ==/UserScript==
(function ()
{ 
    var articleLink = document.getElementsByTagName("a")[1];
    location.href = articleLink;
})();
// ==UserScript==
// @name        Skip feedsportal Add
// @namespace   http://da.feedsportal.com/*
// @match       http://da.feedsportal.com/*
// @grant       none
// @version     1
// ==/UserScript==
(function ()
{ 
    var articleLink = document.getElementsByTagName("a")[1];
    location.href = articleLink;
})();

lundi 22 avril 2013

Provide your own allocator for std::vector or boost storage

If you plan to write some optimized code (SIMD code like SSE or MMX) using memory coming from a standard container (std::vector or boost::numeric::ublas::unbounded_array), your may be interested by the following.


#include <stdlib.h>//linux aligned malloc
#include <malloc.h>
#if defined(WIN32) || defined(WIN64)
#define simd_alloc(alignment, len) _aligned_malloc(len, alignment)
#define simd_free _aligned_free
#else
#define simd_alloc(alignment, len) memalign(alignment, len)
#define simd_free free
#endif

template<typename T>
class aligned_allocator : public std::allocator<T>
{
public:
void deallocate(pointer _Ptr, size_type)
{ // deallocate object at _Ptr, ignore size
simd_free(_Ptr);
}

pointer allocate(size_type _Count)
{ // allocate array of _Count elements
return (T*)simd_alloc(16, _Count*sizeof(T));
}
};



using that allocator class, you can create a vector like:


template struct myType
{
typedef boost::numeric::ublas::vector<T, boost::numeric::ublas::unbounded_array<T, aligned_allocator<T> > > boost_vector;
typedef std::vector<T, aligned_allocator<T> > std_vector;
};

vendredi 19 avril 2013

An interview question

I had an job interview, and the interviewer had a cool technical and language agnostic question.... He just put the focus on algorithms and their complexity.

The problem was: What is the original complexity of the following function, how to optimize it and what is the resulting complexity.

The function was a common box filter where each output pixel is the sum of the each input pixel around him in a KxK area. And just add some 'noise' in the reflexion he said that the 2-dimensional image was a WxH plane.

So let use some math notation to express that:

P '( x , y ) = i = k 2 k 2 j = k 2 k 2 P ( x + i , y + j )

Looking at that formula, the complexity is clearly O( k 2 ).

Now let express P' iteratively:
P '( x + 1 , y ) = i = k 2 k 2 j = k 2 k 2 P ( x + i , y + j ) + j = k 2 k 2 P ( x + 1 + k 2 , y + j ) j = k 2 k 2 P ( x k 2 , y + j )   
or ....
P '( x + 1 , y )   =   P '( x , y ) + j = k 2 k 2 P ( x + 1 + k 2 , y + j ) j = k 2 k 2 P ( x k 2 , y + j )   

So now the complexity is O(2k)! and if you use a k-vector store the sum of each column, and use that sliding window process, a correct implementation will be in O(k+1)




How to check if an executable is 32bit or 64bit

On windows, using dumpbin.exe what you should have in your PATH if you launch a Visual studio command line or if you run the vsvars.bat ("C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat")

>dumpbin.exe path_to_executable.exe /HEADERS

And on linux, just use file

>file path_to_executable

samedi 6 avril 2013

Google API is really easy to use ....

2 weeks ago i spend 1 or 2 hour looking how work the Google Map API (http://hpneo.github.io/gmaps/). The Example page provide helpful piece of code as below.
I put the map focus somewhere .... using latitude and longitude coordinate (48.13199070073399, -1.6228169202804565).







jeudi 31 janvier 2013

Debugging Tips: Exporting a bunch of memory without instrumenting thecode!

Context and problem:

In the last 3 weeks, I worked on aligning a C/C++ code with its Matlab equivalent. During that task I came across the following problem.... I had to compare the data and the program state from C/C++ and Matlab code during 2 parallel debugging session,. So how can we debug and save at any time a C++ vector as binary file without adding noisy instrumentation code?

1
2
3
4
std::vector<double> vec(10);
std::ofstream output("out.bin", std::ios::binary|std::ios::out);
output.write(reinterpret_cast<char*>(&vec[0]), vec.size()*sizeof(vec[0]));
output.close();


 And when the file is ready, write in Matlab (for example):
vec_from_c_code = read(open('out.bin'), 'double');
sum(abs(vec_from_c_code - vec_in_matlab))

It could be a debugger feature:

That's why as usual, I first googled it, but I didn't find something ready to use.....

I used Visual Studio 2008 IDE to develop and debug, and it's a really good tools, but it doesn't contain that feature. Using the "memory windows" developer can just see the program memory in hexadecimal and eventually copy a part of it as Text.

With WinDbg, you should have access to a ".writemem" function, and it looks like some extension for VS'2010 to support that syntax from the "Immediate console".... So if its your case, search on http://visualstudiogallery.msdn.microsoft.com/

In another hand Matlab come with an API to directly feed in realtime data from C/C++ (and maybe others languages) in a Matlab session for vizualisation, etc.... But it's something I would found in a production code.

So I build it by myself based on some informations found in the previous page, my own command line.

A solution:

The code below is a simple usage of the 2 function win32 API, OpenProcess and ReadProcessMemory (more details on MSDN). I call this tool a ProcessMemDumper....  evolutions of this code, using a Write function can be made to replace the memory of the program with data coming from file during a debugging session.
Using your debugger, your are going step-by-step through your program, you found for example a "std::vector vec;" that you want to see in another tools (Matlab in my case). You need 3 things, pickup from a simple debugger watch:
  • the offset in the process memory , &vec[0],
  • and the number of bytes, vec size * 8(size of double)
Also get the process PID, and  call from a command line:
  • processmemdumper.exe PID offset nbbytes  out.bin
 Really more efficient than adding export source code everywhere....

Source code:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// ProcessMemDumper.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <windows.h>

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <stdexcept>
#include <sstream>

#include "boost/shared_ptr.hpp"

void delete_HANDLE(HANDLE p) 
{ 
  std::cout << "Releasing Handle ...." << std::endl;
  CloseHandle(p);  
};


int _tmain(int argc, _TCHAR* argv[])
{
 try
 {
  unsigned int procPID = atoi(argv[1]);
  std::string procAddressHexa = std::string(argv[2]);
  std::stringstream hexToInt;
  hexToInt << std::hex << procAddressHexa;
  unsigned int procAddress = 0;
  hexToInt >> procAddress;
  unsigned int nbbytetoRead = atoi(argv[3]);
  std::string targetFileName = std::string(argv[4]);

  boost::shared_ptr<void> procHandle(OpenProcess(PROCESS_VM_READ, false, DWORD(procPID)), &::delete_HANDLE);
  if(procHandle)
  {
   std::vector<char> memCopyofProcMem(nbbytetoRead);

   SIZE_T NumberOfBytesRead = 0;
   if(ReadProcessMemory(procHandle.get(), LPCVOID(procAddress), LPVOID(&memCopyofProcMem[0]), SIZE_T(nbbytetoRead), &NumberOfBytesRead) && NumberOfBytesRead == nbbytetoRead)
   {
    std::ofstream targetFile(targetFileName, std::ios::binary|std::ios::out);
    targetFile.write(&memCopyofProcMem[0], nbbytetoRead);
    targetFile.close();
   }
   else
   {
    std::cout << "ReadProcessMemory return false, or NumberOfBytesRead != of the requested number of byte." << std::endl;
    return -1;
   }
  } 
 }
 catch (const std::exception& e)
 {
  std::cout << "std::exception: " << e.what() << std::endl;
  return -1;
 }
 catch (...)
 {
  std::cout << "unknown exception: " << std::endl;
  return -1;
 }
 return 0;
}