spectrate 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
+ SHA1:
3
+ metadata.gz: 4954896d21a9384af77fe28691ac879d19bd2801
4
+ data.tar.gz: bb77764b5a0309baa614c4acdb23161d55657b66
5
+ SHA512:
6
+ metadata.gz: 30e9c34fad186a6e76fac24ebed11b66c19314659962123cee0a8b01e8e77c81173625e7a57e4e9bff62d2fc30ce31c8b90f46954c118ce1cabc261dd971f9ff
7
+ data.tar.gz: f504358194c2b228d79dfd9f45710237519c46675dbba7d8ef06ddd5450c7f5dee6009c2afd2917dd52400961020ca0a509c4b99da8a4141e9e9e7ec4decf68c
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /ext/spectrate/Makefile
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers 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. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 i2bskn
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,60 @@
1
+ # Spectrate
2
+
3
+ Object with continuous range in Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'spectrate'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install spectrate
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ objects = [*1..10].map {|i| Struct.new(:name, :range).new("obj#{i}", rand(10) + 1) }
25
+ # => [#<struct name="obj1", range=9>,
26
+ # #<struct name="obj2", range=9>,
27
+ # #<struct name="obj3", range=2>,
28
+ # #<struct name="obj4", range=8>,
29
+ # #<struct name="obj5", range=6>,
30
+ # #<struct name="obj6", range=1>,
31
+ # #<struct name="obj7", range=4>,
32
+ # #<struct name="obj8", range=9>,
33
+ # #<struct name="obj9", range=5>,
34
+ # #<struct name="obj10", range=6>]
35
+
36
+ spectrate = Spectrate.new(objects, objects.map(&:range))
37
+ spectrate[5..15] # Object of the corresponding range.
38
+ # => [#<struct name="obj1", range=9>, #<struct name="obj2", range=9>]
39
+
40
+ spectrate.sample # Sample to match the range of size.
41
+ [*1..(objects.map(&:range).inject(&:+) * 10)]
42
+ .map { spectrate.sample }
43
+ .group_by(&:name)
44
+ .each_with_object({}) {|(k,v), obj| obj[k] = v.size }
45
+ # => {"obj8"=>93, "obj3"=>24, "obj1"=>90, "obj4"=>77, "obj9"=>50, "obj7"=>38, "obj2"=>92, "obj5"=>64, "obj10"=>53, "obj6"=>9}
46
+ ```
47
+
48
+ ## Development
49
+
50
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/i2bskn/spectrate. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
55
+
56
+
57
+ ## License
58
+
59
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
60
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "spectrate"
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
+ require "pry"
10
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ require "mkmf"
2
+
3
+ create_makefile("spectrate/spectrate")
@@ -0,0 +1,154 @@
1
+ #include "spectrate.h"
2
+
3
+ VALUE rb_cSpectrate;
4
+
5
+ struct spectrate_object {
6
+ int range_size;
7
+ int range_max;
8
+ int *borders;
9
+ VALUE objects;
10
+ VALUE ranges;
11
+ };
12
+
13
+ static void
14
+ spectrate_mark(void *ptr) {
15
+ struct spectrate_object *spectrum = ptr;
16
+ rb_gc_mark(spectrum->objects);
17
+ rb_gc_mark(spectrum->ranges);
18
+ }
19
+
20
+ static void
21
+ spectrate_free(void *ptr) {
22
+ struct spectrate_object *spectrum = ptr;
23
+ if (spectrum->borders != NULL) {
24
+ free(spectrum->borders);
25
+ }
26
+ }
27
+
28
+ static size_t
29
+ spectrate_memsize(const void *spectrum) {
30
+ return sizeof(struct spectrate_object);
31
+ }
32
+
33
+ static const rb_data_type_t spectrate_type = {
34
+ "spectrate",
35
+ {
36
+ spectrate_mark,
37
+ spectrate_free,
38
+ spectrate_memsize,
39
+ },
40
+ 0,
41
+ 0,
42
+ RUBY_TYPED_FREE_IMMEDIATELY
43
+ };
44
+
45
+ static VALUE
46
+ spectrate_alloc(VALUE klass) {
47
+ struct spectrate_object *spectrum;
48
+ VALUE obj = TypedData_Make_Struct(klass, struct spectrate_object, &spectrate_type, spectrum);
49
+ return obj;
50
+ }
51
+
52
+ static VALUE
53
+ spectrate_init(VALUE obj, VALUE objects, VALUE ranges) {
54
+ struct spectrate_object *spectrum;
55
+ TypedData_Get_Struct(obj, struct spectrate_object, &spectrate_type, spectrum);
56
+
57
+ int i, object_size;
58
+
59
+ Check_Type(objects, T_ARRAY);
60
+ Check_Type(ranges, T_ARRAY);
61
+ spectrum->range_size = RARRAY_LENINT(ranges);
62
+ object_size = RARRAY_LENINT(objects);
63
+ if (object_size < spectrum->range_size) {
64
+ rb_raise(rb_eArgError, "The size of the objects must be greater than or equal ranges");
65
+ } else if (object_size == 0 || spectrum->range_size == 0) {
66
+ rb_raise(rb_eArgError, "Objects and ranges must be non empty");
67
+ }
68
+
69
+ spectrum->range_max = 0;
70
+ spectrum->borders = (int *)malloc(sizeof(int) * spectrum->range_size);
71
+
72
+ for (i = 0; i < spectrum->range_size; i++) {
73
+ spectrum->range_max += FIX2INT(RARRAY_AREF(ranges, i));
74
+ spectrum->borders[i] = spectrum->range_max;
75
+ }
76
+
77
+ spectrum->objects = objects;
78
+ spectrum->ranges = ranges;
79
+ return obj;
80
+ }
81
+
82
+ static VALUE
83
+ spectrate_sample(VALUE obj) {
84
+ struct spectrate_object *spectrum;
85
+ TypedData_Get_Struct(obj, struct spectrate_object, &spectrate_type, spectrum);
86
+
87
+ int i, tmin;
88
+
89
+ tmin = rand() % spectrum->range_max;
90
+ for (i = 0; i < spectrum->range_size; i++) {
91
+ if (tmin < spectrum->borders[i]) {
92
+ break;
93
+ }
94
+ }
95
+ return RARRAY_AREF(spectrum->objects, i);
96
+ }
97
+
98
+ static VALUE
99
+ spectrate_select(VALUE obj, VALUE index) {
100
+ struct spectrate_object *spectrum;
101
+ TypedData_Get_Struct(obj, struct spectrate_object, &spectrate_type, spectrum);
102
+
103
+ int i, tmin, tmax, rmin;
104
+ VALUE result = Qnil;
105
+
106
+ switch (TYPE(index)) {
107
+ case T_FIXNUM:
108
+ tmin = FIX2INT(index);
109
+
110
+ for (i = 0; i < spectrum->range_size; i++) {
111
+ if (tmin < spectrum->borders[i]) {
112
+ result = RARRAY_AREF(spectrum->objects, i);
113
+ break;
114
+ }
115
+ }
116
+ break;
117
+ default:
118
+ if (rb_funcall(index, rb_intern("is_a?"), 1, rb_cRange) == Qtrue) {
119
+ result = rb_ary_new();
120
+ tmin = FIX2INT(rb_funcall(index, rb_intern("min"), 0));
121
+ tmax = FIX2INT(rb_funcall(index, rb_intern("max"), 0));
122
+
123
+ for (i = 0; i < spectrum->range_size; i++) {
124
+ if (i == 0) {
125
+ rmin = 0;
126
+ } else {
127
+ rmin = spectrum->borders[i - 1];
128
+ }
129
+
130
+ if (tmin < spectrum->borders[i] && rmin <= tmax) {
131
+ rb_ary_push(result, RARRAY_AREF(spectrum->objects, i));
132
+ }
133
+
134
+ if (rmin >= tmax) {
135
+ break;
136
+ }
137
+ }
138
+ } else {
139
+ rb_raise(rb_eArgError, "Invalid index type");
140
+ }
141
+ break;
142
+ }
143
+
144
+ return result;
145
+ }
146
+
147
+ void
148
+ Init_spectrate(void) {
149
+ rb_cSpectrate = rb_define_class("Spectrate", rb_cObject);
150
+ rb_define_alloc_func(rb_cSpectrate, spectrate_alloc);
151
+ rb_define_method(rb_cSpectrate, "initialize", spectrate_init, 2);
152
+ rb_define_method(rb_cSpectrate, "sample", spectrate_sample, 0);
153
+ rb_define_method(rb_cSpectrate, "[]", spectrate_select, 1);
154
+ }
@@ -0,0 +1,7 @@
1
+ #ifndef SPECTRATE_H
2
+ #define SPECTRATE_H
3
+
4
+ #include "ruby.h"
5
+ #include <stdlib.h>
6
+
7
+ #endif
data/lib/spectrate.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "spectrate/version"
2
+ require "spectrate/spectrate"
@@ -0,0 +1,3 @@
1
+ class Spectrate
2
+ VERSION = "0.1.0"
3
+ end
data/spectrate.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'spectrate/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "spectrate"
8
+ spec.version = Spectrate::VERSION
9
+ spec.authors = ["i2bskn"]
10
+ spec.email = ["i2bskn@gmail.com"]
11
+
12
+ spec.summary = %q{Object with continuous range in Ruby.}
13
+ spec.description = %q{Object with continuous range in Ruby.}
14
+ spec.homepage = "https://github.com/i2bskn/spectrate"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+ spec.extensions = ["ext/spectrate/extconf.rb"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "benchmark-ips"
27
+ spec.add_development_dependency "pry"
28
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spectrate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - i2bskn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-04 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: benchmark-ips
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Object with continuous range in Ruby.
84
+ email:
85
+ - i2bskn@gmail.com
86
+ executables: []
87
+ extensions:
88
+ - ext/spectrate/extconf.rb
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - ext/spectrate/extconf.rb
102
+ - ext/spectrate/spectrate.c
103
+ - ext/spectrate/spectrate.h
104
+ - lib/spectrate.rb
105
+ - lib/spectrate/version.rb
106
+ - spectrate.gemspec
107
+ homepage: https://github.com/i2bskn/spectrate
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.4.5
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Object with continuous range in Ruby.
131
+ test_files: []