Data structure for O(log N) find and update, considering small L1 cache

后端 未结 3 1622
夕颜
夕颜 2021-02-12 15:41

I\'m currently working on an embedded device project where I\'m running into performance problems. Profiling has located an O(N) operation that I\'d like to eliminate.

I

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-12 16:29

    std::set usually provides the O(log(n)) insert and delete by using a binary search tree. This unfortunately uses 3*N space for most pointer based implementations. Assuming word sized data, 1 for data, 2 for pointers to left and right child on each node.

    If you have some constant N and can guarantee that ceil(log2(N)) is less than half the word size you can use a fixed length array of tree nodes each 2*N size. Use 1 for data, 1 for the indexes of the two child nodes, stored as the upper and lower half of the word. Whether this would let you use a self balancing binary search tree of some manner depends on your N and word size. For a 16 bit system you only get N = 256, but for 32 its 65k.

提交回复
热议问题