c - Fast algorithm mapping int to monotonically increasing int subset -


i have encountered variations of problem multiple times, , became bottleneck in arithmetic coder implementation. given n (<= 256) segments of known non-negative size si laid out in order starting origin, , given x, want find n such

s0 + s1 + ... + sn-1 <= x < s0 + s1 + ... + sn

the catch lookups , updates done @ same frequency, , every update in form of increasing size of segment 1. also, bigger segment, higher probability looked or updated again.

obviously sort of tree seems obvious approach, have been unable come tree implementation satisfactorily takes advantage of known domain specific details.

given relatively small size of n, tried linear approaches, turned out considerably slower naive binary tree (even after optimization, starting of list numbers above half total)

similarly, tested introducing intermediate step remaps values in such way keep segments ordered size, make access faster used, added overhead exceeded gains.

sorry unclear title -- despite being basic problem, not aware of specific names it.

i suppose bst do... may try add new numeric member (int or long) each node keep sum of values of left descendants. you'll seek each item in approximately logarithmic time, , once item added, removed or modified you'll have update ancestors on returning path recursion. may apply self-organizing tree structure, example avl keep worst-case search optimal or splay tree optimize searches used items. take care update left-subtree-sums during rebalancing or splaying.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -