Method: PEROBS::IDList#insert

Defined in:
lib/perobs/IDList.rb

#insert(id) ⇒ Object

Insert a new value into the list.

Parameters:

  • id (Integer)

    The value to add



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/perobs/IDList.rb', line 59

def insert(id)
  # Find the index of the page that should hold ID.
  index = @page_records.bsearch_index { |pr| pr.max_id >= id }
  # Get the corresponding IDListPageRecord object.
  page = @page_records[index]

  # In case the page is already full we'll have to create a new page.
  # There is no guarantee that a split will yield an page with space as we
  # split by ID range, not by distributing the values evenly across the
  # two pages.
  while page.is_full?
    new_page = page.split
    # Store the newly created page into the page_records list.
    @page_records.insert(index + 1, new_page)
    if id >= new_page.min_id
      # We need to insert the ID into the newly created page. Adjust index
      # and page reference accordingly.
      index += 1
      page = new_page
    end
  end

  # Insert the ID into the page.
  page.insert(id)
end