Wednesday, March 22, 2017

How to Install YouCompleteMe Vim Plugin for Linux

***If you are like me, using vim as the main editor for coding, then I highly recommend that you read this post.

 Here, I will go through a step by step guide for installing YouCompleteMe plugin for vim with C-family support, i.e., C/C++/Objective-C/Objective-C++. Detailed instruction is given in its Github repo, but it took me quite a while to figure out how to do this right, so I just think it will be helpful to other people as well.

First, you will need to install Vundle. Run the following:
$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

Next, copy and paste the following at the top of ~/.vimrc file:
set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'Valloric/YouCompleteMe'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList       - lists configured plugins
" :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line



Next, install Vundle and YouCompleteMe in vim. To do this, open up vim and install plugins:
$ vim
:PluginInstall

This command within vim will install Vundle and YouCompleteMe plugins directly from the Github repositories, so make sure that you have Internet connection. Wait until it says "Done" at the bottom of vim.

You are not done yet. You will need to install necessary packages to configure YouCompleteMe.
$ sudo apt-get install build-essential cmake python-dev python3-dev

Next, you will need to change directory to where the YouCompleteMe is installed and setup clang-completion for C-family:
$ cd ~/.vim/bundle/YouCompleteMe
$ ./install.py --clang-completer

This will take a while, so be patient. When this is done, you are done with the installation, but you are not done with C-family auto-completion features just yet. For more info, you will need to read the official documentation regarding the part.

Basically, you will need to let clang know how to compile your project, so that it can suggest auto-completion methods or fields. If your simply want to skip all and see it in action, then create ~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py with the following:
def FlagsForFile( filename, **kwargs ):
  return {
    'flags': [ '-x', 'c++', '-Wall', '-Wextra', '-Werror' ],
  }


Also, append the following line to ~/.vimrc
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py'

Now, start editing a C++ file in the same directory with vim
$ vim hello.cpp

As you edit, you will see YouCompleteMe in action with auto-completion for C++ functions!

By the way, you may wish to read here when using Python modules. It explains how to set the python executable path. Also, if you are a Mac user, you may want to checkout this post by Oliver.


Saturday, February 18, 2017

Copying to Clipboard in Mac OS X Terminal

In case you want to copy the entire content of a file to clipboard, here is what you can do conveniently.

$ cat some_file.txt | pbcopy

This command will copy the standard output to the clipboard. Since the cat command pipes the file's text to standard output, pbcopy will copy it into clipboard.

That's it!

Tuesday, February 14, 2017

Fundamental Git Concepts and Useful Git Commands

Summary of fundamental git concepts:

- repository: a collection of all commits and logs
- commit: a snapshot of the files at the time it was created
- branch: label for a commit and its subsequent commits
- master: the main branch
- HEAD: current commit
- origin: remote repository
- staging area: intermediate step before creating a commit


Summary of most frequently used commands in git:

# show current status
$ git status

# show commits in chronological order [with statistics]
$ git log [--stat]

# show branching graph between two branches [represented in single lines]
$ git log --graph [--oneline] branch1 branch2

# show difference between two commits
$ git diff commit1 commit2

# show difference between the specified commit and its direct parent
$ git show commit_hash

# load specified commit
$ git checkout commit_hash

# load specified branch
$ git checkout branch_name

# save current work as a new branch
$ git checkout -b new_branch_name

# show [hidden] branches
$ git branch [-a]

# create a new branch
$ git branch new_branch_name

# add branch2 into branch1; i.e., branch1 will incorporate branch2
$ git merge branch1 branch2

# add files to staging area
$ git add .

# remove files from staging area
$ git reset

How to Push an Existing Repository to Git Server

Say you have a Github account or a local git server where you want to publish your work either publicly or privately as a bare repository. This post will explain how to create a bare repository on Github or git server. Refer here for official Github documentation.

First off, you will need to create a bare repository. If you are using Github, simply create a new repository. Note that you must NOT add README or, .gitignore files.

If you are using a local git server, you will need to run the following as a local user in the server:
$ git init --bare test_repo.git 

Note that by convention bare repositories end with .git to differentiate with working repositories. Bare repositories refer to those that are on Github or a local git server where users only upload / download and not directly work with. In contrast, a working repository is what you would have on your desktop, constantly editing and saving files locally.

Next, open up terminal and change directory to your project directory. If you haven't initialized it as a working git repository, then do so.
$ git init
$ git add .
$ git commit -m "initial commit"

Now, add your remote repository
$ git remote add origin GIT_REMOTE_REPOSITORY

where GIT_REMOTE_REPOSITORY will be your remote git repository in https or ssh.

Finally, push your current repository to the remote repository:
$ git push -u origin master

Now, the two repositories should be in sync!

Sunday, February 12, 2017

Java Threading: Synchronization II

In the previous post, we looked at how synchronized methods can be used to ensure safe access to a single object, where multiple threads try to read/write to it.

Here, we improve upon the example. In the last post, I mentioned that there were still fixes to be made, and today we will discuss one of them.

Below is the copy from the previous lesson. Take a closer look at the run() method of removeThread, line 54-57 below:


You will notice that the thread continuously checks whether there is at least 1 or more elements in the queue, and when there is, it calls dequeue() method. The question is: can we guarantee that element in the queue will remain in between the two calls? That is, what if another thread calls dequeue() in between, intercepting the element first? This is certainly possible...

Solution? I'd say we fix this issue by having dequeue() method check for the number of elements in the queue. Since dequeue() method is synchronized, it is guaranteed that if there is an element, it will be able to retrieve it for sure. What if there is no element in the queue? It will return immediately with an exception stating that there is no element to retrieve. Thus, we need to modify removeThread run() method to loop dequeue() method continuously until it does not throw an exception. Take a look at code below for implementation.


The modified code above certainly is better than the previous version, but we are not done yet. There are still a few more fixes to be made, and we will tackle one by one in the subsequent posts!

Saturday, February 11, 2017

Android Threading: Handler Example

In this post, we will go over a very simple Handler example for Android development. Hopefully this post will explain and demonstrate why and how to use Handler class in Android.

Consider a very simple app, where you have two buttons: task button and increment button.

Upon click, the task button will notify the user that the task is now running, and carry out some heavy task, which may take up to seconds. When complete, it will notify the user that the task is finally done.

Upon click, the increment button will simply increment counter and display its current value.

Let's take a look at a crude attempt to implement this using a simple Thread. Here is the layout file.

Here is the implementation activity file.


Here, upon the task button click, it launches the task, which is simulated by sleep(3000), on a separate thread so that we don't slow down the main UI thread. However, the problem is that we must wait for this task thread to complete because we need to update the button's text upon completion. Thus, we use join() method to wait for the task to complete. With this implementation, we find that the UI thread becomes unresponsive while waiting. This is NOT a good way of implementing the task.

The core problem is that we ask the task thread to carry out some heavy task, and we need to make sure that the main UI thread does not just sit and wait, but rather do its jobs on its own. The task thread then must communicate with the UI thread and let it know the task is complete, at which point, the UI thread can update the UI accordingly.

The Handler class in Android achieves exactly this, and below is modification that makes use of the Hander class.


As you can see, the task thread will do its job, and when the task is complete, it notifies the main UI thread that the job is done through handler. The handler is created in the main activity, so it is bound to the main UI thread. When the handler receives a message from the task thread that the job is complete, it will then update the button's text. In the meantime, the main UI task carries out its own tasks, such as incrementing the counter when the increment button is pressed.

Of course, one can implement this without using the Handler class, shown below. However, this solution is possible because we are communicating with the UI thread in this example. If, however, you have two different task threads that must communicate with each other, then you need to use the Handler.

Tuesday, February 7, 2017

How to Load Java OpenCV Library to Android Studio

In this tutorial, I will go through a step by step method to load Java OpenCV Library to Android Studio.

First, download OpenCV for Android from here. Extract the zip file, and you should see OpenCV-android-sdk folder.

Next, in Android Studio, open up a project where you want to integrate OpenCV Java library. Then, click File - New - Import Module and select OpenCV-android-sdk/sdk/java folder. Android Studio will ask about import option, and just accept the default.

You should see OpenCVLibrary module in your Android project. Select its build.gradle file and set appropriate versions for compileSdkVersion, buildToolsVersion, etc.

Now, you need to add dependency. Open up your app's build.gradle file and add
compile project(':openCVLibrary310')
into dependencies { ... } section.

Next, you will need to make sure that your app loads in the OpenCV library. Go to the activity class file that first uses OpenCV Library, and add static statement, similar to below:

...
import org.opencv.android.OpenCVLoader;

public class MainActivity extends Activity {
    final static String TAG = "Main Activity";

    static {
        if(!OpenCVLoader.initDebug()){
            Log.d(TAG, "OpenCV not loaded");
        } else {
            Log.d(TAG, "OpenCV loaded");
        }
    }
...

Finally, you will need to copy native binary files. Copy OpenCV-android-sdk/sdk/native/libs folder as src/main/jniLibs folder in your app's directory.

That's it! You should now be able to use OpenCV functions in your app!

Sunday, January 22, 2017

Java Threading: Synchronization I

In this example, we will look into a simple example that uses java's synchronized keyword.
Consider the following unsafe shared queue example first.


public class SharedQueueUnsafe
{
    private int[] queue;
    private int head, tail, num_elems;
    private int size;

    public SharedQueueUnsafe(int size)
    {
        this.size = size;
        queue = new int[size];
        for (int i=0; i<size; i++)
            queue[i] = -1;
        head = tail = num_elems = 0;
    }
    public void enqueue(int data)
    {
        num_elems++;
        System.out.println("inserting " + data + " into the queue");
        try
        {
            Thread.sleep(100);
        }
        catch (InterruptedException ie)
        {
        }
        queue[head] = data;
        head = (head + 1) % size;
    }
    public int dequeue()
    {
        num_elems--;
        int ret = queue[tail];
        tail = (tail + 1) % size;
        return ret;
    }
    public int getElementsInQueue()
    {
        return num_elems;
    }
    static public void main(String[] args)
    {
        SharedQueueUnsafe sharedQueue = new SharedQueueUnsafe(10);
        Thread insertThread = new Thread()
        {
            public void run()
            {
                sharedQueue.enqueue(12345);
            }
        };
        Thread removeThread = new Thread()
        {
            public void run()
            {
                while (sharedQueue.getElementsInQueue() <= 0)
                    ;
                int element = sharedQueue.dequeue();
                System.out.println("retrieved " + element);
            }
        };
        insertThread.start();
        try
        {
            Thread.sleep(50);
        }
        catch (InterruptedException ie)
        {
        }
        removeThread.start();
    }
}

Two threads are created such that one thread is inserting elements while the other is retrieving. To demonstrate racing condition, the enqueue method is designed to be intentionally slow. When I compile and run this, I get

$ javac SharedQueueUnsafe.java && java SharedQueueUnsafe
inserting 12345 into the queue
retrieved -1

This is because the retrieving thread accessed the queue before it was actually inserted, so that it retrieved the initialization value of -1 rather than 12345.

A crude solution to prevent this happening is rather trivial. We simply make sure that enqueue, dequeue, and getElementsInQueue methods to execute exclusively for a single object. That is, however many threads want to access these methods simultaneously, we force only a single thread can actually access only one of the synchronized methods at any given time. This will of course bring up side effect: performance issue, but for not let's not worry about it yet. We just want to solve the racing condition here.

Java language supports keyword synchronized for methods for this purpose. We simply let each method synchronized such that only a single thread can run any one synchronized method at a given time. Other threads must wait until the current thread is done executing the method. This is rather trivial in this case, and the code is as follows:


public class SharedQueueSafe
{
    private int[] queue;
    private int head, tail, num_elems;
    private int size;

    public SharedQueueSafe(int size)
    {
        this.size = size;
        queue = new int[size];
        for (int i=0; i<size; i++)
            queue[i] = -1;
        head = tail = num_elems = 0;
    }
    public synchronized void enqueue(int data)
    {
        num_elems++;
        System.out.println("inserting " + data + " into the queue");
        try
        {
            Thread.sleep(100);
        }
        catch (InterruptedException ie)
        {
        }
        queue[head] = data;
        head = (head + 1) % size;
    }
    public synchronized int dequeue()
    {
        num_elems--;
        int ret = queue[tail];
        tail = (tail + 1) % size;
        return ret;
    }
    public synchronized int getElementsInQueue()
    {
        return num_elems;
    }
    static public void main(String[] args)
    {
        SharedQueueSafe sharedQueue = new SharedQueueSafe(10);
        Thread insertThread = new Thread()
        {
            public void run()
            {
                sharedQueue.enqueue(12345);
            }
        };
        Thread removeThread = new Thread()
        {
            public void run()
            {
                while (sharedQueue.getElementsInQueue() <= 0)
                    ;
                int element = sharedQueue.dequeue();
                System.out.println("retrieved " + element);
            }
        };
        insertThread.start();
        try
        {
            Thread.sleep(50);
        }
        catch (InterruptedException ie)
        {
        }
        removeThread.start();
    }
}

With this new class, we get correct result.

$ javac SharedQueueSafe.java && java SharedQueueSafe
inserting 12345 into the queue
retrieved 12345

Well, the solution above is only a temporary crude solution. There are some more fixes to be made, but let's not worry about them for now. We will revisit this example in subsequent posts (part II).

Saturday, January 21, 2017

Java Threading: Simplest Example

In the series of posts, I will present Java's threading APIs. Today's example is the simplest threading example using anonymous class in Java.

public class SimpleThread
{
    public static void main(String[] args)
    {
        Thread thread1 = new Thread()
        {
            public void run()
            {
                int i = 0;
                while (i < 10)
                {
                    System.out.println(++i);
                    try
                    {
                        sleep(100);
                    }
                    catch (InterruptedException ie)
                    {
                    }
                }
            }
        };
        Thread thread2 = new Thread()
        {
            public void run()
            {
                int i = 0;
                while (i < 10)
                {
                    System.out.println(++i + 10);
                    try
                    {
                        sleep(100);
                    }
                    catch (InterruptedException ie)
                    {
                    }
                }
            }
        };
        thread1.start();
        thread2.start();
    }
}

To compile and run
$ javac SimpleThread.java && java SimpleThread
1
11
2
12
13
3
4
14
5
15
6
16
7
17
8
18
9
19
10
20

This example is easy enough and self-explanatory!

Tuesday, November 29, 2016

Transition from C/C++/Java to Python

I am trying to learn Python myself, as it greatly expedites prototype development compared to C/C++/Java.
Here is quick conversion of C/C++/Java common codes into Python:

for (int i=0; i<N; i++) {
for i in range(N):

if (x==y) {

if x==y:

!true

not True

else if {

elif:

printf("%d\n", i)

print i

printf("%d ", i)

print i,

for (int i : array) {

for i in array:

vector<int> v;

v.push_back(1);
v.push_back(2);
v = [1,2]

// C++
vector<int> u,v;
...
v = u;
import copy
v = copy.copy(u) // OR // v = u[:]

// C++ vector<int> &u, &v;
...
v = u;
v = u