March 4th, 2008
Accessing and using office components from .net framework is not trivial (god knows why).
There is a complex office API and some weird security configurations that need to be done.
After wasting a lot of time trying to create a simple file with a simple table, I decided to share with you the articles that helped me.
Accessing office components from c# are well described in this great article By Doctor Peter A. Bromberg.
But!!!! this is not sufficient. after executing this code you will bump into weird security exceptions.
Solving those issues are well described in this article by Sushmita Kumari
Have fun
Posted in c#, excel, office, security  3 Comments »
How to add a parameter to a Predicate
January 14th, 2008
When using a “Find” method in a generic List, there is a need to implement a Predicate(T) delegate that defines the conditions of the element to search for. a not good MSDN example can be found here. why not good? because usually we want to add extra parameters to the Predicate(T) delegate.
A good way of doing it is by using Anonymous Method as below:
ourList.Find(delegate(OurObject objectItem)
{
return objectItem.StringToFind == stringToCheck;
}
);
a good reference for Anonymous Method can be found here
BTW, this post was written with a great assistance of our Telephony team
Posted in c#, data structures  1 Comment »
Fibonacci Heap and Amortized Time
January 1st, 2008
In every system we design and develop, choosing the data structure that fits to our needs is one of the most important decisions we made. Data structures not only fits to our storage need, it also affects our system complexity and its performance.
Performance is a big word, when it refers to data structures, we usually discuss the worstcase sequence and according to that we determine it.
but, in some cases the worstcase happens infrequently, and in most cases (also the average case) our performance is extremely different then the worstcase.
This kind of analyses is being called Amortized Analysis. Amortized analysis refers to the average running time and not the worst case running time.
Fibonacci Heap is a great example for amortized analysis. a glance in the table below shows its advantages versus a linked list

Linked List 
Fibonacci Heap 
insert 
O(1) 
O(1) 
access min 
O(1) 
O(1) 
delete min 
O(n) 
O(log n)* 
decrease key 
O(1) 
O(1)* 
delete 
O(n) 
O(log n)* 
merge 
O(1) 
O(1) 
(*) Amortized time
How we accomplish this?
Fibonacci Heap is heap data structure consisting of a forest of trees. Its implementation discussed in details in this Wikipedia article and also being demonstrated in .
The main idea of it, is keeping the level of trees in a way that in the average case we will have less operations to do. only once in a while we will merge the trees and organize them to a binomial heap (this operation is being called CONSOLIDATE). since we kept the level of the trees, the CONSOLIDATE operation will take O(log n) in the average case and only infrequently it will take O(n).
Also, in some cases we can control the phase of the worst case and force it to happen. it can help us to improve our system performance by forcing CONSOLIDATE operation during an offpeak.
The use of Fibonacci Heaps improves the asymptotic time of Dijkstra’s algorithm for computing shortest paths in a graph and Prim’s algorithm for computing a minimum spanning tree of a graph.
Maybe some day in the future, Microsoft or Sun will choose to add this fascinating data structure to there implemented collections.
Posted in data structures  2 Comments »
How to shuffle a list
December 13th, 2007
usually, most of the manipulations that are being done with lists are sorting. therefore most of the .Net Framework solutions are for doing that. But what if we want to shuffle a list instead of sorting it?
well, I have bump into that problem while ago, and try to come up with the most elegant solution for doing it.
my first approach was using the existing .Net Framework solutions for sorting and manipulate them in a way to provide a shuffling solution.
so I did as follow:
I have created a RandomComparer who implements IComparer<int> with the following compare method:
public int Compare(int x, int y)
{
if (x == y) return 0;
else return random.Next(2) == 0 ? 1 : 1;
}
Then, I shuffled the list while using the RandomComparer I have created:
List<int> sortedList = GetSortedList();
List<int> shuffledList;
shuffledList = sortedList.Sort(RandomComparer.Instance);
Looks like an elegant solution? well, it has 2 problems:
 performance  sorting a list takes in the worst case O(n lgn) while shuffling can take O(n)
 after running this code in load test I have noticed that once in ~200 times, a System.ArgumentException is being thrown saying: “IComparer did not return zero when Array.Sort called x. CompareTo(x). x”.
notice that the Compare method described above returns 0
if(x == y). after investigating this I figure that in some cases there is 3 parameters comparison, and some times we get:
a > b, b > c, c> a. which makes no sense…
So, the best way I come up with is a simple implementation of a shuffle method that picks a random value from the sorted list and add it to the shuffled list:
public List<int> Shuffle(List<int> sortedList)
{
List<int> shuffledList = new List<int>(sortedList.Count);
int count = sortedList.Count;
for (int i = 0; i < count; i++)
{
int j = random.Next(sortedList.Count);
shuffledList.Add(sortedList[j]);
sortedList.Remove(sortedList[j]);
}
return shuffledList;
}
notice that this solution takes in the worst case O(n)
Tags: c#, data structures
Posted in c#, data structures  3 Comments »
Date and Time manipulations in SQL
December 12th, 2007
Data types
in SQL Server, data types for representing date and time are datetime and smalldatetime.
Not like the variety of structures .NET Framework 2.0 and .NET Framework 3.0 where DateTime, TimeSpan, Calendar and other structures are being used for storing dates and time intervals, in SQL we can use only datetime and smalldatetime.
The difference between datetime and smalldatetime is the range of dates and the accuracy of minutes and seconds
 datetime: January 1, 1753  December 31, 9999, with accuracy of one threehundredth of a second
 smalldatetime: January 1, 1900  June 6, 2079, with accuracy to the minute
So, let’s get started…
Converting datetime into string (nchar, nvarchar, char, varchar)
using the CONVERT method we can convert a datetime into other expression in lots of formats
Syntax for CONVERT:
CONVERT ( data_type [ ( length ) ] , expression [ , style ] ) 
samples:
Initiate datetime: declare @date datetime
set @date = getdate()

Sample 1: get the date in U.S standard
query: select convert(varchar,@date,101)

result: ’12/12/2007’

Sample 2: get the date and time in ODBC canonical (with milliseconds) standard
query: select convert(varchar,@date,121)

result: ’20071212 16:31:20.967’

Sample 3: get the time in hh:mi format
query: select convert(varchar(5),@date,108)

result: ’16:39’

useful functions when dealing with datetime
GETDATE:
Returns the current system date and time in the SQL Server standard internal format for datetime values.
Syntax for GETDATE:
GETDATE ( )

Sample:
query:
SELECT GETDATE()

result:
‘20071212 17:07:39.733’

DATEADD:
Returns a new datetime value based on adding an interval to the specified date.
Syntax for DATEADD: DATEADD (datepart , number, date )

Sample:
query:
SELECT DATEADD(month, 1, ‘08/30/2006′)

result:
‘20060930 00:00:00.000’

DATEDIFF:
Returns the number of date and time boundaries crossed between two specified dates.
Syntax for DATEDIFF: DATEDIFF ( datepart , startdate , enddate )

Sample:
query:
SELECT DATEDIFF(day, ‘08/30/2006′, ‘09/30/2006′)

result:
‘31′

DATENAME:
Returns a character string representing the specified datepart of the specified date.
Syntax for DATENAME:
DATENAME ( datepart ,date )

Sample:
Query:
SELECT DATENAME(month,‘19951030 12:15:32.123′)

result:
‘October’

DATEPART:
Returns an integer that represents the specified datepart of the specified date.
Syntax for DATEPART:
DATEPART ( datepart , date )

Sample:
Query:
SELECT DATEPART(month, ‘08/30/2006′)

result:
‘8’

Tags: sql
Posted in sql  2 Comments »