Monday, May 19, 2008

Enable .bashrc changes without restart the shell

This is just a quick reference for myself:-)

> source .bashrc

Wednesday, March 19, 2008

Some useful emacs tools

;; display date and time always
(setq display-time-day-and-date t)
(display-time)

;; highlight matching parentheses next to cursor
(require 'paren)
(show-paren-mode t)

;; type "y"/"n" instead of "yes"/"no"
(fset 'yes-or-no-p 'y-or-n-p)

;; load auto-show (shows lines when cursor moves to right of long line)
(require 'auto-show)
(auto-show-mode 1)

; highlight region between point and mark
(transient-mark-mode t)

(setq-default auto-show-mode t)

Monday, March 3, 2008

Stack with Max() and Min()

Question: Implement a stack with Max() and Min().


Traditional solution:
1. Push triple(value, max, min)
2. use a seperate stack

An improved solution of the first solution above is:

you may consider only use one additional space per record to save
space, since a new value cannot be both a new maximum or minimum. So, just
let the previous maximum or minimum to share a single space is enough.

Each stack record is pair where the first is the new value to push
in and second is the previous MAX or MIN.

Use two variables Max and Min to record the current max and min.

Define push(int n) as:
if (empty()) push(pair(n, ARBITRARY VALUE)); Max = Min = n;
if (n > MAX) push(pair(n, MAX)); Max = n;
if (n < MIN) push(pair(n, MIN)); Min = n;
o/w push(pair(n, ARBITRARY VALUE));


So, pop(int n) becomes:
if (top().first == Max) Max = top().second;
if (top().first == Min) Min = top().second;
pop();

This reduce the space requirement by 1/3, which is at the worst case twice
the # of push().

However, as one has previously pointed out that use an extra stack is
another solution, which, turns out to be more space efficient with the extra
space complexity as O(total # of changes of Max or Min in push() - those in pop()), which is guaranteed smaller than the # of push().

Friday, February 29, 2008

Find the largest increase in a sequence

Question:

Given an array A[1..N], find the i, j such that for all 1 <= i < j <= N, find the largest A[j] - A[i]

Answer:

For array A[1..N]

let f(n) to be the result value for the first n elements.
let m(n) to be the smallest element of A[1]..A[n].

f(n) = max{ f(n-1), A[n] - m(n-1) }
m(n) = min{ m(n-1), A[n] }

Solve with DP, O(n) time, O(1) space.

Meta key problem on Mac

Just got my Macbook Pro. I'm trying to ssh to my devbox and use emacs there. The meta key is not working. On windows keyboard, the Alt key sends the meta key. Then there is an option for terminal to solve this problem.

Just go to Window settings for Terminal. Choose the keyboard page and enable the option "use option key as meta key".

Thursday, February 28, 2008

"screen" on Mac OS X

I use "screen" to keep connections to remote servers. However, the mac machine has a different keyboard than a PC, so backspace and some other keys may not work.

A simple and quick solution is to add the following line to your shell profile of your account on the remote server.

alias screen="TERM=screen screen"

Monday, January 28, 2008

Dynamic Memory Allocation on Stack

How to dynamically allocate memory in the stack? Use alloca() is an option:

f()
{
int * i = (int *)alloca(sizeof(int));
}

alloca() allocates on the current stack frame. Anyway it makes no sense to allocate dynamic mem in stack? It does.

If you want to dynamically allocate some memory but do not take the hassle to release it at all exit points. One way to do it is like this.

Another way is to use a smart pointer. However, the second solution is not available in C.

Stack meant for dynamic memory for the CURRENT context. But then we have to make sure the size of the object. If it is a C++ object, then need to use the displacement new method I described before. I believe this is more useful for dynamic array, though dynamic array is supported by C99. For C++, smart pointer is better, or dynamic array. But for C, this is somehow useful.

Displacement new operator in C++

Have you ever seen C++ code like this?

void *spc = memPool->Alloc(sizeof(ObjectC));

ObjectC * content = new (spc) ObjectC();

This is really hard-to-read code for many beginners. It looks this piece of code couldn't even pass the symtax check.

This is called "placement new". That is, the first line allocate the memory block at a specific location in memory (which should you implement in the Alloc() method of your memory pool object). The second lilne takes the pointer to the allocated memory block as a parameter to new operator (yes, new operator could take a parameter). So new will use this memory block to place the new object, instead of allocating by itself. This way, the new object is allocated at a specific location.

Why you can't directly do this way?

ObjectC * content = (ObjectC *) memPool->Alloc(sizeof(ObjectC));

Since then the constructor is not called. You should NEVER allocate a new object by simple allocate memory for it.

That's a special symtax for new. Last thing is: try not to use thi symtax unless you really need it. If you use this symtax, make sure you always allocate sufficient and correctly aligned memory for the object.

Also, you are responsive for the destruction of this object. Neither compiler nor run-time will take care of that. So, write down the line somewhere after you finished your object:

content->~ObjectC();

Monday, January 14, 2008

Type Casting between Basic Types

We have a double type variable and need to pass it to another module. However, we only have a communication API that can pass a single uint32_t. How can we do this without losing any precision?

Using the following C style casting is not working:

double d = 3.14;
int i = (int) d;

In this case i = 3 and we lost the decimals.

Using C++ style static_cast has the same effect:

int i = static_cast(d);

The C++ reinterpret_cast seems to work how ever if you write

int i = reinterpret_cast(d);


it is not working. reinterpret_cast can only cast between pointer types.

So, we got the hint here: reinterpret it as a pointer!

The correct solution then works as this way:

nt i = *(reinterpret_cat<*int>(&d));

If you prefer the C style, this could be neater:

int i = *((int *)(&d));

When we need to decode it, simple do the reverse way:

double newD = *((double *)(&i));

Wednesday, October 17, 2007

Introduce Yahoo! AutoSync

For those who uses Yahoo! Mail and Outlook. I would suggest you to install "Yahoo! AutoSync". This tool sync your yahoo mail, calendar, contact list and tasks with Outlook (also Palm). This is the best way I found so far that can synchronize my Outlooks on different machines along with my web mails.

To install it, simply log into your Yahoo Calendar and click the "sync" link at the upper-right corner and then follow the instructions.