restricted_set 1.0.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
+ SHA1:
3
+ metadata.gz: e44fdc70b454bc1e49389fb77ec533615f656274
4
+ data.tar.gz: fdb4d797e39d26a108a5702ea7c8f5793b3c6c16
5
+ SHA512:
6
+ metadata.gz: ec90d032291087b4199e2f516f1e71324a13cea604a439bc163d905442c04f2c7c3b1d5e707de863f0d12fef3f04fdbee5a1e5a81b2a5318a951f746e88e6fe0
7
+ data.tar.gz: 4981211757d0cd5232de1a28a395ccaa94f571a4b8cc0abdce042ba6531ff3c3ae3e35ed64c48d0a2c95f126222e529a7177f458495263c149d043f5e369add6
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - jruby-19mode
6
+ - rbx-2.1.1
7
+ - ruby-head
8
+ - jruby-head
data/BSDL ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions
5
+ are met:
6
+ 1. Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ 2. Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+
12
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
16
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18
+ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21
+ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22
+ SUCH DAMAGE.
data/COPYING ADDED
@@ -0,0 +1,56 @@
1
+ Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.jp>.
2
+ You can redistribute it and/or modify it under either the terms of the
3
+ 2-clause BSDL (see the file BSDL), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) give non-standard binaries non-standard names, with
21
+ instructions on where to get the original software distribution.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or binary form,
26
+ provided that you do at least ONE of the following:
27
+
28
+ a) distribute the binaries and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c) give non-standard binaries non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under these terms.
43
+
44
+ For the list of those files and their copying conditions, see the
45
+ file LEGAL.
46
+
47
+ 5. The scripts and library files supplied as input to or produced as
48
+ output from the software do not automatically fall under the
49
+ copyright of the software, but belong to whomever generated them,
50
+ and may be sold commercially, and may be aggregated with this
51
+ software.
52
+
53
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
54
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
55
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56
+ PURPOSE.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # RestrictedSet [![Build Status](https://travis-ci.org/invisiblefunnel/restricted_set.png?branch=master)](https://travis-ci.org/invisiblefunnel/restricted_set)
2
+
3
+ > RestricedSet implements a set with restrictions defined by a given block. If the block's arity is 2, it is called with the RestrictedSet itself and an object to see if the object is allowed to be put in the set. Otherwise, the block is called with an object to see if the object is allowed to be put in the set.
4
+
5
+ > Credit: [lib/set.rb][ruby-restricted-set]
6
+
7
+ This project is inspired by a data structure described in the Ruby codebase. The interface matches the described class and the implementation is complete, tested and avoids dynamically defining methods on each instance.
8
+
9
+ **Compatible with Ruby 1.9+**
10
+
11
+ The source is released under the same terms as [Ruby][ruby-license].
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'restricted_set'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ ```console
24
+ $ bundle
25
+ ```
26
+
27
+ Or install it yourself as:
28
+
29
+ ```console
30
+ $ gem install restricted_set
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ ```ruby
36
+ set = RestrictedSet.new(-2..2) { |o| o > 0 }
37
+ set.add(-1)
38
+ set.add(42)
39
+ set #=> #<RestrictedSet: {1, 2, 42}>
40
+ ```
41
+
42
+ ```ruby
43
+ set = RestrictedSet.new { |o| Symbol === o }
44
+ set.add("Hello!")
45
+ set.add(:name)
46
+ set.add(99)
47
+ set.add(:location)
48
+ set #=> #<RestrictedSet: {:name, :location}>
49
+ ```
50
+
51
+ ```ruby
52
+ set = RestrictedSet.new do |current_set, _|
53
+ current_set.count + 1 <= 2 # Maximum of 2 objects
54
+ end
55
+ set << :a << :b << :c
56
+ set #=> #<RestrictedSet: {:a, :b}>
57
+ ```
58
+
59
+ ```ruby
60
+ require 'prime'
61
+ class PrimeSet < RestrictedSet
62
+ def initialize(enum = nil)
63
+ super enum, &Prime.method(:prime?)
64
+ end
65
+ end
66
+
67
+ (1..100).to_set(PrimeSet) #=> #<PrimeSet: {2, 3, 5, 7, 11, 13, 17, 19, ...}>
68
+ ```
69
+
70
+ ## Contributing
71
+
72
+ 1. Fork it
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create new Pull Request
77
+
78
+ [ruby-restricted-set]: https://github.com/ruby/ruby/blob/v2_0_0_247/lib/set.rb#L640-L665
79
+ [ruby-license]: https://www.ruby-lang.org/en/about/license.txt
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ task default: :test
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.pattern = "test/*_test.rb"
8
+ end
@@ -0,0 +1,50 @@
1
+ # The following copyright notice is included because parts of this
2
+ # library are modified from code in Ruby's codebase.
3
+
4
+ # Copyright (c) 2002-2013 Akinori MUSHA <knu@iDaemons.org>
5
+ #
6
+ # Documentation by Akinori MUSHA and Gavin Sinclair.
7
+ #
8
+ # All rights reserved. You can redistribute and/or modify it under the same
9
+ # terms as Ruby.
10
+
11
+ require "set"
12
+
13
+ class RestrictedSet < Set
14
+ def initialize(enum = nil, &block)
15
+ raise ArgumentError, "must provide a block" unless block_given?
16
+
17
+ @proc = if block.arity == 2
18
+ block.curry[self]
19
+ else
20
+ block
21
+ end
22
+
23
+ # Pass the identity proc to super to prevent
24
+ # passing the given block argument through
25
+ super(enum) { |o| o }
26
+ end
27
+
28
+ def add(o)
29
+ if @proc.call(o)
30
+ @hash[o] = true
31
+ end
32
+ self
33
+ end
34
+ alias << add
35
+
36
+ def add?(o)
37
+ if include?(o) || !@proc.call(o)
38
+ nil
39
+ else
40
+ @hash[o] = true
41
+ self
42
+ end
43
+ end
44
+
45
+ def restriction_proc
46
+ @proc
47
+ end
48
+
49
+ VERSION = "1.0.0"
50
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "restricted_set"
7
+ spec.version = "1.0.0"
8
+ spec.authors = ["Danny Whalen"]
9
+ spec.email = ["daniel.r.whalen@gmail.com"]
10
+ spec.description = %q{RestricedSet implements a set with restrictions defined by a given block.}
11
+ spec.summary = <<-SUMMARY.gsub(/\n/, ' ').strip
12
+ RestricedSet implements a set with restrictions defined by a given block.
13
+ If the block's arity is 2, it is called with the RestrictedSet itself and
14
+ an object to see if the object is allowed to be put in the set. Otherwise,
15
+ the block is called with an object to see if the object is allowed to be
16
+ put in the set.
17
+ SUMMARY
18
+ spec.homepage = "https://github.com/invisiblefunnel/restricted_set"
19
+ spec.license = "BSD"
20
+
21
+ spec.files = `git ls-files`.split($/)
22
+ spec.test_files = spec.files.grep(%r{^test/})
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "minitest", "~> 5.0"
27
+ end
@@ -0,0 +1,98 @@
1
+ require File.expand_path("../../lib/restricted_set", __FILE__)
2
+ require "minitest/autorun"
3
+
4
+ class RestrictedSetTest < Minitest::Test
5
+ def setup
6
+ @enum = [-2, 3, -1, 1, -3, 2, 0]
7
+ @arity_1_proc = Proc.new { |o| o > 0 }
8
+ @arity_2_proc = Proc.new { |rs, o| rs.empty? ? true : o > rs.max }
9
+ end
10
+
11
+ def test_superclass
12
+ assert RestrictedSet < Set
13
+ end
14
+
15
+ def test_new
16
+ assert_raises(ArgumentError) { RestrictedSet.new }
17
+ assert_raises(ArgumentError) { RestrictedSet.new(@enum) }
18
+
19
+ s = RestrictedSet.new(@enum, &@arity_1_proc)
20
+ assert_equal [1,2,3], s.sort
21
+
22
+ s = RestrictedSet.new(@enum, &@arity_2_proc)
23
+ assert_equal [-2,3], s.sort
24
+ end
25
+
26
+ def test_add
27
+ s = RestrictedSet.new(&@arity_1_proc)
28
+ s.add(-1)
29
+ assert s.empty?
30
+
31
+ s = RestrictedSet.new(@enum, &@arity_2_proc)
32
+ s.add(4)
33
+ assert_equal [-2,3,4], s.sort
34
+
35
+ s = RestrictedSet.new(@enum, &@arity_1_proc)
36
+ assert_equal s, s.add(42)
37
+ assert_equal [1,2,3,42], s.sort
38
+
39
+ s = RestrictedSet.new(@enum, &@arity_2_proc)
40
+ assert_equal s, s.add(42)
41
+ assert_equal [-2,3,42], s.sort
42
+ end
43
+
44
+ def test_shovel
45
+ s = RestrictedSet.new(&@arity_1_proc)
46
+ s << 2 << 1 << -1
47
+ assert_equal [1, 2], s.sort
48
+ end
49
+
50
+ def test_add?
51
+ s = RestrictedSet.new(@enum, &@arity_1_proc)
52
+ assert_equal nil, s.add?(1)
53
+ assert_equal s, s.add?(42)
54
+ assert_equal [1,2,3,42], s.sort
55
+
56
+ s = RestrictedSet.new(@enum, &@arity_2_proc)
57
+ assert_equal nil, s.add?(1)
58
+ assert_equal s, s.add?(42)
59
+ assert_equal [-2,3,42], s.sort
60
+ end
61
+
62
+ def test_replace
63
+ other = [4,6,-4,5,3,-1]
64
+
65
+ s = RestrictedSet.new(@enum, &@arity_1_proc)
66
+ assert_equal [1,2,3], s.sort
67
+ s.replace(other)
68
+ assert_equal [3,4,5,6], s.sort
69
+
70
+ s = RestrictedSet.new(@enum, &@arity_2_proc)
71
+ assert_equal [-2,3], s.sort
72
+ s.replace(other)
73
+ assert_equal [4,6], s.sort
74
+ end
75
+
76
+ def test_merge
77
+ other = [4,6,-4,5,3,-1]
78
+
79
+ s = RestrictedSet.new(@enum, &@arity_1_proc)
80
+ assert_equal [1,2,3], s.sort
81
+ s.merge(other)
82
+ assert_equal [1,2,3,4,5,6], s.sort
83
+
84
+ s = RestrictedSet.new(@enum, &@arity_2_proc)
85
+ assert_equal [-2,3], s.sort
86
+ s.merge(other)
87
+ assert_equal [-2,3,4,6], s.sort
88
+ end
89
+
90
+ def test_restriction_proc
91
+ s = RestrictedSet.new(&@arity_1_proc)
92
+ f = s.restriction_proc
93
+
94
+ assert_instance_of Proc, f
95
+ assert f.call(1)
96
+ refute f.call(0)
97
+ end
98
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: restricted_set
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Danny Whalen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ description: RestricedSet implements a set with restrictions defined by a given block.
42
+ email:
43
+ - daniel.r.whalen@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - .travis.yml
50
+ - BSDL
51
+ - COPYING
52
+ - Gemfile
53
+ - README.md
54
+ - Rakefile
55
+ - lib/restricted_set.rb
56
+ - restricted_set.gemspec
57
+ - test/restricted_set_test.rb
58
+ homepage: https://github.com/invisiblefunnel/restricted_set
59
+ licenses:
60
+ - BSD
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.0.14
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: RestricedSet implements a set with restrictions defined by a given block.
82
+ If the block's arity is 2, it is called with the RestrictedSet itself and an object
83
+ to see if the object is allowed to be put in the set. Otherwise, the block is called
84
+ with an object to see if the object is allowed to be put in the set.
85
+ test_files:
86
+ - test/restricted_set_test.rb
87
+ has_rdoc: