Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack and Hash joint

I'm trying to write a data structure which is a combination of Stack and HashSet with fast push/pop/membership (I'm looking for constant time operations). Think of Python's OrderedDict.

I tried a few things and I came up with the following code: HashInt and SetInt. I need to add some documentation to the source, but basically I use a hash with linear probing to store indices in a vector of the keys. Since linear probing always puts the last element at the end of a continuous range of already filled cells, pop() can be implemented very easy without a sophisticated remove operation.

I have the following problems:

  • the data structure consumes a lot of memory (some improvement is obvious: stackKeys is larger than needed).
  • some operations are slower than if I have used fastutil (eg: pop(), even push() in some scenarios). I tried rewriting the classes using fastutil and trove4j, but the overall speed of my application halved.

What performance improvements would you suggest for my code? What open-source library/code do you know that I can try?

like image 380
Alexandru Avatar asked Nov 06 '22 13:11

Alexandru


1 Answers

You've already got a pretty good implementation. The only improvement obvious to me is that you do more work than you need to by searching when popping. You should store in the stack not the key itself but the index into the key array. This gives you trivially fast pops at the expense of only one more pointer indirection when you want to peek the last item.

Just size your stack to LOAD_FACTOR*(heap array size), in addition to that, and you should have about as fast an implementation as you could expect with as little memory as you can manage given your speed requirements.

like image 123
Rex Kerr Avatar answered Nov 12 '22 17:11

Rex Kerr