Tuesday, February 12, 2008

Simple .net BST

using System;
using System.Collections.Generic;
using System.Text;

namespace BinarySearchTree
{
internal class Node where T: IComparable
{
private T _data;
private Node leftNode = null;
private Node rightNode = null;

internal Node(T data)
{
_data = data;
}

internal Node LeftNode
{
get
{
return leftNode;
}
set
{
leftNode = value;
}
}

internal Node RightNode
{
get
{
return rightNode;
}
set
{
rightNode = value;
}
}

internal int CompareTo(T val)
{
return _data.CompareTo(val);
}
}
}


----------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace BinarySearchTree
{
public class BinarySearchTree where T : IComparable
{
private Node rootNode = null;

public BinarySearchTree()
{
}

public void Insert(T val)
{
rootNode = Insert(rootNode, val);
}


private Node Insert(Node node, T val)
{
if (node == null)
{
node = new Node(val);
return node;
}

if (node.CompareTo(val) >= 0)
{
node.LeftNode = Insert(node.LeftNode, val);
}
else
{
node.RightNode = Insert(node.RightNode, val);
}

return node;
}
}
}

----------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace BinarySearchTree
{
class Program
{
static void Main(string[] args)
{
BinarySearchTree bns = new BinarySearchTree();

bns.Insert(3);
bns.Insert(2);
bns.Insert(10);
bns.Insert(1);
bns.Insert(4);
bns.Insert(11);
}
}
}

Tuesday, February 5, 2008

Simple Pointers

int cmp(const int i, const int j)
{
if (i == j)
{
return 1;
}

return 0;
}

void search(const int val, const int *p, int (*compare)(const int i, const int j))
{
int ret = 0;
for (int i = 0; i < 10; i++)
{
ret = compare(val, p[i]);
if (ret == 1)
{
printf("value found");
return;
}
}

printf("value not found");
}

int _tmain(int argc, _TCHAR* argv[])
{
int num[] = {100, 1,2,3,4,15,6,7,8,9,10};
int *p = num;
int **pp = &p;

printf("*p = %d\n", *p);
printf("p[0] = %d\n", p[0]);
printf("p[1] = %d\n", p[1]);
printf("*p++ = %d\n", *p++);
printf("*p = %d\n", *p);
printf("*p-- = %d\n", *p--);
printf("*p = %d\n", *p);
printf("*++p = %d\n", *++p);
printf("*--p = %d\n", *--p);
printf("*(p++) = %d\n", *(p++));
printf("*p = %d\n", *p--);
printf("*p+5 = %d\n", *p+5);
printf("*(p+5) = %d\n", *(p+5));

printf("**pp = %d\n", **pp);

search(2, num, cmp);


getc(stdin);
return 0;
}


OUTPUT:
*p = 100
p[0] = 100
p[1] = 1
*p++ = 100
*p = 1
*p-- = 1
*p = 100
*++p = 1
*--p = 100
*(p++) = 100
*p = 1
*p+5 = 105
*(p+5) = 15
**pp = 100
value found

Windows Threading

include windows.h
HANDLE CreateThread(LPSECURITY_ATTRIBUTES secAttr,
SIZE_T stackSize,
LPTHREAD_START_ROUTINE threadFunc,
LPVOID param,
DWORD flags,
LPDWORD threadID);

Thread can be destroyed by calling CloseHandle(HANDLE );
or BOOL TerminateThread(HANDLE thread, DWORD status); //no cleanup
VOID ExitThread(DWORD status); //cleanup stacks

The Visual C++ alternatives to CreateThread( ) and ExitThread( ) are _beginthreadex( ) and _endthreadex( ). Both require the header file .

DWORD SuspendThread(HANDLE hThread);
DWORD ResumeThread(HANDLE hThread);
DWORD GetPriorityClass(HANDLE hApp);
BOOL SetPriorityClass(HANDLE hApp, DWORD priority);
HANDLE GetCurrentThread(void); //get the main thread

Windows Synchronization:
- Semaphore
- Mutex //global object can be used by multi process
- Event Object
- Waitable Timer
- Critical section

MUTEX:
HANDLE CreateMutex(LPSECURITY_ATTRIBUTES secAttr, BOOL acquire, LPCSTR name);
name = NULL => localized sync
A mutex handle is automatically closed when the main process ends. You can explicitly close a mutex handle when it is no longer needed by calling CloseHandle( ).
DWORD WaitForSingleObject(HANDLE hObject, DWORD howLong);
BOOL ReleaseMutex(HANDLE hMutex);

if(WaitForSingleObject(hMutex, 10000)==WAIT_TIMEOUT) {
// handle time-out error
}

// access the resource

ReleaseMutex(hMutex);