Saturday, February 22, 2014

Sign In option doesn't appear in blogspot.com in Firefox

Sign In option is available on the Navbar on the top right corner.



Recently this appears to have disappeared when opened in Firefox.
This is a new issue and seems to occur because of some javascript issue and a filter rule in Fanboy's Social Blocking List in Adblock Plus.
The Filter rule causing this is ||google.com/js/plusone.js$third-party. You need to disable this filter.
In Firefox, goto Tools → Adblock Plus → Filter preferences
Under Filter subscriptions tab, select Fanboy's Social Blocking List.

On the right side there are the filters. If you do not see the filters on right, hover your mouse on the dotted line on the right till a Hand appears, then click on it.



Scroll down to ||google.com/js/plusone.js$third-party and uncheck it.





Close Adblock Plus Filter preferences.
Now open blogspot webpage or refresh an existing blogspot webpage. The Navbar should be back with the Sign In option.

Friday, February 21, 2014

C++ std::map

Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order. map<Key, Value>
Properties of maps

  1. Like Set, Map (ordered or unordered) does not allow duplicate Key
  2. Ordered map is sorted by it's Key. It is sorted following a specific strict weak ordering criterion indicated by its internal comparison object std::map::key_comp. So a map of user defined class objects which have no meaning of comparison, will not be sorted.
  3. Unordered map is not sorted by Key
  4. map is generally slower than unordered_map to access individual elements by their key map[Key]. So if std::find is mostly what you will be doing, unordered_map is better choice.
  5. unordered_map is generally less efficient for range iteration through a subset of it's elements than ordered map. So if iteration is mostly what you will be doing, then ordered map is a better choice.
  6. Maps are typically implemented as binary search trees.
  7. A unique feature of map among associative containers is that they implement the direct access operator (operator[]), which allows for direct access of the mapped value.
Proof of key uniqueness and sorting properties
cout << "Ordered Map" << endl;
cout << "===============================" << endl;
map<int, string> myMap;
myMap.insert(make_pair(33, "Hello"));
myMap.insert(make_pair(11, "Cello"));
myMap.insert(make_pair(11, "Vello"));
myMap.insert(make_pair(44, "Jello"));
myMap.insert(make_pair(22, "Fello"));

for(map<int, string>;::iterator iter = myMap.begin(); iter != myMap.end(); ++iter)
{
    cout << "<Key, Value> = <" << iter->;first << ", " << iter->;second << ">" << endl;
}

cout << endl;
cout << "Unordered Map" << endl;
cout << "===============================" << endl;
unordered_map<int, string> myUnorderedMap;
myUnorderedMap.insert(make_pair(33, "Hello"));
myUnorderedMap.insert(make_pair(11, "Cello"));
myUnorderedMap.insert(make_pair(11, "Vello"));
myUnorderedMap.insert(make_pair(44, "Jello"));
myUnorderedMap.insert(make_pair(22, "Fello"));

for(unordered_map<int, string>::iterator iter = myUnorderedMap.begin(); iter != myUnorderedMap.end(); ++iter)
{
    cout << "<Key, Value> = <" << iter->first << ", " << iter->second << ">" << endl;
}
Output
Ordered Map
===============================
<Key, Value> = <11, Cello>
<Key, Value> = <22, Fello>
<Key, Value> = <33, Hello>
<Key, Value> = <44, Jello>

Unordered Map
===============================
<Key, Value> = <33, Hello>
<Key, Value> = <11, Cello>
<Key, Value> = <44, Jello>
<Key, Value> = <22, Fello>
Find in a map by Key
if(myMap.find("f") != myMap.end()) 
{
    // Found
}
else 
{
    // Not found
}
Find in a map by Value
There are four options
  1. Use boost::bimap if boost library is available
  2. Create another map with Value as the Key and use map::find on it
  3. Use std::find_if with a predicate/functor in which you define some equal to comparison logic
  4. Iterate through the whole map to find the value
    for(map<int, string>::iterator iter = myMap.begin(); iter != myMap.end(); ++iter)
    {
        if(iter->second == "Hello")
        {
            // Found
            break;
        }
    }
    
Option 1 & 2 are equivalent. Runtime will be O(log n) for a map and O(1) for an unordered map. Option 2 you are essentially reinventing the boost:bimap. Option 3 & 4 are equivalent. Runtime in both cases is O(n). They both iterate the whole map entry-by-entry until a matching entry is found. Hence, if boost library is not available, go for option 2. Create two maps and implement functionality to keep them synchronized.

Free memory from a map of object pointers
  • map or any STL container destroys it's elements while being destroyed itself. If the map had stored some objects, those objects will be destroyed. If it had stored pointers, those pointers will be destroyed, but not the actual objects pointed to by the pointers. So, you have to destroy those instances by yourself explicitly, iterate through std::map elements and call delete on contained pointer.
  • std::map::clear() empties out the contents of the map, i.e. all contained pointers will be destroyed. It doesn't do anything with the objects pointed by them, in particular it doesn't delete them. All those objects will stay alive in memory.
Note: Raw pointers do not have ownership of the objects they point to. So when a pointer is destroyed, it will not delete whatever it points to. Generally speaking, when you have a pointer, there is no way to know if:
  • It points to a valid object at all (you don't want to call delete on some random garbage in memory),
  • The object it points to has been allocated with new (if it has been allocated in another way, it should not be deleted with delete), or
  • If there are other pointers that also point to the same object (in which case only one of them should call delete. Which one?)
So even if you wanted to, there is no way to automatically delete an object when a pointer to it is destroyed.

Sunday, February 9, 2014

C++ Functor Predicate with std::find_if

struct StringFinder
{
  StringFinder(const std::string & st) : s(st) { }
  const std::string s;
  bool operator()(const RegPair& lhs) const { return lhs.first == s; }
}

std::find_if(sequence.begin(), sequence.end(), StringFinder(foo));


That's not how predicates work. You have to supply either a free function bool Comparator(const MyClass & m) { ... }, or build a function object, a class that overloads operator():

struct MyClassComp
{
  explicit MyClassComp(int i) n(i) { }
  inline bool operator()(const MyClass & m) const { return m.myInt == n; }
private:
  int n;
};

std::find_if(v.begin(), v.end(), MyClassComp(5));

A Functor is a object which acts like a function. Basically, a class which defines operator().

class MyFunctor
{
   public:
     int operator()(int x) { return x * 2;}
}

MyFunctor doubler;
int x = doubler(5);

The real advantage is that a functor can hold state.

class Matcher
{
   int target;
   public:
     Matcher(int m) : target(m) {}
     int operator()(int x) { return x == target;}
}

Matcher Is5(5);

if (Is5(n))    // same as if (n == 5)
{ ....}


a functor is an object that acts like a function, i.e. it overloads the function call operator.

Functors are commonly used in STL algorithms. They are useful because they can hold state before and between function calls, like a closure in functional languages. For example, you could define a MultiplyBy functor that multiplies it's argument by a specified amount:

class MultiplyBy {
private:
    int factor;

public:
    MultiplyBy(int x) : factor(x) {
    }

    int operator () (int other) const {
        return factor * other;
    }
};

Then you could pass a MultiplyBy object to an algorithm like std::transform:

int array[5] = {1, 2, 3, 4, 5};
std::transform(array, array + 5, array, MultiplyBy(3));
// Now, array is {3, 6, 9, 12, 15}

Another advantage of a functor over a pointer to a function is that the call can be inlined in more cases. If you passed a function pointer to transform, unless that call got inlined and the compiler knows that you always pass the same function to it, it can't inline the call through the pointer.

You can do it with a functor or a regular function that is not part of MyClass, or with a static function inside MyClass - here's an example with non-member function (basically just removing the MyClass:: part of the condition definition):

#include 
#include 

using namespace std;

class Foo
{
  //whatever
};

class MyClass
{
  public:
  int myInt;
  vector foo_v;
};

bool condition(MyClass mc)
{
  if(mc.myInt==5)
    return true;
  else
    return false;
}


int main (void)
{
  vector myClass_v;
  std::find_if(myClass_v.begin(),myClass_v.end(),condition);
}


C++ Comparison Operator overloading

A.hpp
#include <string>
#include <vector>
class A
{
    private:
 std::vector <std::string> m_member;

    public:
 A(std::vector <std::string> input); 
 const std::string GetValue() const;
 bool operator==(const A& rhs) const;
};
A.cpp
#include <string>
#include <vector>
#include "A.hpp"

A::A(std::vector <std::string> input) : m_member(input)
{ 
}

const std::string A::GetValue() const
{
    std::string returnString;
    for(std::vector <std::string>::const_iterator iter = m_member.begin(); iter != m_member.end(); ++iter)
 {
     returnString = returnString + *iter;
 }
 
    return returnString;
}

bool A::operator==(const A& rhs) const
{
    std::string rhsValue = rhs.GetValue();
    std::string lhsValue = this->GetValue();
 
    //Equality is true if lhsValue is equal to rhsValue
    return (lhsValue == rhsValue);
}
main.cpp
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include "A.hpp"

using namespace std;

int main(int argc, char *argv[])
{
    vector <string> input1;
    input1.push_back("Hello");
    input1.push_back("World");
 
    vector <string> input2;
    input2.push_back("Fellow");
    input2.push_back("World");
 
    vector <string> input3;
    input3.push_back("Hollow");
    input3.push_back("World");
 
    vector <string> input4;
    input4.push_back("Hello");
    input4.push_back("World");
 
    A obj_A1(input1);
    A obj_A2(input2);
    A obj_A3(input3);
    A obj_A4(input4);
 
    vector <A> A_vec;
    A_vec.push_back(obj_A1);
    A_vec.push_back(obj_A2);
    A_vec.push_back(obj_A3);
 
    A* obj_p_A1 = new A(input1);
    A* obj_p_A2 = new A(input2);
    A* obj_p_A3 = new A(input3);
    A* obj_p_A4 = new A(input4);
 
    cout << obj_p_A2->GetValue() << endl;
    cout << obj_p_A4->GetValue() << endl; 
 
    vector <A*> A_p_vec;
    A_p_vec.push_back(obj_p_A1);
    A_p_vec.push_back(obj_p_A2);
    A_p_vec.push_back(obj_p_A3);
 
    if(obj_A2 == obj_A4)
    {
        cout << "objects are equal" << endl;
    }
    else
    {
        cout << "objects are NOT equal" << endl;
    }

    if(find(A_vec.begin(), A_vec.end(), obj_A4) != A_vec.end())
    {
        cout << "obj_A4 found in A_vec" << endl;
    }
 
    if(*obj_p_A2 == *obj_p_A4)
    {
        cout << "objects pointers are equal" << endl;
    }
    else
    {
        cout << "objects pointers are NOT equal" << endl;
    }

    //if(find(A_p_vec.begin(), A_p_vec.end(), obj_p_A4) != A_p_vec.end()) // Overloaded comparison operator not 
                                                                          // called because pointer obj_p_A4 
                                                                          // has been used.
    //{
    //    cout << "*obj_p_A4 found in A_p_vec" << endl;
    //}

    system("PAUSE");
    return EXIT_SUCCESS;
}