Skip to main content


Showing posts from 2012

Linked List Quiz - Part II !!!

In the last part, we saw the academic (not general purpose) version of a Linked List used to solve the puzzles, and solved the following puzzles on linked list.
Reverse the list recursivelyReverse the list iterativelyFind if a list is cyclic
In this part, I will be solving the remaining two puzzles that I listed in the last part.
Finding the cyclic node in a cyclic linked list According to my solution, the node which is actually supposed to be the end of the linked list is the cyclic node. Let us call Cn. Taking node Cn as the cyclic one has an advantage wherein you can break the cycle; assign Cn->next = nullptr;But some people take the node after Cn as the cyclic node. The node after Cn is the node somewhere back in the list. This way it is not possible to break the cycle as we would traversed past Cn.
LinkedList::Node* LinkedList::FindCyclicNode() const { int iterCount = 0; auto jmpBy1Ptr = root; auto jmpBy2Ptr = root; while (jmpBy1Ptr != nullptr   && jmpBy2P…

Offering __FILE__ and __LINE__ for C# !!!

Not the same way but we could say better.
Visual Studio 2012, another power packed release of Visual Studio, among a lot of other powerful fancy language features, offers the ability to deduce the method caller details at compile time.
C++ offered the compiler defined macros __FILE__ and __LINE__ (and __DATE__ and __TIME__), which are primarily intended for diagnostic purposes in a program, whereby the caller information is captured and logged. For instance, using __LINE__ would be replaced with the exact line number in the file where this macro has been used. That sometimes beats the purpose and doesn't gives us what we actually expect. Let's see.

For instance, suppose you wish to write a verbose Log method with an idea to print rich diagnostic details, it would look something like this.
void LogException(const std::string& logText, const std::string& fileName, …

Linked List Quiz - Part I

A short while back, Azhagu quizzed me on linked list based problems; singly linked list.
I am recording those problems, solutions and my experience as a two part series. In the first part, I am introducing the linked list class, which I wrote for packaging the implementation of the solutions. This class pertains to the context of the problem(s) and cannot be used as a general purpose linked list. A std::list might more pertinent in the context of the general purpose implementation of a list.
Here are some of the problems that Azhagu asked me to solve:-
Reverse the list recursivelyReverse the list iterativelyFind if the list is cyclicFind the node that causes the cycle (and break the cycle)Reverse every K nodes in the listI will be solving reversing the list (both iteratively and recursively) and finding if the list is cyclic problems in this episode.
#pragma once #include <iostream> #include <vector> using namespace std; template<typename T> class LinkedList { public:…

Sms FireWall Update !!!

I gave SMS FireWall a refresh with a couple of features requested by users:- An option 'Allow and Move' in the settings screen, which when checked moves the messages from the quarantine vault when opted to be added to the allowed list. Although it might be clear to you, let me explain it in a few words how it works. When you long press a sender in the quarantine vault, a menu with two options appear - Allow, Delete. When 'Allow' is clicked, the sender is added to the allowed list. And when the 'Allow and Move' setting is checked, the messages from this sender are moved to the inbox.A confirmation dialog prompt when attempted to empty the quarantine vaultA few minor (internal) improvements Hope they are useful to others too. And let me know if you need any other features to be in the application.

OrderedThreadPool - Bug Fix !!!

Hugh pointed out a bug in the OrderedThreadPool.
I think there is a small window for error in the OrderedThreadPool class. Bascially, if an item of work is queued, then a worker thread runs, takes the item off the queue and is about to call wcb(state) - but at that instant is (say) context switched. Then another item gets queued and another worker thread runs and dequeues the item and then again is about to call wcb(state). There is scope here for the two operations to run concurrently or even out of order...
Here is the fixed version of the same.
using System; using System.Collections.Generic; using System.Diagnostics; namespace System.Threading { public class OrderedThreadPool { private Queue workItemQ = new Queue(); private bool loopWorkRunning = false; public void QueueUserWorkItem(WaitCallback wcbDelegate, object state) { lock (workItemQ) { workItemQ.Enqueue(new ThreadPoolTaskInfo(wcbDelegate, state)); if (workItemQ.Count == 1 && !loopWorkRunnin…

Unique Id Generation !!!

A short while I was engaged in a little project where I had to interact with a third party service provider who required a (30 length) unique id as part of the transaction. I am little dumb and am used to GUIDs for a long time when it comes to unique ids. But GUIDs are more than 30 in length. I was trying out some stupid ways like stripping out the trail part of the GUID to make 30 length unique but my intuition wasn't convinced about the tricks I was working out.
Finally, Sriram helped me with it. I am sharing his code for the benefit of others.
string GenerateUniqueId(int length) { string asciiChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; char[] chars = asciiChars.ToCharArray(); byte[] randombytes = new byte[length]; RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider(); crypto.GetNonZeroBytes(randombytes); StringBuilder result = new StringBuilder(length); foreach (byte b in randombytes) { result.Ap…