I am trying to come up with an algorithm for getting items for a news feed from several sources.
There are multiple lists of data: S1, S2, … Sn. Each of these lists has a certain known amount of items, Q1, Q2, … Qn. I need to show these items with pagination, with a certain amount from each list per page, P1, P2, … Pn, with total amount of items per page T = sum(P). I get items from lists using offset and limit (in reality, they are SQL tables). Obviously, at some point i shall run out of items of a particular type, in which case i need to fill the blanks with items from other lists to get exactly T items for each page except the last one which can have less than T items.
Here’s a concrete example for N=2, Q1=27, Q2=95, P1=10, P2=20, T=10+20=30:
========================================================================== | Page | From S1 | S1 offset | S1 limit | From S2 | S2 offset | S2 limit | ========================================================================== | 1 | 10 | 0 | 10 | 20 | 0 | 20 | | 2 | 10 | 10 | 10 | 20 | 20 | 20 | | 3 | 7 | 20 | (7) | 23 | 40 | 23 | | 4 | 0 | 0 | (0) | 30 | 63 | 30 | | 5 | 0 | 0 | (0) | 2 | 93 | (2) | ==========================================================================
The numbers in parenthesis are not really important, because i can live with asking the SQL table for more entries than it has.
Ideally, i’m looking for an algorithm that can give me offsets and limits for a given page.
Any ideas?
4