Last active
October 6, 2016 13:51
-
-
Save neurodynamic/93e19c53ace1e679f6aa9693b75068b2 to your computer and use it in GitHub Desktop.
Artificially slow Ruby array implementation for data structures learning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SlowArray | |
def initialize(items) | |
operation! | |
@items = {} | |
items.each_with_index do |item, index| | |
@items[index] = item | |
end | |
end | |
def get(index) | |
operation! | |
items[index] | |
end | |
def insert(value, index) | |
operation! | |
shift_right_from(index) | |
items[index] = value | |
end | |
def remove(index) | |
operation! | |
shift_left_from(index + 1) | |
end | |
def length | |
operation! | |
items.length | |
end | |
private | |
attr_accessor :items | |
def shift_left_from(index) | |
current = index | |
while current < length | |
items[current - 1] = items[current] | |
current += 1 | |
end | |
items.delete(length - 1) | |
end | |
def shift_right_from(index) | |
current = length - 1 | |
while current >= index | |
items[current + 1] = items[current] | |
current -= 1 | |
end | |
items.delete(index) | |
end | |
def shift_item_left(index) | |
operation! | |
item = items[index] | |
items[index] = nil | |
items[index - 1] = item | |
end | |
def shift_item_right(index) | |
operation! | |
item = items[index] | |
items[index] = nil | |
items[index + 1] = item | |
end | |
def operation! | |
puts "Operation!" | |
sleep 0.1 | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
require_relative 'slow_array' | |
describe SlowArray do | |
let(:slow_array) { SlowArray.new([:a, :b, :c, :d]) } | |
context "#length" do | |
it "returns the length of the array" do | |
expect(slow_array.length).to eq(4) | |
end | |
end | |
context "#get" do | |
it "returns the value at index" do | |
expect(slow_array.get(0)).to eq(:a) | |
end | |
end | |
context "#insert" do | |
before do | |
slow_array.insert(:new_item, 2) | |
end | |
it "inserts item at index" do | |
expect(slow_array.get(2)).to eq(:new_item) | |
expect(slow_array.length).to eq(5) | |
end | |
it "shifts following items right" do | |
expect(slow_array.get(3)).to eq(:c) | |
expect(slow_array.get(4)).to eq(:d) | |
expect(slow_array.get(5)).to eq(nil) | |
end | |
end | |
context "#remove" do | |
before do | |
slow_array.remove(1) | |
end | |
it "inserts item at index" do | |
expect(slow_array.length).to eq(3) | |
expect(slow_array.get(1)).to eq(:c) | |
end | |
it "shifts following items left" do | |
expect(slow_array.get(2)).to eq(:d) | |
expect(slow_array.get(3)).to eq(nil) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment