Skip to content

Instantly share code, notes, and snippets.

@sandinist
Created June 30, 2012 19:39
Show Gist options
  • Save sandinist/3025233 to your computer and use it in GitHub Desktop.
Save sandinist/3025233 to your computer and use it in GitHub Desktop.
memo2
require 'mathn'
MAX = 30
AMAX = 10
BMAX = 20
def conditions(page, count_a, count_b)
max_page_a = (count_a / AMAX).ceil
max_page_b = (count_b / BMAX).ceil
return nil if page > max_page_a && page > max_page_b
return {offset_a: (page - 1) * AMAX, limit_a: AMAX, offset_b: (page - 1) * BMAX, limit_b: BMAX} if page < max_page_a && page < max_page_b
less_a = max_page_a < max_page_b
less_page = less_a ? max_page_a : max_page_b
less_count = less_a ? count_a : count_b
more_count = less_a ? count_b : count_a
less_max = less_a ? AMAX : BMAX
more_max = less_a ? BMAX : AMAX
mod = less_count % less_max
if less_page == page
h = {offset_less: (page - 1) * less_max , limit_less: nil, offset_more: (page - 1) * more_max, limit_more: more_max + mod}
else
more_count1 = (less_page * more_max) + mod
more2_page = ((more_count - more_count1) / MAX).ceil
more_mod = (more_count - more_count1) % MAX
if (more2_page + less_page) == page
final_offset = more_mod != 0 ? more_mod : MAX
h = {offset_less: nil , limit_less: nil, offset_more: more_count - final_offset, limit_more:nil }
elsif (more2_page + less_page) > page
offset_more = more_count1 + ((page - 1 - less_page) * MAX)
h = {offset_less: nil , limit_less: nil, offset_more: offset_more , limit_more: MAX}
else
return nil
end
end
if less_a
return {offset_a: h[:offset_less], limit_a: h[:limit_less], offset_b: h[:offset_more], limit_b: h[:limit_more]}
else
return {offset_a: h[:offset_more], limit_a: h[:limit_more], offset_b: h[:offset_less], limit_b: h[:limit_less]}
end
end
count_a = 105
count_b = 2005
p conditions(1, count_a, count_b)
p conditions(10, count_a, count_b)
p conditions(11, count_a, count_b)
p conditions(12, count_a, count_b)
p conditions(13, count_a, count_b)
p conditions(69, count_a, count_b)
p conditions(70, count_a, count_b)
p conditions(71, count_a, count_b)
p conditions(72, count_a, count_b)
p '-----'
count_a = 100
count_b = 2000
p conditions(1, count_a, count_b)
p conditions(9, count_a, count_b)
p conditions(10, count_a, count_b)
p conditions(11, count_a, count_b)
p conditions(12, count_a, count_b)
p conditions(69, count_a, count_b)
p conditions(70, count_a, count_b)
p conditions(71, count_a, count_b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment