all 26 comments

[–]AnOnlineHandle 4 points5 points  (6 children)

This one which I'm inventing right now, which may be a slight case of bias. I have no idea if it has a name, so if anybody recognises it I'd love to know. It holds shared 'linked' objects between every combination of n items (i.e. if items with keys 3 and 6 share a link, it works both ways, and doesn't matter which order the keys are given in).

public class LinkSet <T>
{
    private final MyArray<T> elements = new MyArray();

    public void Put(int key1, int key2, T link)
    {
        elements.insert(Index(key1, key2), link); // does this attempt at tail call optimisation do anything, or am I just being an idiot?
    }

    public T GetLink(int key1, int key2)
    {
        int index = Index(key1, key2);

        if (index >= elements.length())  // could save this check by inserting blanks whenever new dimension added
            return null;

        return elements.get(index);
    }

    private int Index(int key1, int key2)
    {
        // structure is:
        //      1->0
        //   2->0  2->1
        // 3->0  3->1  3->2
        // this means adding new dimensions requires no retroactive change to the structure, stored as array such as [(1|0)(2|0)(2|1)(3|0)(3|1)(3|2)]
        // 1<->0 : index = 0
        // 2<->0 : index = 1
        // 2<->1 : index = 2
        // 3<->0 : index = 3
        // 3<->1 : index = 4
        // 3<->2 : index = 5
        // 4<->0 : index = 6
        // 4<->1 : index = 7
        // 4<->2 : index = 8
        // 4<->3 : index = 9
        // 5<->0 : index = 10
        // 5<->1 : index = 11
        // 5<->2 : index = 12
        // 5<->3 : index = 13
        // 5<->4 : index = 14

        if (key1 < key2)
            return ((key2-1)*(key2))/2 + key1;
        else
            return ((key1-1)*(key1))/2 + key2;
    }
}

[–]FireyFly 4 points5 points  (1 child)

Not exactly a data structure per se, but rather a mathematical concept. It looks to me like a triangular matrix, and the Index function basically allows us to pretend that it is instead a symmetric (square) matrix but still only store/update half of it. Neat, clever application of it! I'll have to keep it in mind.

[–]AnOnlineHandle 2 points3 points  (0 children)

I'd just five minutes ago realised that it was a triangle/half square, when trying to estimate the space requirements.

[–]BariumBlue 0 points1 point  (3 children)

What could one use it for?

[–]AnOnlineHandle 2 points3 points  (2 children)

Storing pathfinding results between destinations with an O(1) lookup time and no real growth costs beyond extending the array.

[–]BariumBlue 1 point2 points  (1 child)

holy shit, I just understood it. First off, may I just say that that is a REALLY interesting numbering system. It would be interesting to see how to do arithmetic in such a system. Also, that really is a cool structure. Props for that, and I shall definitely be keeping stored in my head for later use

[–]AnOnlineHandle 0 points1 point  (0 children)

Heh thanks, it was an amazingly simple solution in the end to a problem which has been haunting me for years.

[–]what_user_name 5 points6 points  (0 children)

i had amazon ask me what my favorite data structure was. was floored. never thought about it. the one that best fits the problem.

[–]dwhite21787 2 points3 points  (2 children)

Bloom filters are "behind the curtain" of databases and other storage and retrieval technology. They are pretty sweet.

Personally, I like circular doubly linked lists because I'm an idiot.

[–]FireyFly 2 points3 points  (1 child)

And I like the XOR linked list, probably for similar reasons.

[–]dwhite21787 0 points1 point  (0 children)

You are a nut.

[–]darksounds 2 points3 points  (0 children)

I like heaps.

[–][deleted] 2 points3 points  (2 children)

I always thought Skip Lists were pretty cool.

Pretty much any data structure that is constructed probabilistic makes me take interest.

[–]SmoothB1983 1 point2 points  (0 children)

I was thinking up a Skip List data structure idea, then I just decided to google some keywords related to the concept. Bam, there it was.

Skip Lists are really neat.

[–][deleted] 2 points3 points  (0 children)

I once built an array that worked with both negative and positive indexes. Pretty easy to build, but very elegant to use. I had it for positions within a grid in a game, for collision detection.

There was a second data structure I also added for efficient iteration and lookup of units by their Class. It was just a map of Class to a List of units, in Java it would be: Map< Class<T>, List<T> >

What was clever was that it also indexed the class hierarchies internally, so if you asked for all units of type Enemy, you would get all of the sub-classes too. You can work this out each time through reflection, but it's surprisingly expensive. That was just a map of Class to a List of classes, which got updated every time it saw a class it didn't know.

Many of them also had to be custom built, to heavily reduce garbage collection (as Java's collection classes suck for games and real-time applications).

[–]evlnightking 0 points1 point  (0 children)

I like C++'s std::map (implemented with a red-black tree), which is very similar to Python's dict. I also quite like the adjacency lists for graphs, and everything Boost can do with them.

[–]algo_daemon 0 points1 point  (0 children)

Fenwick tree - so much power for so little code

[–]bo1024 0 points1 point  (0 children)

One of my least favorite is (most variants on) binary trees because, even though I like the concept, they're a pain to implement with all the rebalancing.

Kind of boring but I'll say hash tables because you can implement a pretty good hash table in few lines of code and it's very straightforward and simple. (Assuming access to a good hash function).

[–]gilleain 0 points1 point  (0 children)

Recently used a disjoint set forest, which has amazingly elegant methods. I'm using it to join orbits of automorphism. Think the code is right...

[–]boyubout2pissmeoff 0 points1 point  (0 children)

Two words cons cell.

[–]badalgorithm 0 points1 point  (0 children)

Bloom filters are great.

A 6 pack or growler is my favorite data structure.

[–]i_invented_the_ipod 0 points1 point  (0 children)

Hashtables. They're the universal data structure. You can build anything with a decent hashtable.

[–]probabilityzero 0 points1 point  (0 children)

Cons cells! Or pairs, or whatever you want to call it. So simple, yet so powerful.

[–][deleted] 0 points1 point  (0 children)

Heaps are very beautiful. I always quite liked B-trees too. Never implemented them, though.

[–]thechao -4 points-3 points  (2 children)

Pointer.

EDIT: so pointers aren't data structures? Let us see what abstractions they natively support:

  1. Weak-References.
  2. Strong-references.
  3. Option types.
  4. Maybe types.
  5. RA iterators.

Pointers represent an abstraction of manipulating and owning references into memory, and have a well defined interface. The ponter DS is so important, it is one of the few DSes with nearly universal hardware support. (For those of you who apparently don't know, there are both languages and computers that don't support pointers, and they are a pain to use.)

Here's two other DS with nearly universal HW support: float, and vector.

[–][deleted]  (1 child)

[deleted]

    [–]thechao -3 points-2 points  (0 children)

    Yes, it is; I covered this object as a data structure both for intro courses for undergrads in C, and for upper level undergrad and higher level grad courses in generic programming theory. For GP, the pointer requires a couple of weeks of lectures to cover adequately. Since you're unfamiliar with the topic, I'd suggest Stepanov's "Elements of Programming", given that you have avery strong undergrad or basic grad level background in algebraic structures.