doubly_linkedlist 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b3cc844b329f52174ce3389f0c1fda4c8f91f96efd411c610154bcd0e044c9e2
4
+ data.tar.gz: aebc095efbd852f1cd4afe54edb332c92175eedc25044c79f3ecaa54aef8c13a
5
+ SHA512:
6
+ metadata.gz: ece5e87b139f17bcbc881641134ed81c438aad6ffde6de6331fd15c8593d2423d5fb7156f73bd3fb43d2ba320a395ab205c41179bb88ee3068a4f65115f9986e
7
+ data.tar.gz: b9986da96197040ce1b028524c42ce5485b5b9f7c5696d489c6d4e4ca322d9eed9156ecb3111fe5c8006c84b1b15beae2985011a362fe22e1aa467899e3840fc
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at TODO: Write your email address. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in doubly_linkedlist.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ doubly_linkedlist (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (12.3.3)
11
+ rspec (3.9.0)
12
+ rspec-core (~> 3.9.0)
13
+ rspec-expectations (~> 3.9.0)
14
+ rspec-mocks (~> 3.9.0)
15
+ rspec-core (3.9.1)
16
+ rspec-support (~> 3.9.1)
17
+ rspec-expectations (3.9.1)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.9.0)
20
+ rspec-mocks (3.9.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.9.0)
23
+ rspec-support (3.9.2)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ doubly_linkedlist!
30
+ rake (~> 12.0)
31
+ rspec (~> 3.0)
32
+
33
+ BUNDLED WITH
34
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 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.
@@ -0,0 +1,65 @@
1
+ # DoublyLinkedlist
2
+
3
+ A Ruby implementation of the famous Doubly Linkedlist data structure following ruby idioms. A Linkedlist is efficient in many specific cases than other data structures such as Array.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'doubly_linkedlist'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install doubly_linkedlist
20
+
21
+ ## Usage
22
+
23
+ #### Initialization
24
+
25
+ There are two different ways to initialize a linkedlist including one which converting an array into a list by calling `to_list` method on the array object.
26
+
27
+ You can find the examples below.
28
+
29
+ ```ruby
30
+ # You can pass an array as argument which
31
+ # will be the created list's node values.
32
+ list = DoublyLinkedlist.new([obj1, obj2, ...])
33
+
34
+ # Argument is optional. If not given,
35
+ # a empty list is created.
36
+ list1 = DoublyLinkedlist.new
37
+ ```
38
+ #### Methods
39
+
40
+ ```ruby
41
+ list.count # => returns the number of nodes in the list
42
+
43
+ list.head # => returns the node value at the list's head. (Alias: first)
44
+
45
+ list.tail # => returns the node value at the list's tail. (Alias: last)
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.
51
+
52
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/anshabmk/doubly_linkedlist. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/anshabmk/doubly_linkedlist/blob/master/CODE_OF_CONDUCT.md).
57
+
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
62
+
63
+ ## Code of Conduct
64
+
65
+ Everyone interacting in the DoublyLinkedlist project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/anshabmk/doubly_linkedlist/blob/master/CODE_OF_CONDUCT.md).
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "doubly_linkedlist"
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__)
@@ -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,28 @@
1
+ require_relative 'lib/doubly_linkedlist/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "doubly_linkedlist"
5
+ spec.version = DoublyLinkedlist::VERSION
6
+ spec.authors = ["Anshab M K"]
7
+ spec.email = ["anshabmk@live.com"]
8
+
9
+ spec.summary = %q{Ruby implementation of a Doubly Linkedlist data structure.}
10
+ spec.description = %q{Doubly Linkedlist with faster and minimal interface.}
11
+ spec.homepage = "https://github.com/anshabmk/doubly_linkedlist"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/anshabmk/doubly_linkedlist"
17
+ spec.metadata["changelog_uri"] = "https://github.com/anshabmk/doubly_linkedlist"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+ spec.add_development_dependency "rspec", "~> 3.2"
28
+ end
@@ -0,0 +1,16 @@
1
+ require "doubly_linkedlist/version"
2
+
3
+ module DoublyLinkedlist
4
+ class Error < StandardError; end
5
+ # Your code goes here...
6
+ end
7
+
8
+ require 'doubly_linkedlist/list'
9
+
10
+ # Monkey patching a method to_list into the Array class.
11
+ # Usage: ary.to_list -> list
12
+ class Array
13
+ def to_list
14
+ DoublyLinkedlist::List.new(self)
15
+ end
16
+ end
@@ -0,0 +1,148 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'doubly_linkedlist/node'
4
+
5
+ module DoublyLinkedlist
6
+ class List
7
+ attr_reader :count
8
+
9
+ # Initialize a DoublyLinkedlist::List object with the values passed in
10
+ # the optional argument array.
11
+ def initialize(values = [])
12
+ @count = 0
13
+ build_list_from_array(values)
14
+ end
15
+
16
+ # Returns the value at the head of the list if present, nil otherwise.
17
+ def head
18
+ @head.value if @head
19
+ end
20
+
21
+ alias :first :head
22
+
23
+ # Returns the value at the tail of the list if present, nil otherwise.
24
+ def tail
25
+ @tail.value if @tail
26
+ end
27
+
28
+ alias :last :tail
29
+
30
+ # Returns the node object at the given index if present, nil otherwise.
31
+ # Signature: Integer -> Node
32
+ def find_at(index)
33
+ return if (index + 1) > count
34
+
35
+ item = @head
36
+ index.times { item = item.next }
37
+ item.value
38
+ end
39
+
40
+ # Returns the leftmost index of the value present in the list.
41
+ # Returns nil if the value is not present.
42
+ # Signature: Type(node_value) -> Integer
43
+ def index(value)
44
+ find_index(0, @head, :next, 1)
45
+ end
46
+
47
+ # Returns the rightmost index of value present in the list.
48
+ # Returns nil if the value is not present.
49
+ # Signature: Type(node_value) -> Integer
50
+ def rindex(value)
51
+ find_index(count - 1, @tail, :prev, -1)
52
+ end
53
+
54
+ # Inserts a node with the given value into the head of the list,
55
+ # increments and returns the count of nodes.
56
+ def insert(value)
57
+ new_node = Node.new(value)
58
+ @head.prev = new_node if @head
59
+ new_node.next = @head
60
+ @tail = new_node unless @tail
61
+ @head = new_node
62
+ @count += 1
63
+ end
64
+
65
+ alias :push :insert
66
+
67
+ # Inserts a node with the given value into the tail of the list,
68
+ # increments and returns the count of nodes.
69
+ def enqueue(value)
70
+ new_node = Node.new(value)
71
+ @head = new_node unless @head
72
+ @tail.next = new_node if @tail
73
+ new_node.prev = @tail
74
+ @tail = new_node
75
+ @count += 1
76
+ end
77
+
78
+ # Deletes the node at a given index and returns the value present in the
79
+ # deleted node.
80
+ # Returns nil if the given index is out of range.
81
+ # Signature: int -> Type(deleted_node.value)
82
+ def delete_at(index)
83
+ return if (index + 1) > count
84
+
85
+ if index.zero?
86
+ deleted = @head
87
+ @head = @head.next
88
+ else
89
+ prev_item = @head
90
+
91
+ (index - 1).times { prev_item = prev_item.next }
92
+
93
+ deleted = prev_item.next
94
+ prev_item.next = deleted.next
95
+ @tail = prev_item if prev_item.next.nil?
96
+ end
97
+
98
+ @count -= 1
99
+ deleted.value
100
+ end
101
+
102
+ # Pops out and returns the node_value at head.
103
+ def pop
104
+ delete_at(0)
105
+ end
106
+
107
+ alias :deque :pop
108
+
109
+ # Converts the list object into an array object with all the node values.
110
+ def to_a
111
+ item = @head
112
+ arr = []
113
+
114
+ while item do
115
+ arr.push(item.value)
116
+ item = item.next
117
+ end
118
+
119
+ arr
120
+ end
121
+
122
+ # Converts the array representation of list into a string.
123
+ def to_s
124
+ to_a.to_s
125
+ end
126
+
127
+ alias :inspect :to_s
128
+
129
+ private
130
+
131
+ # Enqueue nodes one by one with the values given as an array into the list.
132
+ def build_list_from_array(values)
133
+ values.each { |v| enqueue(v) }
134
+ end
135
+
136
+ def find_index(start_index, start_item, item_iterator_name, step)
137
+ i = start_index
138
+ item = start_item
139
+
140
+ while item do
141
+ return i if item.value == value
142
+
143
+ item = item.send(item_iterator_name)
144
+ i += step
145
+ end
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DoublyLinkedlist
4
+ class Node
5
+ attr_reader :value
6
+ attr_accessor :next, :prev
7
+
8
+ def initialize(value)
9
+ @value = value
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module DoublyLinkedlist
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: doubly_linkedlist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anshab M K
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-05-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ description: Doubly Linkedlist with faster and minimal interface.
28
+ email:
29
+ - anshabmk@live.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".travis.yml"
37
+ - CODE_OF_CONDUCT.md
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - bin/console
44
+ - bin/setup
45
+ - doubly_linkedlist.gemspec
46
+ - lib/doubly_linkedlist.rb
47
+ - lib/doubly_linkedlist/list.rb
48
+ - lib/doubly_linkedlist/node.rb
49
+ - lib/doubly_linkedlist/version.rb
50
+ homepage: https://github.com/anshabmk/doubly_linkedlist
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ homepage_uri: https://github.com/anshabmk/doubly_linkedlist
55
+ source_code_uri: https://github.com/anshabmk/doubly_linkedlist
56
+ changelog_uri: https://github.com/anshabmk/doubly_linkedlist
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.3.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.1.2
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Ruby implementation of a Doubly Linkedlist data structure.
76
+ test_files: []