This is a follow up to my previous post on implementing a prefix tree in C#.
This is the main program:
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 ListSubtree; /// /// 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; } } }
namespace PrefixTree { ////// this is the main prefix tree class /// class Tree { private ListsubsequentStrings; 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;">
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");
SELECT some_column = (your select query
for xml path(''))
SELECT Documente = (SELECT coalesce(td.Descriere+',','') as [text()] FROM Document d
INNER JOIN TipDocument td ON d.TipDocumentId = td.Id for xml path(''))
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
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 "";
}
}
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”
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
SET DATEFIRST 1 --start day is Monday
SET DATEFIRST 7 --start day is Sunday – default
To test it PRINT @@DATEFIRST
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.
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);
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
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
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
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); } } }