Everyday opens like a flower

Tuesday, 25 August 2009

I got a thank you email last week from someone who found my TST prefix match search code useful. I posted that code 2 and half years ago on CodeProject, very please to know someone is interested in it :-)

Ternary Search Tree is an useful data structure for certain type of project. It combines attributes of binary search trees and digital search tries. Like tries, they proceed character by character. Like binary search trees, they are space efficient, though each node has three children, rather than two. A search compares the current character in the search string with the character at the node. If the search character is less, the search goes to the left child; if the search character is greater, the search goes to the right child. When the search character is equal, though, the search goes to the middle child, and proceeds to the next character in the search string.

The idea behind ternary search trees dates back at least as far as 1964. In "Randomized Binary Searching with Tree Structures" (Communications of the ACM, March 1964), H.A. Clampett sketched a primitive version of the structure. Computer scientists have proven many theorems about the trees; for instance, searching for a string of length k in a ternary search tree with n strings will require at most O(log n+k) comparisons.

This is a great C# implementation, although there are a couple of issues (please read the comments of the article), the main one is the partial search it provided is not what I expect, for example, provide words zymase, zyme and zymic, partial search for zym* only return zyme, partial search for zym** will return zymic. What I need is a prefix match search that will return all three words for zym. There is a guy provide a wildcard search function, but it didn't get back what I expect as well. So I wrote the prefixMatch feature based on a Java Ternary Tree example.

Feel free to change it to suite your requirements and polish it (e.g. exception handling etc ^_^). Currently it return a StringCollection. In my project I actually changed the dictionary to generic so can store and return different type of value object. I use a keysNumReturnValues to limit the number of results that returned, -1 means return all. I do this for my testing purpose.

I stored the words that in the words.txt in the TstDictionary(included in the Tst source code), when I search for zym, it give me the following results:------------------------------------------------------------type a prefix to do prefix match or type exit to exitzym-- end 19 results --zymasezymezymiczymogenzymogenezymogeniczymologiczymologicalzymologistzymologyzymolysiszymomezymometerzymophytezymoscopezymosezymosimeterzymosiszymotic

Hope you find the code useful.

------------------------ Source Code -------------------------------

/// /// Returns alphabetical list of all keys in the tree that begin with prefix./// ///

Friday, 21 August 2009

For website that require cookies, if using the web site in an Iframe, and parent website is on a different domain, then 3rd party(cross domain) cookies that do not have a compact policy will be blocked on some common browser privacy settings, e.g. IE when privacy setting is set to Medium.

This result on the web site inside the Iframe won't working properly, as session won't work if cookie is not enabled.

P3P compact policy can be applied to website as HTTP header so user agents such as IE can detect the P3P policy and decide whether or not to allow cookies from a particular site.

If you are building a test site, you can grab some dummy P3P compact policy online(e.g. CP="CAO PSA OUR"), but if you need a proper one for your production website, generate your own one would be better :-)

There are some softwares or online generators can be used to generate P3P policy. I used the P3P Policy Editor from IBM http://www.alphaworks.ibm.com/tech/p3peditor/ to generate P3P policy.

Unzip the p3pExecutableJar and execute the p3p.jar to load up the software.

You can create a new policy from scratch or create one from one of the templates which I found is easier.

Please refer to this Knowledge Gene Create and Use P3P Policy for Website for detailed instructions.
* To view text views, select a knowde and click Attached Text icon on the right panel header.

Once the policy is created, it includes a Privacy Policy html file and the Compact Policy text, you need to deploy the Privacy Policy html to your website, and use the Compact Policy text in individual pages header.

For example, Page.Response.AddHeader("p3p","CP=\"CAO PSA OUR\""); for ASP.NET C#,Adam Young's blog has a very good post about syntax on different languages.
Alternatively, you can set it in IIS for the whole web site. The KGene include instruction on how to set up HTTP header in IIS as well :-)

Wednesday, 19 August 2009

Many crochet projects require start crocheting in a round, for example, Amigurumi(Japanese art of knitting or crocheting small stuffed animals and anthropomorphic creatures), coasters, containers, hats, doily, individual pattern for crochet patchwork projects etc.
In general, there are 3 ways of doing this. Some people might have developed their own method.

Start with Multiple Chain stitches

Use multiple chain stitches to form a loop, e.g. use 6 or 8 ch. Example below is using 8 ch.

1. Wrap the yarn twice around your finger (I usually wrap it around my two fingers instead).

2. Carefully took off the created loops and use two fingers to pinch them. Crochet the yarn out of the loop.
3. Then make a ch(chain stitch), note: this ch is not counted as one stitich in the 1st row. It just an upright stitch. Use the left hand finger to pull the yarn to control the tightness of the base of this ch.
4. Now start to crochet a sc(single stitch).
Now the first stitch of the 1st row is finished. Continue crochet 5 sc if the 1st row requires 6 stitches.
Next step is to adjust the loop. The left hand side thread is connect to the yarn ball, I named it T1. In the loop itself there are two threads, I named it T2 and T3. You need to pull one of them(T2 or T3) to tighten the loop. To find out which one you need to pull, slightly pull the left hand side yarn thread T1, don't pull too much, just pull it little bit so you can notice one of the yarn thread in the loop get tighter, assume it's T2, this is the thread you need to pull to make the loop smaller. So pull T2 and after the loop is minimized, you can see that T3 is the remaining thread in the loop, pull T1 again which will then tighten up the this remaining thead in the loop.

Then do a sl st(slip stitch) from the first ch(marked in green below) to form the circle. I normally will pull this sl st stitch tighter so it won't be too obvious and the project will look better.

I managed to install East Asian Language support to my BlackBerry 8900 Curve. I have been searching through lots of posts in order to find out how to just install the language support without upgrade the OS, and finished my installation without any heart attack, so I will share my installation steps here to save some effort if anybody needs it.

You can remove unwanted input after installation, e.g. I only keep Chinese input. To switch between input languages, press Entre while holding down Alt key.

You can export the KGene and document using the pdf export icon, or hover over the first knowde, then click 'Learn more', navigate through the KGene, or click 'Show Full Document' to view the full doc :-)

Below is the outline steps:

1. Download Multi Language OS
For my 8900 Curve, I downloaded the 8900-v4.6.1.199_P4.2.0.102 version of OS.

2. Install the downloaded OS to your PC, you will need the files from this new OS later on to add multi language support to your BlackBerry device.
A new 8900-v4.6.1.199_P4.2.0.102 folder will be created under C:\Program Files\Common Files\Research In Motion\Shared\Loader Files.
The CJK.alx is the file we need to use later on.

3. Use Desktop Manager to backup your BlackBerry
Just in case... ^__^

4. Use AppLoader to add extra language support to BB.
Before the installation, go to your AppLoader folder, e.g. C:\Program Files\Common Files\Research In Motion\AppLoader, move the Vendor.xml to a backup directory, so you have kept a copy of the file.
Use the loader.exe to load up the AppLoader, follow the wizard, you will be able to add the CJK.alx file to the list at some point and select the desire languages.