sourcecode

Friday, February 22, 2013

xml intro

First impression to XML parsing using RapidXML lib in C++
http://rapidxml.sourceforge.net/

Example XML from here:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms762271%28v=vs.85%29.aspx

Test code under the same folder of the RapidXML:


#include <iostream>
#include <sstream>
#include <fstream>

#include "rapidxml/rapidxml.hpp"
#include "rapidxml/rapidxml_print.hpp"


using namespace std;
using namespace rapidxml;

 char *buffer_file( const char *filename, size_t *buffer_len = NULL )
 {
  //open file.  note: it is important to say binary here, otherwise it does conversion that may change the length!
  std::fstream file( filename, std::ios::in|::std::ios::binary );
  if( !file ) return NULL;

  //read the size...
  file.seekg(0, std::ios::end);
  size_t length = (size_t)file.tellg();
  if( buffer_len ) *buffer_len = length;
  file.seekg(0, std::ios::beg);
  
  //read into memory buffer..
  char *filebuf = new char[length+1];
  file.read(filebuf, length);
  filebuf[length] = '\0'; //make it null-terminated
  file.close();
  return filebuf;
 }


int main(int argc, char* argv[]) {
    char* xml = buffer_file("books.xml");

    //Parse the original document
    xml_document<> doc;
    doc.parse<0>(xml);
    xml_node<> * node = doc.first_node();
    cout << "Name of my first node is: " << node->name() << "\n";
for( xml_node<>* node_ptr = node->first_node("book"); node_ptr != NULL; node_ptr = node_ptr->next_sibling())

    cout<< "node name : " << node_ptr->first_attribute("id")->value()<< endl;
    return 0;
}

Sunday, February 17, 2013

HTC Thunderbolt bricked

Hold Power Key + Volume Down, boot to the list

http://androidforums.com/evo-4g-all-things-root/146246-hboot-driver-help.html
http://unrevoked.com/rootwiki/doku.php/public/windows_hboot_driver_install?

Select Recovery, and reboot (press Power Key). This lead to the ClockWork (if rooted)

Wipe Cache
http://www.droidforums.net/forum/thunderbolt-tech-support/147167-how-get-rid-ota-forced-reboots-rooted-phone.html

Then reboot. See an Error: Update Failed  message at start up, which is good. Click "Cancel".

Thursday, February 14, 2013

Word Ladder

//passed small test, but not big test yet
class Solution {
    unordered_set<string> dict;//short dictionary
    bool off_by_one(string const& s1, string const& s2){
      int counter = 0;
      for(unsigned int ii = 0; ii < s1.length(); ++ii){
        if (s1[ii] != s2[ii]) ++counter;
        if (counter >= 2) return false;
      }
      return (1 == counter);
    }
    
    class Node{
        public:
          Node(string name):word(name),parent(0){}
          string word;
          vector<Node*> children;
          Node* parent;
    };
    
public:
    int ladderLength(string start, string end, unordered_set<string> &dict) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int const word_length = start.length();
        for(auto word:dict){
            if (word.length() == word_length)
            this->dict.insert(word);
        }
        return mutationPath(start,end).size();
    }
    
    vector<string> mutationPath(string const& source, string const& dest){
    
      vector<string> result;
      Node* sp = new Node(source);
      if (source!=dest)dict.erase(source);
      deque<Node*> frontier;
      frontier.push_back(sp);
      while(!frontier.empty()){
        Node* node = frontier.front(); frontier.pop_front();
        string const word = node->word;
        for (auto it = dict.begin(); it != dict.end();){
          string const neighbor(*it);
          if (off_by_one(word, neighbor)){
            if (dest == neighbor){
              result.push_back(neighbor);
              while(node){
                result.push_back(node->word);
                node = node->parent;
              }
              return vector<string>(result.rbegin(),result.rend());
            }
            dict.erase(it++);
            auto it_n = new Node(neighbor);
            node->children.push_back(it_n);
            node->children.back()->parent = node;
            frontier.push_back(it_n);
          }else ++it;
        }//for it
      }//while not empty
      return result;
      //--------------------------
    }
};
//Another way, but still does not pass the big test
class Solution {
    struct Node{
        string name;
        int count;
        Node(string n, int cnt = 0):name(n),count(cnt){}
    };
    string const alphabet{"abcdefghijklmnopqrstuvwxyz"};
    vector<string> neighborList(string const& word){
        vector<string> neighbors;
        for(int ii = 0; ii < word.length(); ++ii){
            int const firstLength = ii;
            int const secondLength = word.length() - firstLength - 1;
            string firstPart = word.substr(0,firstLength);
            string secondPart = word.substr(ii+1, secondLength);
            for(char c:alphabet){
                if (c != word[ii]) neighbors.push_back(firstPart+c+secondPart);
            }
        }
        return neighbors;
    }
public:
    int ladderLength(string start, string end, unordered_set<string> &dict) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        set<string> sdict;//short dict
        int const word_length = start.length();
        for(auto word: dict){
            if (word.length() != word_length) continue;
            sdict.insert(word);
        }
        deque<Node> frontier{Node(start)};
        while(!frontier.empty()){
            Node currentNode = frontier.front();
            frontier.pop_front();
            vector<string> neighbors = neighborList(currentNode.name);
            for(string neighbor_candidate: neighbors){
                set<string>::iterator it = sdict.find(neighbor_candidate);
                if (it != sdict.end()){
                    if (end == neighbor_candidate) return currentNode.count+2;
                    frontier.push_back(Node(neighbor_candidate, currentNode.count+1));
                    sdict.erase(it);
                }
            }
        }
        return 0;
    }
};

Tuesday, February 12, 2013

Doxygen Graph

1. Install doxygen
2. Install graphviz
3. Set DOT path
export PATH=$PATH:/usr/lib/graphviz

4. Create Doxyfile
doxygen -g

5. Configurate Doxyfile: http://www-scf.usc.edu/~peterchd/doxygen/
vi Doxyfile
EXTRACT_ALL = YES
Extract documentation even from those elements you haven't yet commented.
INLINE_SOURCE = YES
Extract the relevant parts of the source and associate them with your description.
HAVE_DOT = YES
Use Graphviz for class and collaboration diagrammes.
CALL_GRAPH = YES
Generate a dependency graph for functions and methods.
GENERATE_LATEX = NO
Skip generating LaTeX sources for PDF.
        RECURSIVE              = YES

6. Run doxygen in the working folder

7. Open index.html under ./html

Gearman - Ubuntu 12.04

1. Pre-Install

You will need to install the following packages in order to build Gearman.

sudo apt-get install –yes gcc
sudo apt-get install –yes autoconf
sudo apt-get install –yes bison
sudo apt-get install –yes flex
sudo apt-get install –yes libtool
sudo apt-get install –yes make
sudo apt-get install –yes libboost-all-dev
sudo apt-get install –yes libcurl4-openssl-dev curl
sudo apt-get install –yes libevent-dev
sudo apt-get install –yes memcached
sudo apt-get install –yes uuid-dev
sudo apt-get install –yes libsqlite3-dev
sudo apt-get install –yes libmysqlclient-dev

http://gearman.info/build/ubuntu.html



2. Download gearman and unpack, run ./configuehttp://gearman.org/getting_started

3. make and sudo make install


4. gearmand -d
gearmand -h

Saturday, February 9, 2013

GITHub remote SSH

https://help.github.com/articles/generating-ssh-keys

Follow the steps above. One edit: no need to install xclip, just
$cat id_rsa.hub

and copy paste the terminal.