Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If your i,k,...,z are arbitrary, then no algorithm with a time constraint of O(1) and a reasonable space constraint of &lt; O(N) comes to mind. If there is some regularity in your bounds, then of course you can put that into use: e.g. if each range contains an even number of elements, then a simple solution be to have a lookup table that is acccessed using [i_N/2]. In the general case, you would need to have a matching folding-function for your number 0...N - one that satisfies the constraint k-mapping(i) != k-mapping(i+1) => folding-function(i) != folding-function(i+1).</p> <p>If only some elements in your map are accessed frequently, you could also build a cache for the mapping, leading to O(1) in the cache-hit case and O(lookup-function) for the cache-miss. Of course, the cache lookup would impact the time spent for each element, even in case of a cache-miss, but it would not affect the O-complexity of your algorithm.</p> <h3>Edit</h3> <p>Basically what you want to implement can be expressed as a Pascal case statement with sets as labels:</p> <pre><code>case n of 0..i-1: value:=0; i..i+k-1: value:=1; // ... i+k+...+z+2..N-1: value:=K; end </code></pre> <p>For this problem, a Google search indeed turns out an interesting paper:</p> <blockquote> <p>This paper descripts a new scheme for building static search trees, using multiway radix search trees. [...] We show that, for sparse case sets, the method produces faster code on average than existing methods, requiring <em>O</em>(1) time with a small constant for the average search.</p> <p><em>Erlingsson, Krishnamoorthy, Raman</em>: <a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.48.2872&amp;rep=rep1&amp;type=pdf" rel="nofollow">Lucid and Efficient Case Analysis</a></p> </blockquote> <p>Alas, you don't have a sparse case set, but as far as I can see, the algorithm should require only minimal adaption for dense case sets that have lots of consecutive results.</p> <p>For the general problem of creating efficient code for case statements, Zhao and Amarla have an <a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.69.7532&amp;rep=rep1&amp;type=pdf" rel="nofollow">interesting paper that uses profiling</a> to achieve something similar to using a cache. Their paper is also interesting for the related work section, which refers to a paper from <em>Kannan and Proebsting</em> (<em>Correction to producing good code for the case statement.</em>) which requires an O(n^2) setup time for clustering. However, I have not access to this paper, and it sounds like that this would only produce an extremely optimized search tree, therefore leading to a runtime of O(log(n)).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload