Category Archives: C++

GMP on Mac and Windows

There is some task requires handling very large numbers. Say 417812367683563 multiplies 354325123456546 equals what? We need way more digits than the normal double precision integers can do. GMP is a nice library to deal with this problem. It is well documented. But it refuses to make on my Mac OS X 10.5.8 due to XCode issue. After a few search, I found a pre-compiled GMP here.  A minimal test:

#include <gmp.h>

mpz_t n;
mpz_init_set_str (n, “417812367683563”, 0);
gmp_printf (“n %Zd\n”, n);

mpz_t m;
mpz_init_set_str (m, “354325123456546”, 0);
gmp_printf (“m %Zd\n”, m);

mpz_t mn;
mpz_init(mn);
mpz_mul(mn, m, n);
gmp_printf (“m * n %Zd\n”, mn);

link flag is -lgmp

This might be working! I know the answer is 148041418761150250269708953398

On the Windows side, I found MPIR. Need YASM to build it. Run configure and make inside /win dir. It seems to be fine.

Leave a comment

Filed under C++

Hybriding Qt with PyQt

A hybrid Qt and PyQt application sounds attractive: within a Qt application, parts for duty calculation are written in C++ , and UI is written in PyQt, making the application scriptable and expandable. To create such a program, we need to know how to do two sets of things: one is to embed PyQt into a C++ application, the other is to send information from PyQt back to C++. Above is an example. First line is in C++, and second is in PyQt. When the dial or slider is changed, numbers related field will be updated.

So how does it work?

Let’s start with the central widget of the main window.

Center::Center()
{
    setObjectName("center");
    QVBoxLayout* box = new QVBoxLayout();
    LeftWidget *leftDial = new LeftWidget();
    box->addWidget(leftDial);
    ShakeHole *rightDial = new ShakeHole();
    box->addWidget(rightDial);
    setLayout(box);
}

It has two widgets: a LeftWidget and a ShakeHole. (Sorry for the confusing names.)

First, the ShakeHole:

class ShakeHole : public QWidget
{
public:
    ShakeHole();
    ~ShakeHole() {}   
};
ShakeHole::ShakeHole()
{

    int wnd = winId();
        cout<<"winid "<<wnd<<endl;

    cout<<"version "<<Py_GetVersion()<<endl;

    FILE *fin = fopen("/Users/jianzhang/aphid/hybrid_qtpyqt/foo.py","r+");
    PyRun_SimpleFile(fin,"foo");

        PyObject *mainDict = PyModule_GetDict(PyImport_Import(PyString_FromString("__main__")));

        PyObject *claus = PyDict_GetItemString(mainDict, "Ui_Form");

        PyObject* snakewig = Py_BuildValue("i", wnd);

        PyObject * pTuple = PyTuple_New (1);
        PyTuple_SetItem (pTuple, 0, snakewig);

        PyObject *clausInstance = PyObject_CallObject(claus, pTuple);

        cout<<"finished python ui!\n";

        PyErr_Print();

}

It is derived from QWidget, and hooks a PyQt widget within via Python C API.

Now LeftWidget:

class LeftWidget : public HWidget
{
public:
    LeftWidget();
    void setAttribute(const char *attributeName, int value);
private:
    QLineEdit *attributeEdit;
    QLineEdit *attribute1Edit;
};

LeftWidget::LeftWidget()
{
    setObjectName("SnakeHole");
    QHBoxLayout* box = new QHBoxLayout();
    box->addWidget(new QLabel("qt host"));
    attributeEdit = new QLineEdit();
    box->addWidget(attributeEdit);
    attribute1Edit = new QLineEdit();
    box->addWidget(attribute1Edit);
    setLayout(box);

}

void LeftWidget::setAttribute(const char *attributeName, int value)
{
    QString attr(attributeName);
    if(attr == "fieldNumber")
    {
        QString str = QString("%1")
             .arg(value);
        attributeEdit->setText(str);
    }
    else if(attr == "sliderNumber")
    {
        QString str = QString("%1")
             .arg(value);
        attribute1Edit->setText(str);
    }
}

It is a derived HWidget, and it has two QLineEdit, which will be updated by setAttribute(). OK, so what is HWidget?

class HWidget : public QWidget
{
public:
    HWidget();
    virtual ~HWidget();
    virtual void setAttribute(const char *attributeName, int value);
};
void HWidget::setAttribute(const char *attributeName, int value)
{
    qDebug()<<"beep";
}

It is a derived QWidget as well, and it has a virtual function setAttribute() to override.

Go to the PyQt side:

from PyQt4 import QtCore, QtGui
import hoatzin

class Ui_Form(QtGui.QWidget):
    def __init__(self, parentId):      
        Form = QtGui.QWidget.find(parentId)
        super(Ui_Form, self).__init__(Form)
        self.resize(400, 100)
        self.host = hoatzin.Hoatzin()
        self.boxLayout = QtGui.QHBoxLayout()
        self.boxLayout.setObjectName('ui_form_box')
        self.boxLayout.setObjectName("boxLayout")
        self.boxLayout.addWidget(QtGui.QLabel('pyqt widget'))
        self.dial = QtGui.QDial()
        self.boxLayout.addWidget(self.dial)

        self.slider = QtGui.QSlider(QtCore.Qt.Horizontal)
        self.boxLayout.addWidget(self.slider)

        self.setLayout(self.boxLayout)

        self.dial.valueChanged.connect(self.dialTest)
        self.slider.valueChanged.connect(self.sliderTest)

    def dialTest(self):
        w = self.host.set_attribute_int('SnakeHole', 'fieldNumber', self.dial.value() )
        if w != 1:
            print 'SnakeHole not found'

    def sliderTest(self):
        w = self.host.set_attribute_int('SnakeHole', 'sliderNumber', self.slider.value() )
        if w != 1:
            print 'SnakeHole not found'

OK, it is a QWidget and will be attached to a specific parent. It has a dial and a slider, each of them will trigger a function by the signal of valueChanged. Wait a second! What on earth is hoatzin?

It is a Python extension to send information back to the host C++ program:

class Hoatzin
{
public:
    Hoatzin();

    int set_attribute_int(const char *widgetName, const char *attributeName, int value);

private:

};
int Hoatzin::set_attribute_int(const char *widgetName, const char *attributeName, int value)
{
    foreach (QWidget *widget, QApplication::allWidgets())
    {
            if(widget->objectName() == QString(widgetName))
            {
                HWidget *h = (HWidget *)widget;
                h->setAttribute(attributeName, value);
                return 1;        
            }          
    }
    return 0;
}

In set_attribute_int() function, it will search the specific QWidget by objectName(). If found, cast to HWidget, and call its setAttribute(). That is why the target widget should have a specific object name. When it calls “SnakeHole” to setAttribute(), the derived LeftWidget will be called.

Now you know all the tricks necessary for this stunt. The rest is how to compile and test.

First you will need file in https://github.com/spinos/aphid/tree/master/hoatzin

Follow the readme to build the Python module of hoatzin.

Second you need files in https://github.com/spinos/aphid/tree/master/hybrid_qtpyqt

Qmake will create the project for you. It links the compiled library of hoatzin, so no need to compile the source of HWidget again.

Just a confusing example, may be helpful.

Leave a comment

Filed under C++, Python, Qt

Static Class Member

class A {
public:
static int pref;
};
int  A::pref;

class B: public A {};

class C: public B {};

int main () {

A::pref = 97;

cout<<“A::pref “<<A::pref<<endl;
cout<<“B::pref “<<B::pref<<endl;
cout<<“C::pref “<<C::pref<<endl;
B ppoly1;
C ppoly2;
ppoly1.pref = 83;

cout<<“A::pref “<<A::pref<<endl;
cout<<“ppoly0.pref “<<ppoly0.pref<<endl;
cout<<“ppoly1.pref “<<ppoly1.pref<<endl;

C::pref = 71;

cout<<“A::pref “<<A::pref<<endl;
cout<<“ppoly0.pref “<<ppoly0.pref<<endl;
cout<<“ppoly1.pref “<<ppoly1.pref<<endl;

return 0;

}

 

Static member is a convenient way to share data between different classes. Once the base class (A) has the public static data (pref). Any class inherits it owns the shared data. The data can be accessed or modified by all the classes, and the value will always be the same.

Leave a comment

Filed under C++

Regex Search Basic

Here is the problem: we are looking for Date within some log text. Cannot use string::find() because we don’t know exactly what the data is. We only know the pattern of the string of Data: MM-DD-YYYY, month and day can have one or two digits, year has four, and they are separated by two hyphens (-), i.e. 1-31-2010, 12-1-2011.

Boost.Regex is useful in this case. regex_search() can search the text base on specific expression. “\\d{1,2}-\\d{1,2}-\\d{4}” means any string that consists of “either 1 or 2 numbers a hyphen either 1 or 2 numbers a hyphen four numbers”. Here is the search:

std::string::const_iterator start, end;
 start = log_text.begin();
 end = log_text.end();
 static const boost::regex expression("\\d{1,2}-\\d{1,2}-\\d{4}");
boost::match_results<std::string::const_iterator> what;
 while(regex_search(start, end, what, expression, boost::match_extra))
 {
cout<<what[0]<<endl;
 start = what[0].second;
 }

Leave a comment

Filed under C++