data-structures

refactoring question

Given a method public static string[] Foo(System.IO.Stream stream) { XmlTextWriter xmlWriter = new XmlTextWriter(stream, System.Text.Encoding.ASCII); xmlWriter.WriteStartDocument(); xmlWriter.WriteStartElement("Element"); xmlWriter.WriteEndElement(); xmlWriter.WriteEndDocument(); ...

Objects vs arrays in Javascript for key/value pairs

Say you have a very simple data structure: (personId, name) ...and you want to store a number of these in a javascript variable. As I see it you have three options: // a single object var people = { 1 : 'Joe', 3 : 'Sam', 8 : 'Eve' }; // or, an array of objects var people = [ { id: 1, name: 'Joe'}, { id: 3, name: ...

Linked lists or hash tables ??

I have a linked list of around 5000 entries ("NOT" inserted simultaneously), and I am traversing the list, looking for a particular entry on occasions (though this is not very often), should I consider Hash Table as a more optimum choice for this case, replacing the linked list (which is doubly-linked & linear) ?? Using C in Linux. ...

I need to have a key with multiple values. What datastructure would you recommend?

I have an string array filled with words from a sentence. words[0] = "the" words[1] = "dog" words[2] = "jumped" words[3] = "over" words[4] = "the" words[5] = "wall." words[6] = "the" words[7] = "cat" words[8] = "fell" words[9] = "off" words[10] = "the" words[10] = "house." etc. (Stupid example, but it works for this) Each word will be ...

data structure used to implement UNDO and REDO option

I want to implement UNDO and REDO option(as we see in MS word etc). Can you suggest me a data structure for it, and how can i implement it.? ...

What are some examples of problems that are best solved with graphs?

After reading Stevey Yegge's Get That Job At Google article, I found this little quote interesting: Whenever someone gives you a problem, think graphs. They are the most fundamental and flexible way of representing any kind of a relationship, so it's about a 50–50 shot that any interesting design problem has a graph involved in it. M...

Design of a database, based on a data structure

I have a data structure something like this: typedef struct tagSUB_DATA { double measuredValue; double standardDeviation; double calculatedValue; double weightedError; } SUB_DATA; typedef struct tagALL_THE_DATA { int aNumber; double aDouble; SUB_DATA measurements1; SUB_DATA measurements2; } ALL_THE_DAT...

Can I use arbitrary metrics to search KD-Trees?

I just finished implementing a kd-tree for doing fast nearest neighbor searches. I'm interested in playing around with different distance metrics other than the Euclidean distance. My understanding of the kd-tree is that the speedy kd-tree search is not guaranteed to give exact searches if the metric is non-Euclidean, which means that I ...

What sort of sorted datastructure is optimized for finding items within a range?

Say I have a bunch of objects with dates and I regularly want to find all the objects that fall between two arbitrary dates. What sort of datastructure would be good for this? ...

Plain, linked and double linked lists: When and Why?

In what situations should I use each kind of list? What are the advantages of each one? ...

How to traverse a binary tree in O(n) time without extra memory

Given a binary tree with an integer, Left & Right pointers, how can one traverse the tree in O(n) time and O(1) extra memory (no stack/queue/recursion)? This guy gave a solution which is not O(n) total time that encoded the current path as an integer (and thus works on for trees of limited depth). I am looking for the classical solutio...

Hierarchy & data structure in Java (or XML to Object conversion: best practices)

The problem: Let's say there is an XML file that contains both the data and the hierarchy of certain elements of interest to the application: <root> <node title="lvl1Node"> <node title="lvl2Node"> <node title="lvl3Node"></node> </node> </node> <node title="lvl1Node2"></node> <node title="lvl1Node3"> ...

Datastructure for googlemap like application?

Hi I am doing a maprouting application. Several people have suggested me, that I do a datastructure where I split the map in a grid. In theory it sounds really good, but I am not to sure because of the bad performance I get when I implement it. In the worst case you have to draw every road. If you divide the map in a grid, the sum of r...

Converting Endianess on a bit field structure

Hi I need to convert a bit-field structure from little-endian to big-endia architecture. What is the best way to do that, as there will be issues in byte boundaries, if I simply swap the structure elements. Ex Structure is: struct { unsigned int b1:1; unsigned int b2:8; unsigned int b3:7; unsigned int b4:8;...

Database Structures with a Large Number of Bit Fields

I have a class of data with a very large number of binary properties--151 (!) to be exact--and I am concerned with how to model this data structurally. Despite the internal efficiencies of storing bit-fields as bytes, my programming spidey senses are tingling at creating a table with 151 bit-fields (in addition to other properties). The...

How should I specify the type of JSON-like unstructured data in Scala?

I'm considering porting a very simple text-templating library to scala, mostly as an exercise in learning the language. The library is currently implemented in both Python and Javascript, and its basic operation more or less boils down to this (in python): template = CompiledTemplate('Text {spam} blah {eggs[1]}') data = { 'spam': 1, 'e...

How does a hash table work?

I'm looking for an explanation of how a hashtable works - in plain English for a simpleton like me! For example I know it takes the key, calculates the hash (how?) and then performs some kind of modulo to work out where it lies in the array that the value is stored, but that's where my knowledge stops. Could anyone clarify the process. ...

Best practice for incorrect parameters on a remove method

So I have an abstract data type called RegionModel with a series of values (Region), each mapped to an index. It's possible to remove a number of regions by calling: regionModel.removeRegions(index, numberOfRegionsToRemove); My question is what's the best way to handle a call when the index is valid : (between 0 (inclusive) and the ...

Persistent data structures in Java

Does anyone know a library or some at least some research on creating and using persistent data structures in Java? I don't refer to persistence as long term storage but persistence in terms of immutability (see Wikipedia entry). I'm currently exploring different ways to model an api for persistent structures. Using builders seems to be...

C# Collection whose items expire

I am writing a Console Application in C# in which I want to cache certain items for a predefined time (let's say 1 hour). I want items that have been added into this cache to be automatically removed after they expire. Is there a built-in data structure that I can use? Remember this is a Console App not a web app. ...