Python C API

It is usually easier to write and debug Python scripts than do it in C/C++, while for heavy computation tasks, C or C++ programs can run much faster. A common strategy to get the best of both worlds is writing the computation kernels in C/C++ and binding Python APIs to invoke the computation kernels in Python scripts. Python C API exists exactly for the purpose of binding Python with C/C++.

I learnt Python C API recently, and this post records my practice of creating a Python package, txt2qr, that uses C library libqrencode. Txt2QR generates the QR code (in SVG format) of a given text, and is easily integrated into a Django app.

Read More

Hash Strings at Compile Time

It is convenient to index data stored in map, and hash table by name, and the theoretical time complexity is as low as O(logn) or O(1). Nevertheless, the overhead to convert a string name into the key to index the map or hash table structure could hurt the performance sometimes. This post demonstrates a technique do the hash computation on constant strings earlier at compile time.

Read More

C++ Template Method Polymorphism

Polymorphism in C++ means a function or object of the same identity/name behaves differently under different scenarios. A common example of polymorphism is a derived class could override the method definition in the base class if it is marked virtual and a base class pointer would invoke the proper definition according to which class instance it is actually pointing to.

Polymorphism reduces trivial programming efforts in some cases and is very useful. Template method for generic programming is another style of polymorphism done at compile time to avoid unnecessary code duplication and eases the burden of programmers (see my earlier post series). However, C++ forbids template methods to be marked as virtual. So could somehow we have it all?

Read More

new and delete in C++

C++ introduced a built-in method to dynamically manage memory, as an alternative to library based malloc, realloc, free functions in C. This is achieved by two keywords, new and delete.

Read More

Customized Key for Container Map: Bi-match Pair of Unsigned Integers

I had a post, Container Types in C++, where I introduced a bit about one of the C++ containters std::map.
Yesterday I ran into an interesting use case of map, namely, I wanted to use map with customized key, bi-match unsigned integer pair. This post talks about the way to customize key for std::map, and issues and solution to get a bi-match unsigned integer pair work correctly as the key.

Read More

A Bite of Template in C++ (3)

In the post A Bite of Template in C++ (1), I mentioned a constraint for template member functions in class that explicit specialization is not allowed. However, I realized there are more constraints on function templates later when I wanted to make two template varients with slightly difference on only one member function. I tried specializing member function template, as well as inheriting a template member function from base class. Both of them failed due to some constraints. Briefly, the constraints are

  1. partial specialization is not allowed for function template,
  2. using declaration plus template disambiguator is not the right way to inherit a template member function from a template class.
Read More

A Bite of Template in C++ (2)

I encountered two issues when using C++ template. One is about why sometimes typename has to be prefixed to declarations, and another is how to access the types in the parameter list from outside of the template. This post has some ideas for those two questions, as well as some discussions about nested templates.

Read More

Find The Right Place For Const Qualifier In Declaration

The keyword const is a qualifier used to claim the values of variables are constant. It is useful, because compiler recognizes it and help programmers to avoid some bugs made unconsciously. However, I am always confused about where to put the const, especially using it with special types, for example pointer. This post is about declarations with const qualifier (also applicable to volatile of course).

Read More