data_structures_101 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: d0d92b27cd70fa2884b0dd6079a3a88581438604
4
+ data.tar.gz: 174e6747d82bde23b1951693a9d4e66d7e677293
5
+ SHA512:
6
+ metadata.gz: 34d2614a243cd65c97215ed7013b22db6b37b88f75203bbaeaf3ac4401bc109d9a92282f6bae1beda5114586fb6d8ee58cedecd537c9f04342cd1d3b35980c53
7
+ data.tar.gz: bff1fb5af12eb3441061517b34a102213a273beb4ba727d54170413d0359d895ebdc8db17ff950e9326d00bf6f2f9760ec6cba98b2e5e965c039a6ea6ba9f67c
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ # RVM files
15
+ .ruby-gemset
16
+ .ruby-version
17
+
18
+ # Sublime files
19
+ *.sublime-project
20
+ *.sublime-workspace
21
+
22
+ # Gem files
23
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.14.6
6
+ script: bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in data_structures_101.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 aegis
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,46 @@
1
+ # DataStructures101 [![Build Status](https://travis-ci.org/renehernandez/bok-data_structures_101.svg?branch=master)](https://travis-ci.org/renehernandez/bok-data_structures_101)
2
+
3
+ DataStructures101 is a simple gem that groups several implementations of common data structures usually taught in Computer Science courses. The overall goal of the gem is to provide easy to use functionality (trying to match the behavior of existent structures in Ruby) while providing the user with a framework to test and compare their implementations against.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'data_structures_101'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install data_structures_101
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'data_structures_101'
25
+ ```
26
+
27
+ ### LinkedList
28
+
29
+ To create a LinkedList
30
+
31
+ ```ruby
32
+ list = DataStructures101::LinkedList.new
33
+ ```
34
+
35
+ For more information in the `LinkedList` class, check [this post](https://bitsofknowledge.net/data-structures-101-linked-list) and read the documentation.
36
+
37
+ ## Contributing
38
+
39
+ In lieu of a formal styleguide, take care to maintain the existing coding style. Add specs for any new or changed functionality and tests it using RSpec.
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/renehernandez/data_structures_101.
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
46
+
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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "data_structures_101"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'data_structures_101/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "data_structures_101"
8
+ spec.version = DataStructures101::VERSION
9
+ spec.authors = ["aegis"]
10
+ spec.email = ["renehr9102@gmail.com"]
11
+
12
+ spec.summary = %q{DataStructures101 is a simple gem that groups several implementations of common data structures usually taught in Computer Science courses.}
13
+ spec.homepage = "https://github.com/renehernandez/bok-data_structures_101"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.14"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.5"
26
+ end
@@ -0,0 +1,6 @@
1
+ require "data_structures_101/version"
2
+ require "data_structures_101/linked_list"
3
+
4
+ module DataStructures101
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,164 @@
1
+ module DataStructures101
2
+
3
+ class LinkedList
4
+ include Enumerable
5
+
6
+ class Node
7
+ attr_accessor :value, :prev, :next
8
+
9
+ def initialize(value, prev = nil, _next = nil)
10
+ @value = value
11
+ @next = _next
12
+ @prev = prev
13
+ end
14
+
15
+ def to_s
16
+ "<value=#{value}>"
17
+ end
18
+ end
19
+
20
+ attr_reader :size
21
+
22
+ def initialize(*values)
23
+ @size = 0
24
+ @head = Node.new(nil)
25
+ @tail = Node.new(nil, @head)
26
+ @head.next = @tail
27
+
28
+ push(*values)
29
+ end
30
+
31
+ def <<(value)
32
+ push(value)
33
+ end
34
+
35
+ def delete(value)
36
+ curr = @head.next
37
+ return_value = nil
38
+ until curr == @tail
39
+ if curr.value == value
40
+ remove_node(curr)
41
+ return_value = curr.value
42
+ end
43
+ curr = curr.next
44
+ end
45
+
46
+ return_value
47
+ end
48
+
49
+ def each(&block)
50
+ curr = head.next
51
+ until curr.nil?
52
+ block.call(value)
53
+ curr = curr.next
54
+ end
55
+ end
56
+
57
+ def fetch(index)
58
+ fetch_node(index).value
59
+ end
60
+
61
+ def insert(index, *values)
62
+ curr = fetch_node(index)
63
+
64
+ curr = index < 0 ? curr : curr.prev
65
+
66
+ values.each do |value|
67
+ curr = add_node(value, curr.next)
68
+ end
69
+ self
70
+ end
71
+
72
+ def first(*args)
73
+ if args.size == 0
74
+ return @head.next.value
75
+ end
76
+
77
+ if args.size > 1
78
+ raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 1)"
79
+ end
80
+
81
+ if args.first < 0
82
+ raise ArgumentError, "negative array size"
83
+ end
84
+
85
+ return new_list_from_range(0, args.first - 1)
86
+ end
87
+
88
+ def last(*args)
89
+ if args.size == 0
90
+ return @tail.prev.value
91
+ end
92
+
93
+ if args.size > 1
94
+ raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 1)"
95
+ end
96
+
97
+ if args.first < 0
98
+ raise ArgumentError, "negative array size"
99
+ end
100
+
101
+ return new_list_from_range(size - args.first, size - 1)
102
+ end
103
+
104
+ def push(*values)
105
+ values.each do |value|
106
+ add_node(value, @tail)
107
+ end
108
+ self # Allows to concatenate consecutive calls to push
109
+ end
110
+
111
+ private
112
+
113
+ def fetch_node(index)
114
+ index += size if index < 0
115
+ if index < 0 || index >= size
116
+ raise IndexError, "index #{index} outside of array bounds: #{-size}...#{size}"
117
+ end
118
+
119
+ pos = 0
120
+ curr = @head.next
121
+ while pos < index
122
+ curr = curr.next
123
+ pos += 1
124
+ end
125
+
126
+ curr
127
+ end
128
+
129
+ def add_node(value, next_node)
130
+ node = Node.new(value, next_node.prev, next_node)
131
+ node.prev.next = node
132
+ node.next.prev = node
133
+ @size += 1
134
+ node
135
+ end
136
+
137
+ def remove_node(node)
138
+ node.prev.next = node.next
139
+ node.next.prev = node.prev
140
+ @size -= 1
141
+ end
142
+
143
+ def new_list_from_range(start_index, finish_index)
144
+ new_list = LinkedList.new
145
+
146
+ return new_list if size == 0 || start_index == size || finish_index == -1
147
+
148
+ start_index = 0 if start_index < 0
149
+ finish_index = size - 1 if finish_index >= size
150
+
151
+ start_node = fetch_node(start_index)
152
+ finish_node = fetch_node(finish_index)
153
+
154
+ until start_node == finish_node.next
155
+ new_list << start_node.value
156
+ start_node = start_node.next
157
+ end
158
+
159
+ new_list
160
+ end
161
+
162
+ end
163
+
164
+ end
@@ -0,0 +1,3 @@
1
+ module DataStructures101
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data_structures_101
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - aegis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.5'
55
+ description:
56
+ email:
57
+ - renehr9102@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - data_structures_101.gemspec
72
+ - lib/data_structures_101.rb
73
+ - lib/data_structures_101/linked_list.rb
74
+ - lib/data_structures_101/version.rb
75
+ homepage: https://github.com/renehernandez/bok-data_structures_101
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.6.8
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: DataStructures101 is a simple gem that groups several implementations of
99
+ common data structures usually taught in Computer Science courses.
100
+ test_files: []