Created
July 3, 2012 14:48
-
-
Save sandinist/3040187 to your computer and use it in GitHub Desktop.
memo3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mathn' | |
MAX = 30 | |
AMAX = 10 | |
BMAX = 20 | |
class Pages | |
attr_accessor :name, :line, :line_of_page | |
def initialize; yield self; end | |
def last; (@line / @line_of_page).ceil; end | |
def line_at_last; (mod_line == 0) ? @line_of_page : mod_line ; end | |
def line_offset(page); @line_of_page * (page-1); end | |
def mod_line; @line % @line_of_page; end | |
def offset_key; "offset_#{name}".to_sym; end | |
def limit_key; "limit_#{name}".to_sym; end | |
end | |
def conditions(page, count_a, count_b) | |
@all_line = count_a + count_b | |
a = Pages.new {|p| p.name = :a; p.line = count_a; p.line_of_page = AMAX} | |
b = Pages.new {|p| p.name = :b; p.line = count_b; p.line_of_page = BMAX} | |
@boundary_page = [a.last, b.last].min | |
@all_page = (@all_line / MAX).ceil | |
a, b = b, a if a.last > b.last | |
case | |
when page > @all_page then nil | |
when page < @boundary_page | |
return before_boundary_offsets(page, a, b) | |
when page == @boundary_page | |
return on_boundary_offsets(page, a, b) | |
when page > @boundary_page | |
return over_boundary_offsets(page, a, b) | |
end | |
end | |
def before_boundary_offsets(page, a, b) | |
{}.tap do |h| | |
h[a.offset_key] = a.line_offset(page) | |
h[a.limit_key] = (page == a.last) ? a.line_at_last : a.line_of_page | |
h[b.offset_key] = b.line_offset(page) | |
h[b.limit_key] = (page == b.last) ? b.line_at_last : b.line_of_page | |
end | |
end | |
def on_boundary_offsets(page, a, b) | |
{}.tap do |h| | |
h[a.offset_key] = a.line_offset(page) | |
h[a.limit_key] = a.line_at_last | |
h[b.offset_key] = b.line_offset(page) | |
h[b.limit_key] = b.line_of_page + (a.line_of_page - a.line_at_last) | |
end | |
end | |
def over_boundary_offsets(page, a, b) | |
line_at_last_of_boundary = b.line_offset(@boundary_page + 1) | |
line_at_last_of_boundary += (a.mod_line == 0 ? 0 : a.line_at_last) | |
line_of_over_boundary = (page - 1 - @boundary_page) * MAX | |
{}.tap do |h| | |
h[a.offset_key] = nil | |
h[a.limit_key] = nil | |
h[b.offset_key] = line_at_last_of_boundary + line_of_over_boundary | |
h[b.limit_key] = page == @all_page ? @all_line % MAX : MAX | |
end | |
end | |
count_a = 105 | |
count_b = 2005 | |
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(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(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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment