data-structures

Any single-consumer single-producer lock free queue implementation in C?

I'm writing a program with a consumer thread and a producer thread, now it seems queue synchronization is a big overhead in the program, and I looked for some lock free queue implementations, but only found Lamport's version and an improved version on PPoPP '08: enqueue_nonblock(data) { if (NULL != buffer[head]) { return EWO...

Idiomatic Ruby: data structure transformation

What's the "Rubyist" way to do the following data structure transformation: I have incoming = [ {:date => 20090501, :width => 2}, {:date => 20090501, :height => 7}, {:date => 20090501, :depth => 3}, {:date => 20090502, :width => 4}, {:date => 20090502, :heigh...

Comparison of collection datatypes in C#

Does anyone know of a good overview of the different C# collection types? I am looking for something showing which basic operations such as Add, Remove, RemoveLast etc. are supported, and giving the relative performance. It would be particularly interesting for the various generic classes - and even better if it showed eg. if there is ...

Objective-C implementation of a histogram or bag datastructure

Instead of implementing my own I was wondering if anyone knows of a histogram or bag datastructure implementation in Objective-C that I can use. Essentially a histogram is a hashmap of lists where the lists contain values that relate to their hash entry. A good example is a histogram of supermarket items where you place each group of it...

how to parse a tree data structure?

I have a tree data structure, comprised of nodes, that I need to parse into an expression tree. My nodes look like this (simplified): public class Node { public Node Left { get; set; } public Node Right { get; set; } public Operation OperationType { get; set; } public object Value { get; set; } ...

books/sites to get good at algorithm competitions

Hi, I've been practicing doing topcoder and acm contests(local ones and practice sets). But I'm hitting a brick wall in terms of performance. I can solve the first topcoder problem, but almost never the second one. I need some solid theory and memory of common strategies/algos/structures involved in these types of competitions. Just try...

Sorting two linked lists according to memory location

I need to merge two doubly-linked lists, but not by their values (the lists are not sorted). I want to obtain a single list that has all the nodes from the two, but in the order in which they appear in memory. Maybe this image helps more: http://img140.imageshack.us/i/drawing2.png/ Is there any algorithm (preferably a fast one) that ca...

What's a better alternative to the following data structure: Dictionary<string, Dictionary<string, string>>

I have the following set of data <ids> <id1 attr1="value1" attr2="value2" /> <id2 attr3="value3" attr4="value4" /> <id3 attr2="value6" attr5="value7" /> </ids> Basically, it's an XML that can have any node name with any attribute name with any attribute value. After parsing the XML, I store the attribute data in a Dictionary...

Probabilistic hashing -- is there such a thing?

Say you want to implement a click tracker where you want to only count a click to a link from any IP address once, but the number of links and clients is very large and you don't want to keep a table of every single IP-click. Say that you might need this as part of something that runs live against every click and don't want to do a looku...

Reverse the ordering of words in a string

Hi, I have this string s1 = "My name is X Y Z" and I want to reverse the order of the words so that s1 = "Z Y X is name My". I can do it using an additional array. I thought hard but is it possible to do it inplace (without using additional data structures) and with the time complexity being O(n)? cheers ...

What's a good data structure for periodic or recurring dates?

Is there a published data structure for storing periodic or recurring dates? Something that can handle: The pump need recycling every five days. Payday is every second Friday. Thanksgiving Day is the second Monday in October (US: the fourth Thursday in November). Valentine's Day is every February 14th. Solstice is (usually) every June ...

Nested grouping strategy/algorithm c#

Not sure if I have to correct label for this type of problem, but do you have any thoughts on a generic solution for the following? Given a collection of Invoices: var invoices = new List<Invoice>() { new Invoice() { Id = 1, Customer = "a", Date = DateTime.Parse("1/1/2009") }, new Invoice() { Id = 2, Customer = "a", Date = DateTime.Par...

Best way to create a hashmap of arraylist

I have one million rows of data in .txt format. the format is very simple. For each row: user1,value1 user2,value2 user3,value3 user1,value4 ... You know what I mean. For each user, it could appear many times, or appear only once (you never know). I need to find out all the values for each user. Because user may appear randomly, I u...

How should I expose hierarchical data from a TreeView control to a MVP presenter?

I have some hierarchical data in a Winforms TreeView control and I need to expose it as a property so my presenter can synchronize changes to it. Just to be clear, I'm using the Passive View pattern. With most WinForm controls this is a no-brainer. The controls themselves expose their data as a system type which can easily be passed alon...

How to write a search engine?

I am trying to write a search engine that will give meaningful results when a user enters words that occur within a bunch of documents. For that I want to know how exactly a search engine works, what data structures and algorithms it uses to build indexes, store and query indexes etc. Plus some pointers on how to make a search engine giv...

Method for making a variable size struct

I need to craft a packet that has a header, a trailer, and a variable length payload field. So far I have been using a vector for the payload so my struct is set up like this: struct a_struct{ hdr a_hdr; vector<unsigned int> a_vector; tr a_tr; }; When I try to access members of the vector I get a seg fault and a sizeof of an entir...

Which data type to use for ordinal?

Whenever I have some records/objects that I want to be in a certain order, I usually create a field called Ordinal. I often wonder if it would be better to use an integer or a decimal value for the ordinal field. This is a consideration when moving an object to a different position in the order: If you use consecutive integers, you h...

What's a good data model for cross-tabulation?

I'm implementing a cross-tabulation library in Python as a programming exercise for my new job, and I've got an implementation of the requirements that works but is inelegant and redundant. I'd like a better model for it, something that allows a nice, clean movement of data between the base model, stored as tabular data in flat files, a...

Parallel algorithms and data structures

Inkeeping with my interests in algorithms (see here), I would like to know if there are (contrary to my previous question), algorithms and data structures that are mainstream in parallel programming. It is probably early to ask about mainstream parallel algos and ds, but some of the gurus here may have had good experiences/bad experience...

Naming a dictionary structure that stores keys in a predictable order?

Note: Although my particular context is Objective-C, my question actually transcends programming language choice. Also, I tagged it as "subjective" since someone is bound to complain otherwise, but I personally think it's almost entirely objective. Also, I'm aware of this related SO question, but since this was a bigger issue, I thoug...