Skip to content

Instantly share code, notes, and snippets.

@sstelfox
Created July 11, 2014 16:07
Show Gist options
  • Save sstelfox/7c45ae9333b9fc30cfc3 to your computer and use it in GitHub Desktop.
Save sstelfox/7c45ae9333b9fc30cfc3 to your computer and use it in GitHub Desktop.
Playing around with list summarization
# Generate a list of all the possible port numbers with 1/80 randomly removed
# from the list.
ar = (1...(2 ** 16)).to_a.select { |i| rand(80) != 0 }
# Build a summary of the list
prev = ar[0]
summary = ar.slice_before do |e|
prev, prev2 = e, prev
prev2.succ != e
end.map do |a|
(a.first == a.last) ? a.first : a.first..a.last
end
summary2 = ar.enum_for(:chunk).with_index do |state, index|
state - index
end.map do |diff, group|
(group.first == group.last) ? group.first : (group.first..group.last)
end
# Print out and compare the relative sizes as they would appear within a JSON
# document.
puts "Normal: #{ar.map(&:to_s).join(',').length} bytes"
puts "Summary: #{summary.map(&:to_s).join(',').length} bytes"
puts "Summary 2: #{summary2.map(&:to_s).join(',').length} bytes"
# Rebuild the original array
str_sum = summary.map(&:to_s).join(',')
original_nums = str_sum.split(',').map do |rep|
first, last = rep.split('..')
last.nil? ? first.to_i : (first.to_i..last.to_i).to_a
end.flatten
# Rebuild the original array
str_sum = summary2.map(&:to_s).join(',')
original_nums2 = str_sum.split(',').map do |rep|
first, last = rep.split('..')
last.nil? ? first.to_i : (first.to_i..last.to_i).to_a
end.flatten
puts original_nums == ar
puts original_nums2 == ar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment