id_stuffer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ id_stuffer*.gem
@@ -0,0 +1,42 @@
1
+ h1. IdStuffer
2
+
3
+ A way to efficiently store ids (i.e., possibly sequential numbers > 0), well suited for params-ecoding.
4
+
5
+ Note that the ids will be sorted and uniqued.
6
+
7
+ h2. Examples
8
+
9
+ <pre>
10
+ > require 'id_stuffer'
11
+ => true
12
+ > l = IdStuffer.stuff [1,2,3,4,5,6,7,10,11,12,15,16,17]
13
+ => "IdS1ulxye8j3x"
14
+ > IdStuffer.unstuff(l)
15
+ => [1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 15, 16, 17]
16
+ </pre>
17
+
18
+ It's efficiency depends on the ocurrence of 'runs':
19
+
20
+ <pre>
21
+ > IdStuffer.stuff [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
22
+ => "IdS1eh"
23
+ > IdStuffer.stuff [1,2,3,4,5,6,7,10,11,12,15,16,17]
24
+ => "IdS1ulxye8j3x"
25
+ > IdStuffer.stuff [1,4,7,10,13,16,19,22,25,26,29,31,35]
26
+ => "IdS14iltvlqc3t1ifhh1o8wnmf"
27
+ </pre>
28
+
29
+ h2. How it's done
30
+
31
+ It's pretty simple, but you can find more infos in <a href="http://blog.metaminded.com/2011/02/08/how-to-efficiently-store-lists-of-ids-in-ruby/" target="_blank">our blog</a>.
32
+
33
+ h2. License
34
+
35
+ Copyright (c) 2010-2011 Peter Horn, <a href="http://www.provideal.net" target="_blank">Provideal GmbH</a>
36
+
37
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
40
+
41
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42
+
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "id_stuffer"
6
+ s.version = "0.0.1"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.summary = "a reasonable way to compress sequences of ids for parameter encoding"
9
+ s.email = "info@provideal.net"
10
+ s.homepage = "http://github.com/provideal/id_stuffer"
11
+ s.description = "a way to efficiently store ids (i.e., possibly sequential numbers > 0), well suited for params-ecoding. Note that the ids will be sorted and uniqued"
12
+ s.authors = ['Peter Horn']
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+ end
@@ -0,0 +1,38 @@
1
+ module IdStuffer
2
+
3
+ PREFIX = 'IdS'
4
+
5
+ # compress the list of ids as good as I could imagine ;)
6
+ # uses fancy base twisting
7
+ def self.stuff(list)
8
+ return PREFIX if list.length == 0
9
+ PREFIX.dup << (list.sort.uniq.map(&:to_i).inject([[-9,-9]]) do |l, c|
10
+ if l.last.last+1 == c
11
+ l.last[-1] = c
12
+ l
13
+ else
14
+ l << [c,c]
15
+ end
16
+ end.map do |r|
17
+ if r.first == r.last
18
+ r.first.to_s(8)
19
+ else
20
+ r.first.to_s(8) << "8" << r.last.to_s(8)
21
+ end
22
+ end[1..-1].join("9").to_i.to_s(36))
23
+ end
24
+
25
+ # inverse of compress_id_list
26
+ def self.unstuff(str)
27
+ return [] if !str or str.length==0 or str=='0' or str==PREFIX
28
+ raise "Corrupted id list. Starts w/o mandatory prefix" unless str.start_with?(PREFIX)
29
+ n = str[PREFIX.length..-1].to_i(36).to_s.split("9").map do |e|
30
+ p = e.split("8")
31
+ if p.length == 1 then p[0].to_i(8)
32
+ elsif p.length == 2 then (p[0].to_i(8)..p[1].to_i(8)).entries
33
+ else raise "Corrupted id list. Or a bug ;)"
34
+ end
35
+ end.flatten
36
+ end
37
+
38
+ end
@@ -0,0 +1,59 @@
1
+ require 'test/unit'
2
+ require 'id_stuffer'
3
+
4
+ class TestIdStuffer < Test::Unit::TestCase
5
+ def test_empty
6
+ l = []
7
+ c = IdStuffer.stuff(l)
8
+ ll = IdStuffer.unstuff(c)
9
+ assert_equal(l,ll)
10
+ end
11
+
12
+ def test_empty2
13
+ assert_equal(IdStuffer.unstuff(nil),[])
14
+ assert_equal(IdStuffer.unstuff(''),[])
15
+ assert_equal(IdStuffer.unstuff("0"),[])
16
+ assert_equal(IdStuffer.unstuff(IdStuffer::PREFIX),[])
17
+ end
18
+
19
+ def test_simple
20
+ l = [1,2,3,4,5,6,7]
21
+ c = IdStuffer.stuff(l)
22
+ ll = IdStuffer.unstuff(c)
23
+ assert_equal(l,ll)
24
+ end
25
+
26
+ def test_simple2
27
+ l = [1,12,23,34,45,56,67]
28
+ c = IdStuffer.stuff(l)
29
+ ll = IdStuffer.unstuff(c)
30
+ assert_equal(l,ll)
31
+ end
32
+
33
+ def test_random
34
+ max = 300
35
+ 100.times do |i|
36
+ n = 10*(i+1)
37
+ l = (1..n).map{|i| 1+(max*rand()).to_i }.sort.uniq
38
+ c = IdStuffer.stuff(l)
39
+ ll = IdStuffer.unstuff(c)
40
+ assert_equal(l,ll)
41
+ end
42
+ end
43
+
44
+ def test_sort
45
+ l = [7,1,6,3,5,4]
46
+ c = IdStuffer.stuff(l)
47
+ ll = IdStuffer.unstuff(c)
48
+ assert_equal(l.sort,ll)
49
+ end
50
+
51
+ def test_uniq
52
+ l = [1,2,2,2,3,4,5,6,7,2,2,2,3,3,3,4,5]
53
+ c = IdStuffer.stuff(l)
54
+ ll = IdStuffer.unstuff(c)
55
+ assert_equal(l.sort.uniq,ll)
56
+ end
57
+
58
+
59
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: id_stuffer
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Peter Horn
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-14 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: a way to efficiently store ids (i.e., possibly sequential numbers > 0), well suited for params-ecoding. Note that the ids will be sorted and uniqued
18
+ email: info@provideal.net
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - .gitignore
27
+ - README.textile
28
+ - id_stuffer.gemspec
29
+ - lib/id_stuffer.rb
30
+ - test/id_stuffer.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/provideal/id_stuffer
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.5.0
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: a reasonable way to compress sequences of ids for parameter encoding
59
+ test_files:
60
+ - test/id_stuffer.rb