run_length_encoder 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +35 -0
- data/README.md +34 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/run_length_encoder.rb +74 -0
- data/lib/run_length_encoder/version.rb +3 -0
- data/run_length_encoder.gemspec +25 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cf3014df4a2330945531b20780b72e647bf05a8893c5d3d88ed0267843958423
|
4
|
+
data.tar.gz: a84fe707d88be101a3a7b4edadd22eb09ec1b1c5b5d336fd71e6f1f378679710
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f005851f3066fa01fbff66c42436ac70d8421d487b3fa1d28bb6d127f454d042a07c8e136a74ce6f19e4c65c8d24221f5fb1f3d94f42be5fce1fcf8d699a47e3
|
7
|
+
data.tar.gz: 3b81577924484fe368cd57a7a603c26e36d5fe62922008fa04c2dbeac9f010f787a47c1eec61fa33704b136fb9cea124ed2519a551d6853c945a31cc9d171d66
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
run_length_encoder (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.7.0)
|
12
|
+
rspec-core (~> 3.7.0)
|
13
|
+
rspec-expectations (~> 3.7.0)
|
14
|
+
rspec-mocks (~> 3.7.0)
|
15
|
+
rspec-core (3.7.1)
|
16
|
+
rspec-support (~> 3.7.0)
|
17
|
+
rspec-expectations (3.7.0)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.7.0)
|
20
|
+
rspec-mocks (3.7.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.7.0)
|
23
|
+
rspec-support (3.7.1)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.16)
|
30
|
+
rake (~> 10.0)
|
31
|
+
rspec (~> 3.0)
|
32
|
+
run_length_encoder!
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.16.1
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# run_length_encoder
|
2
|
+
|
3
|
+
run-length encode/decode stuff
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```rb
|
8
|
+
gem 'run_length_encoder'
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
```rb
|
14
|
+
require 'run_length_encoder'
|
15
|
+
|
16
|
+
# Basic usage
|
17
|
+
# (see `RunLengthEncoder::Instance#initialize` for supported options)
|
18
|
+
rle = RunLengthEncoder.new
|
19
|
+
rle.encode("0000111010")
|
20
|
+
=> "4:0;3:1;1:0;1:1;1:0"
|
21
|
+
|
22
|
+
rle.decode("4:0;3:1;1:0;1:1;1:0")
|
23
|
+
=> "0000111010"
|
24
|
+
|
25
|
+
# You can use built-in configurations
|
26
|
+
rle = RunLengthEncoder.for_integer_array
|
27
|
+
rle.encode([0, 0, 0, 0, 1, 1, 1, 0, 1, 0])
|
28
|
+
=> "4:0;3:1;1:0;1:1;1:0"
|
29
|
+
|
30
|
+
rle.decode("4:0;3:1;1:0;1:1;1:0")
|
31
|
+
=> [0, 0, 0, 0, 1, 1, 1, 0, 1, 0]
|
32
|
+
|
33
|
+
# encode/decode are aliased to dump/load for integration with ActiveRecord::Serialization
|
34
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "run_length_encoder"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require "run_length_encoder/version"
|
2
|
+
|
3
|
+
module RunLengthEncoder
|
4
|
+
def self.new(opts = {})
|
5
|
+
Instance.new(opts)
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.for_integer_array(opts = {})
|
9
|
+
default_opts = {
|
10
|
+
converter: RunLengthEncoder::Converters.method(:integer_array)
|
11
|
+
}
|
12
|
+
Instance.new(default_opts.merge(opts))
|
13
|
+
end
|
14
|
+
|
15
|
+
module Converters
|
16
|
+
def self.integer_array(splits)
|
17
|
+
splits.reduce([]) do |acc, (count, n)|
|
18
|
+
acc + count.times.map { n.to_i }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Instance
|
24
|
+
def initialize(count_separator: ":", term_separator: ";", converter: default_converter)
|
25
|
+
@count_separator = count_separator
|
26
|
+
@term_separator = term_separator
|
27
|
+
@converter = converter
|
28
|
+
end
|
29
|
+
|
30
|
+
def encode(string_or_array)
|
31
|
+
case string_or_array
|
32
|
+
when String
|
33
|
+
_encode(string_or_array.each_char)
|
34
|
+
when Array
|
35
|
+
_encode(string_or_array.each)
|
36
|
+
else
|
37
|
+
raise "Unsupported type: #{string_or_array.class}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
alias :dump :encode
|
41
|
+
|
42
|
+
def decode(encoded)
|
43
|
+
terms = encoded.split(@term_separator)
|
44
|
+
splits = terms.map { |term| term.split(@count_separator) }
|
45
|
+
splits2 = splits.map { |(count, char)| [count.to_i, char] }
|
46
|
+
@converter[splits2]
|
47
|
+
end
|
48
|
+
alias :load :decode
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def _encode(enum)
|
53
|
+
acc = []
|
54
|
+
curr = nil
|
55
|
+
count = 0
|
56
|
+
flush = proc { |count, curr| acc << "#{count}#{@count_separator}#{curr}" if count > 0 }
|
57
|
+
enum.each do |el|
|
58
|
+
if el == curr
|
59
|
+
count += 1
|
60
|
+
else
|
61
|
+
flush[count, curr]
|
62
|
+
curr = el
|
63
|
+
count = 1
|
64
|
+
end
|
65
|
+
end
|
66
|
+
flush[count, curr]
|
67
|
+
acc.join(@term_separator)
|
68
|
+
end
|
69
|
+
|
70
|
+
def default_converter
|
71
|
+
proc { |splits| splits.reduce("") { |acc, (count, char)| acc << char * count }}
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "run_length_encoder/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "run_length_encoder"
|
8
|
+
spec.version = RunLengthEncoder::VERSION
|
9
|
+
spec.authors = ["Josh Bodah"]
|
10
|
+
spec.email = ["joshuabodah@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{run-length encode/decode stuff}
|
13
|
+
spec.homepage = "https://github.com/jbodah/run_length_encoder"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: run_length_encoder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Josh Bodah
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- joshuabodah@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/run_length_encoder.rb
|
72
|
+
- lib/run_length_encoder/version.rb
|
73
|
+
- run_length_encoder.gemspec
|
74
|
+
homepage: https://github.com/jbodah/run_length_encoder
|
75
|
+
licenses: []
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.7.3
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: run-length encode/decode stuff
|
97
|
+
test_files: []
|