parasort 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 66d5cf035cc2c29c4529ca92fcc67b73aa9f78aa54bc6e0b26d9e639caad87ac
4
+ data.tar.gz: 44eef565ecc563a987c8d770a5bb805d87d97ed0aba0f98f6eb31496fe4517ee
5
+ SHA512:
6
+ metadata.gz: a899cda325161012e8a806f26a42e97def70580943c2d97a693824feebd8cf5c08b44e6f298b95f0db1858e56c258c1c78c8885e499e267271b1f3cb9f92e994
7
+ data.tar.gz: b516b58a8a32c32a8c9db5a50b77e018cb4f489600246fffae0c200e1afc6f2035861162f7f5d674cc83e1c6b9aab4d7de25781d2e459b107d8f8f0610a87099
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at TODO: Write your email address. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in parasort.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,23 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ parasort (0.1.0)
5
+ xenum (~> 0.1.4)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ minitest (5.22.2)
11
+ rake (13.1.0)
12
+ xenum (0.1.4)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ minitest (~> 5.0)
19
+ parasort!
20
+ rake (~> 13.0)
21
+
22
+ BUNDLED WITH
23
+ 2.4.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Parasort
2
+
3
+ Parallel sort
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'parasort'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install parasort
20
+
21
+ ## Usage
22
+
23
+ use as lib
24
+
25
+ ```ruby
26
+ require 'parasort'
27
+
28
+ Parasort.each(File.foreach(...)) do |line|
29
+ # ...
30
+ end
31
+ ```
32
+
33
+ use as cli
34
+
35
+ ```sh
36
+ seq 1 10 | parasort
37
+
38
+ # or
39
+ parasort file1 file2
40
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
data/exe/parasort ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "parasort"
4
+
5
+ Parasort.each(ARGF) do |line|
6
+ puts line
7
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Parasort
4
+ VERSION = "0.1.0"
5
+ end
data/lib/parasort.rb ADDED
@@ -0,0 +1,154 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "parasort/version"
4
+
5
+ require "fileutils"
6
+ require "xenum"
7
+
8
+ module Parasort
9
+ class << self
10
+ def each(lines, &block)
11
+ tempdir = File.join('/tmp', Time.now.strftime('%Y%m%d_%H%M%S'))
12
+ FileUtils.mkdir(tempdir)
13
+
14
+ compound = Compound.new(tempdir)
15
+ lines.each_slice(Atom::SIZE).each_with_index do |ls, i|
16
+ ls.sort!
17
+ dest = File.join(tempdir, "#{i}_#{i}")
18
+ File.open(dest, 'w'){ |f| f.puts ls }
19
+ compound.add(0, Atom.new(dest))
20
+ end
21
+ compound.pack!
22
+
23
+ compound.each(&block)
24
+ end
25
+ end
26
+
27
+ class Compound
28
+ include Enumerable
29
+
30
+ attr_reader :tempdir
31
+
32
+ GRANULES_COUNT = 128
33
+
34
+ def initialize(tempdir)
35
+ @tempdir = tempdir
36
+ @compound = Hash.new{ |h, k| h[k] = [] }
37
+ pid = Process.pid
38
+ at_exit{ FileUtils.rm_rf(tempdir) if pid == Process.pid }
39
+ end
40
+
41
+ def add(level, path)
42
+ @compound[level] << path
43
+ loop do
44
+ level, granules = @compound.detect{ |lvl, grans| grans.count >= GRANULES_COUNT }
45
+ break unless level
46
+
47
+ @compound[level + 1] << Molecule.new(tempdir, granules.dup)
48
+ @compound[level].clear
49
+ end
50
+ path
51
+ end
52
+
53
+ def pack!
54
+ loop do
55
+ break if @compound.each_value.map(&:count).sum <= GRANULES_COUNT
56
+
57
+ level, granules = @compound.detect{ |lvl, grans| !grans.empty? }
58
+ break unless level
59
+
60
+ @compound[level + 1] << Molecule.new(tempdir, granules.dup)
61
+ @compound[level].clear
62
+ end
63
+ end
64
+
65
+ def each(&block)
66
+ [].merge_sort(*@compound.flat_map{ |lvl, grans| grans.map(&:each) }).each(&block)
67
+ end
68
+ end
69
+
70
+ class Atom
71
+ attr_reader :path, :range
72
+
73
+ SIZE = ENV['PARASORT_ATOM_SIZE'].to_i.yield_self{ |n| n > 0 ? n : 10000 }
74
+
75
+ def initialize(path)
76
+ @path = path
77
+ @range = File.basename(@path).split('_').map(&:to_i)
78
+ end
79
+
80
+ def move_to_dir(dir)
81
+ FileUtils.mv(@path, dir)
82
+ @path = File.join(dir, File.basename(@path))
83
+ end
84
+
85
+ def each(&block)
86
+ File.foreach(@path, &block)
87
+ end
88
+ end
89
+
90
+ class Molecule
91
+ attr_reader :path, :range
92
+
93
+ MOLECULE_RATE = ENV['PARASORT_MOLECULE_RATE'].to_i.yield_self{ |n| n > 0 ? n : 10000 }
94
+
95
+ def initialize(dir, files)
96
+ @done = false
97
+ @lock = Mutex.new
98
+ @cond = ConditionVariable.new
99
+
100
+ @range = files.map(&:range).flatten.minmax
101
+ @path = File.join(dir, @range.join('_'))
102
+ @dir = dir
103
+
104
+ Thread.new(files.freeze) do |fs|
105
+ @lock.synchronize do
106
+ Process.wait(fork{ _merge(fs) })
107
+ @done = true
108
+ @cond.broadcast
109
+ end
110
+ end
111
+ end
112
+
113
+ def move_to_dir(dir)
114
+ wait_for_done
115
+ FileUtils.mv(@path, dir)
116
+ @path = File.join(dir, File.basename(@path))
117
+ end
118
+
119
+ def each(&block)
120
+ wait_for_done
121
+ File.foreach(@path, &block)
122
+ end
123
+
124
+ private
125
+
126
+ def wait_for_done
127
+ loop do
128
+ break if @done
129
+ @lock.synchronize do
130
+ next if @done
131
+ @cond.wait(@lock)
132
+ end
133
+ end
134
+ end
135
+
136
+ def _merge(files)
137
+ # move to dir
138
+ subdir = File.join(@dir, "merging_#{@range.join('_')}")
139
+ FileUtils.mkdir(subdir)
140
+ files.each{ |f| f.move_to_dir(subdir) }
141
+
142
+ # merge
143
+ File.open(@path, 'w') do |dest|
144
+ lineses = files.map(&:each)
145
+ [].merge_sort(*lineses).each_slice(MOLECULE_RATE) do |lines|
146
+ dest.puts lines
147
+ end
148
+ end
149
+
150
+ # tear down
151
+ FileUtils.rm_rf(subdir)
152
+ end
153
+ end
154
+ end
data/sig/parasort.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Parasort
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parasort
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ken
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-04-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: xenum
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.4
27
+ description:
28
+ email:
29
+ - block24block@gmail.com
30
+ executables:
31
+ - parasort
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - CODE_OF_CONDUCT.md
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - exe/parasort
42
+ - lib/parasort.rb
43
+ - lib/parasort/version.rb
44
+ - sig/parasort.rbs
45
+ homepage: https://github.com/turnon/parasort
46
+ licenses:
47
+ - MIT
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 2.6.0
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubygems_version: 3.4.1
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Parallel sort
68
+ test_files: []