Skip to content

Instantly share code, notes, and snippets.

@dvv
Created June 3, 2010 16:57
Show Gist options
  • Save dvv/424142 to your computer and use it in GitHub Desktop.
Save dvv/424142 to your computer and use it in GitHub Desktop.
Given:
=====
unconstrained querystring (Q),
zero or more chainable slices (S) to be applied on Q,
Range: items=A,B (R)
Task:
=====
determine:
"skip" (offset from start of Q),
"limit" (the final length of required recodrset, R being honored),
"count" (the length of constrained recodrset, which we denote as Q&S)
Content-Range: items=<skip>-<skip+limit-1>/<count>
Solution:
=====
Let's determine the result of sequential application of slices (deanlandolt, please)
In case when all slices have non-negative first parameters the resulting is slice(X,Y) where X=sum(Xi), Y=min(Yi-Xi) for all i.
So the length of the constrained (sliced) recordset is always the minimum of Y-X and len(S). That gives us
count := min(Y-X, len(S))
and we need not calculate the len(Q&S). Thus we need not mangle the initial querystring (Q).
Next, trivially:
skip := X+A
Next,
limit := min(count,B-A+1)
Conclusion:
=====
Using just arithmetics, we avoided mangling query and allowed any number of slices S
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment