luni, 31 ianuarie 2011

Autocomplete functionality using prefix trees

Update: all the source code for this article can be accessed from here

This is a follow up to my previous post on implementing a prefix tree in C#.
This expands on that idea and uses the data structure to generate a list of autocomplete matches for a given input character sequence.
These are the modifications I brought to the code:





This is the main program:

duminică, 30 ianuarie 2011

Implementation of the prefix tree(trie) data structure in C#

Update: all the source code for this article can be accessed from here

A prefix tree is a data structure with many uses. I won't go into details about this data structure Wikipedia does a better job than I could. What I will do is post a C# implementation of this data structure and in a following post I will show how to use this data structure when implementing an auto-complete solution.

These are the properties of a prefix tree:
  • it stores strings(finite sequences of chars)
  • it had nodes :) (doooooooh!)
  • each node stores a character
  • each node can have a number of subnodes(0,1,2, ... infinity)
  • the char stored in a subnode is the one following the char stored by the node in a string
  • each node has a flag which is raised if a word ends there(this doesn't mean it can't have subnodes for example a prefix tree can store the words "cat" and "catastrophy" in this case node 3 stores 't' and has a subnode that stores 'a')

And now the code:
The node:

namespace PrefixTree
{
 /// 
 /// this is the class that represents a node in the prefix tree
 /// the tree stores:
 /// 1. a char
 /// 2. references to it's subnodes
 /// 3. a flag which is raised if any word ends with the char stored in this node
 /// 
 class Node
 {

     /// 
     /// char stored by the node
     /// 
     public char Char;
     /// 
     /// references to it's subnodes
     /// 
     public List Subtree;
     /// 
     /// the flag which is raised if any word ends with the char stored in this node
     /// 
     public bool AWordEndsHere;

     /// 
     /// constructor
     /// 
     /// 

     public Node(char c)
     {
         Char = c;
         Subtree = new List();
     }
     /// 
     /// gets a reference to this node's subnode that stores the specified char
     /// 
     /// 
the specified char
     /// reference to the subnode that stores the specified char
     public Node GetChild(char c)
     {
         if (Subtree.Count != 0)
             foreach (var node in Subtree)
                 if (node.Char == c)
                     return node;
         return null;
     }
 }
}


The tree:

namespace PrefixTree
{

   /// 
   /// this is the main prefix tree class
   /// 
   class Tree
   {
       private List subsequentStrings;

       private Node _root;
       public Node Root
       {
           get
           {
               return _root;
           }
       }

       /// 
       /// Create a prefix tree - the root node should always store ' '
       /// 
       public Tree()
       {
           _root = new Node(' ');
       }

       /// 
       /// Add a word(series of characters) into the prefix tree
       /// 
       /// 

       public void AddWord(string word)
       {
           char[] chars = word.ToCharArray();
           Node currentNode = _root;
           Node child = null;
           //cycle through the chars in the string
           for (int counter = 0; counter < child =" currentNode.GetChild(word[counter]);" child ="="" newnode =" new" currentnode =" newNode;" currentnode =" child;" counter ="="" awordendshere =" true;">
       /// return wether a string can be found in the tree
       /// 
       /// 
a string
       /// true or false
       public bool Find(string query)
       {
           char[] chars = query.ToCharArray();
           Node currentNode = _root;
           Node child = null;
           //search throughout the child nodes of the current node for the current char
           for (int counter = 0; counter < child =" currentNode.GetChild(chars[counter]);" child ="="" currentnode =" child;">


The main program:

Tree prefixTree = new Tree();
          
prefixTree.AddWord("abc");
prefixTree.AddWord("abcd");
prefixTree.AddWord("abcde");
prefixTree.AddWord("abcdef");
prefixTree.AddWord("ab123cd");
prefixTree.AddWord("abc123d");
prefixTree.AddWord("abc132d");
          
string word = "abc";


if (prefixTree.Find(word))
Console.WriteLine("found");
else
Console.WriteLine("not found");

marți, 18 ianuarie 2011

How to concatenate data from multiple rows into a single rows

I had a problem once. One table had a lot of data in a column on multiple rows. I had to display it in a view but I needed all the data on that column to be displayed in a single cell on a row. This view was supposed to be the data source for a report ... long story ...
Anyway, the basic idea is this:




SELECT some_column = (your select query
for xml path(''))



this only works in sql server 2008 because earlier versions don't have the "xml" keyword.
Here is an example:




SELECT Documente = (SELECT coalesce(td.Descriere+',','') as [text()] FROM Document d
INNER JOIN TipDocument td ON d.TipDocumentId = td.Id for xml path(''))

duminică, 16 ianuarie 2011

Unable to start debugging on the web server error

Just in case you also received this error:

Unable to start debugging on the web server. Debugging failed because
integrated Windows authentication is not enabled. Please see Help for
assistance

Here is what I did to not get it any more

  1. Open IIS Manager (Internbet Information Services)
  2. Right-click the web site (in case you run it locally you only have
    Default web site) and pick Properties
  3. Choose "Directory Security" tab and click Edit on "Anonymous access and
    authentication control"
  4. In the opening window, uncheck "Allow Anonymous access" and check
    "Integrated Windows Authentication"

Finding the path to an installed application in C#

Well, you can find the path to a locally installed application by querying the windows registry.
The information is here:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\

Example photoshop.exe

I wrote this function in C# to do this for me:




///
/// get the path to a specific application from the registry
///

/// name of the application exe
///
public static string GetApplicationPath(string ExeName)
{
try
{
RegistryKey OurKey = Registry.LocalMachine;
OurKey = OurKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\App Paths\"+ExeName, true);
if (OurKey != null)
return OurKey.GetValue("").ToString();
else
return "";
}
catch(Exception ex)
{
return "";
}
}

Exporting MS SQL data as sql scripts

In PhpMyAdmin exporting data to sql statements not to .mdb files or other databases is very straight forward. When I first had to do this with Sql Management Studio I didn't know how at first.
Then I found out.
Basically, what you have to do is this:
1.right click on a database item
2.go to tasks -> generate scripts in the menu that appears

You now have a wizard that will help you generate scripts for the objects you need. You can either generate one script for all the objects in your database(tables, triggers, stored procedures etc) or one script for just some of the object(you choose which) or each object you choose in it's own script. You can either generate these resulting scripts in files or in new query windows. Everything is configurable from the options window(the wizard step with the title "Choose script options").

Here is an example:
I wanted to just script the triggers in a db. Each in it's own file.
In the options window I set "script triggers" to true and in the following wizard form(with the title "Output options") I chose "file per object".


Reseting visual studio

Visual Studio needs to be reset(in order to change a setting) / is frozen / doesn’t work right

1. Close all instances of VS

2. Click Start > Run...

3. Type "devenv /resetuserdata"

4. Open task manager and wait until devenv.exe process is finished executing

5. Restart VS2005

In Win 7:

search for the folder in which devenv.exe can be found

follow the steps above but instead of typing “devenv /resetuserdata” type “devenv.exe /resetuserdata”

Finding the first non consecutive value in an identity column

This is an interesting SQL problem that presented itself to me once.
You have this table. It has of course a primary key which is an identity column which has a seed of 1.
Then you insert a some data and then delete some data.
The primary key column(let's call it id from now on) will no longer contain only consecutive numbers.
So the question is: "How do you find the first non consecutive value in this column?"
Well, the first thing that came to my mind at the time was making a cursor. That works too but it's not very efficient.
Here is one implementation using a self join which worked for me:



select top 1 t1.id from [table] t1 left join [table] t2 on t1.id+1 = t2.id where t2.id is NULL

vineri, 14 ianuarie 2011

Opening compressed databases or .mdf files in SQL Server Management Studio

If when you try to open a database or attaching a .mdf file you get the error:

is compressed but does not reside in a read-only database or filegroup. The file must be decompressed.

So here is the algorithm I followed to solve this issue:

1. only if an existing database gives this error detach the database – right click -> Detach

2. closed all SQL Server Management Studio instances

3. Stop the SQL Server service.. (Start->Run -> services.msc)

4. Selected .mdf file(should be blue if compressed ) Right click properties -> Advanced -> Uncheck "Compress files to save disk space"

5.(re)attach .mdf file

Setting the week's start day in MS SQL

SET DATEFIRST 1 --start day is Monday

SET DATEFIRST 7 --start day is Sunday – default

To test it PRINT @@DATEFIRST

Making ASP.NET write to a file

Select the file right click->”Properties”->click the”Security” Tab->click the “Add” tab->write YOURCOMPUTERNAME/ASPNET replacting YOURCOMPUTERNAME with your computer’s name->check the “full control” checkbox below-> click “OK”

When calling the file from the code write with two “\” where one is found in the path for example c:\\q.xml.

"Converting" an XmlNode to and XmlDocument in C#


private void Output(XmlNode xnodeOutput,bool bOutputToFile)
{
try
{

XmlDocument xdocOutput = new XmlDocument();

XmlNode xnodeTemp = xdocOutput.ImportNode(xnodeOutput, true);

XmlElement xWC = xdocOutput.CreateElement("whatever");

xdocOutput.AppendChild(xnodeTemp);



The first element will never by shown anyway(assuming you do xdocOutput.Save(afile.xml)) so it can have any name.

Encode Sensitive parameters ASP.NET

Use:

Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(

To Encode

System.Text.Encoding.Unicode.GetString(Convert.FromBase64String(

To decode

This should work for both GET and POST


Query string delimiters

Don’t use:

“#”

Use:

Anything else

Reason:

Browsers interpret # as end of string

Don’t take my word for it test it in the address bar http://www.google.com#www.microsoft.com

display/hide divs

To display/hide runat="server" divs in asp.net

Don’t use:

Divid.visible = true/false

Use:

Divid.attributes[“style”] = “display:none”/ “display:block”

Reason:

You won’t have javascript access to those divs

duminică, 9 ianuarie 2011

C# implementation of the parallel sieve of Erathostenes

Update: all the source code for this article can be downloaded from here

This is my implementation of a parallel sieve of Erathostenes in C#.
I got the algorithm from here.
I know I could have used some of the existing C# functionality to manage threads rather than doing it all myself but I found this more convenient at the time.
Anyone is welcomed to bring any corrections.





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ParalelErathostenes
{
    /// 
    /// Finds prime numbers starting from 2 and up to a given value
    /// 
    class Program
    {

        /*         
        1. Create a list of natural numbers 2, 3, 4, 5, ….., n.  None of which is marked. Each process 
        creates its share of lists. 
        2. Set k to 2, the first unmarked number on the list. Each process does this. 
        3. Repeat: Each process marks its share of list 
        a. Mark all multiples of k between k² and n. 
        b. Find the smallest number greater than k that is unmarked. Set  k to this new 
        value 
        c. Process 0 broadcasts k to rest of processes. 
        Until k² > n. 
        4. The unmarked numbers are primes. 
        5. Reduction to determine number of primes
        */

        /// 
        /// possible status of each thread used by this program
        /// 
        enum ThreadStatus
        {
            DoingStuff = 0,
            Waiting = 1,
            Dead = 2
        };

        /// 
        /// Statuses for all the threads started
        /// 
        static ThreadStatus[] ThreadStatuses;

        /// 
        /// array that stores wether a number is prime or not true = prime, false = not prime
        /// this array should be as long as the maximum number up to which you want to find primes
        /// 
        static bool[] nums;

        /// 
        /// k from the algorithm above
        /// 
        static int k;

        /// 
        /// n from the algorithm above
        /// 
        static int n;

        /// 
        /// nr of threads that compute primes - this is actually the number of worker threads because one 
        /// extra thread will handle just finding the smallest remainig prime and broadcasting it to all the 
        /// other threads
        /// 
        static int nrThreads;

        /// 
        /// changes status of all worker threads to "DoingStuff"
        /// 
        static void MakeWorkerThreadsDoStuff()
        {
            for (int i = 1; i < nrThreads + 1; i++)
            {
                ThreadStatuses[i] = ThreadStatus.DoingStuff;
            }
        }

        /// 
        /// changes status of all worker threads to "Waiting"
        /// 
        static void MakeWorkerThreadsWait()
        {
            for (int i = 1; i < nrThreads + 1; i++)
            {
                ThreadStatuses[i] = ThreadStatus.Waiting;
            }
        }

        /// 
        /// changes status of all worker threads to "Dead"
        /// 
        static void KillWorkerThreads()
        {
            for (int i = 1; i < nrThreads + 1; i++)
            {
                ThreadStatuses[i] = ThreadStatus.Dead;
            }
        }

        /// 
        /// checks wether at least one of the worker threads is still doning something
        /// 
        /// 
        static bool WorkerThreadsDoingStuff()
        {
            for (int i = 1; i < nrThreads + 1; i++)
            {
                if (ThreadStatuses[i] == ThreadStatus.DoingStuff)
                    return true;
            }
            return false;
        }

        /// 
        /// checks wether at least one of the worker threads is waiting(on stand by)
        /// 
        /// 
        static bool WorkerThreadsWaiting()
        {
            for (int i = 1; i < nrThreads + 1; i++)
            {
                if (ThreadStatuses[i] == ThreadStatus.Waiting)
                    return true;
            }
            return false;
        }

        /// 
        /// checks wether the main thread is in wairing mode
        /// 
        /// 
        static bool MainThreadWaiting()
        {
            return (ThreadStatuses[0] == ThreadStatus.Waiting);
        }

        /// 
        /// checks if all threads are done working and waiting
        /// 
        /// 
        static bool AllThreadsDead()
        {
            foreach (var threadStatus in ThreadStatuses)
            {
                if (threadStatus != ThreadStatus.Dead)
                    return false;
            }

            return true;
        }

        /// 
        /// each thread runs this function it marks non prime numbers within it's given range
        /// between start and stop
        /// 
        /// 

        static void MarkNonPrimes(int ThreadNr)
        {

            while (ThreadStatuses[ThreadNr] == ThreadStatus.Waiting)
            {
                Thread.Sleep(1);
            }

            if (ThreadStatuses[ThreadNr] == ThreadStatus.Dead)
                return;
            //stores how many numbers we have to decide are primes or non primes
            int nrOfNumbers = n - k * k;
            //divide that by the number of threads to get the interval within which each thread should 
            //look for primes
            int interval = nrOfNumbers / nrThreads;

            //for this thread and this value of k start at this value
            int startNumber = k * k + interval * (ThreadNr - 1);
            //and end at this value
            int stopNumber = (ThreadNr == nrThreads) ? n : startNumber + interval;

            lock (nums)
            {
                for (int j = startNumber; j < stopNumber; j++)
                {
                    //Mark all multiples of k between k² and n. 
                    if (j % k == 0)
                        nums[j] = false;
                }
            }

            //nothig to do now but wait
            ThreadStatuses[ThreadNr] = ThreadStatus.Waiting;

            MarkNonPrimes(ThreadNr);
        }

        /// 
        /// this is the main thread's function it manages the worker therads and finds the smallest prime
        /// when the worker threads finish marking non primes
        /// 
        static void FindSmallestPrimeAndBroadcast()
        {
            while (WorkerThreadsDoingStuff())
            {
                Thread.Sleep(1);
            }

            //c. Process 0 broadcasts k to rest of processes. 
            for (int i = k + 1; i < n; i++)
            {
                if (nums[i])
                {

                    k = i;

                    break;
                }
            }
            //Until k² > n. 
            if (k * k > n)
            {
                //kill worker threads
                KillWorkerThreads();
                //commit suicide
                ThreadStatuses[0] = ThreadStatus.Dead;
                return;
            }
            //all worker threads can now read the correct new value of k so they can all get to work
            MakeWorkerThreadsDoStuff();

            FindSmallestPrimeAndBroadcast();
        }

        static void Main(string[] args)
        {

            n = 100;
            nrThreads = 10;

            //add one because one main thread is needed to manage all the others
            ThreadStatuses = new ThreadStatus[nrThreads + 1];

            //1. Create a list of natural numbers 2, 3, 4, 5, ….., n.  None of which is marked.
            nums = new bool[n];
            //suppose all these are primes, we will then start marking them as non primes
            for (int i = 2; i < n; i++)
                nums[i] = true;

            //initialize k with 1, start looking for primes bigger than 1, 1 is not a prime
            k = 1;

            //make sure all threads are waiting so they don't run out and find primes on their 
            //own without the main thread regulating them
            MakeWorkerThreadsWait();

            //this is the main thread
            var _mainThread = new System.Threading.Thread(() => FindSmallestPrimeAndBroadcast());
            _mainThread.IsBackground = true;
            _mainThread.Start();

            //worker threads
            for (int i = 1; i < nrThreads + 1; i++)
            {
                //need to declare this as a locol variable so that the value passed to the thread
                //won't be changed causing chaos
                var localI = i;
                var _workerThread = new System.Threading.Thread(() => MarkNonPrimes(localI));

                _workerThread.IsBackground = true;
                _workerThread.Start();
            }

            //wait for all threads to end
            while (!AllThreadsDead())
            //commenting the line above and decommenting the one below really help with debugging
            //because you don't constantly get sidetracked to AllThreadsDead() when pressing F5
            //while (true)
            {
                Thread.Sleep(1);
            }

            //print the primes you found
            for (int i = 0; i < n; i++)
                if (nums[i])
                    Console.WriteLine(i);
        }
    }
}