0

I'm looking for most optimal way to store objects containing two properties, by which object could be looked up, in order. Easy way to insert new objects before and after specified key (one of property of the stored objects) is required too.

I am currently experimenting with SplDoublyLinkedList, which has a nice property of always being ordered and functionality of inserting nodes by index by add($index, $object);. DLL however requires looping through whole list to perform any lookup and additional loop to reset internal pointer to remembered position.

I was thinking about classic php array, but it needs ugly array_splice hacks to perform insertions (splitting array into keys and values, slicing both arrays in desired index, adding key and value into first keys and values arrays, merging resulting arrays and then combining keys with values) and keeping track of current position is less intuitive.

What would be my best bet?

Łukasz Biały
  • 431
  • 5
  • 14
  • 1
    Instead of asking for the 'most optimal way' - which i have no way of knowing - and whatever way i suggest - someone will announce a 'better way'. Why don't you suggest some of the ways you have thought of, their disadvantages and ask if, maybe there are other ways? Also include the 'size' of the issue you are trying to solve. 100 pair of objects - not a problem - 2 billion pairs of objects - gets 'interesting' ;-/ – Ryan Vincent Feb 15 '15 at 04:23
  • thanks for your comment, I went with two associative arrays keeping both keys and references to objects, works much faster than SplDoublyLinkedList, even if method to insert objects is somewhat hacky. btw.: I did suggest 2 ways I've been thinking of, I actually implemented both and benchmarked them, array based solution with 2 associative arrays is much faster. regarding size: shouldn't ever pass 100 values and that's a long shot, 30 max is more expected. – Łukasz Biały Feb 15 '15 at 14:28

0 Answers0