Skip to content
This repository was archived by the owner on Jan 5, 2021. It is now read-only.

Commit 0f52aff

Browse files
author
Loic Teixeira
committed
Add Assignment 06 Sources
1 parent 0a6fea4 commit 0f52aff

File tree

12 files changed

+1327
-0
lines changed

12 files changed

+1327
-0
lines changed

06_matrix_pagerank/lib/base.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class BasicObject
2+
NIX = ::Object.new
3+
4+
def nix
5+
return NIX
6+
end
7+
8+
def nix?
9+
return equal?(NIX)
10+
end
11+
12+
def checkcast(s)
13+
return self
14+
end
15+
end
16+
17+
class Class
18+
alias_method :__new__, :new
19+
20+
def nil
21+
return nil
22+
end
23+
end
24+
25+
class << self
26+
def each_manifest_name(dir)
27+
path = File.join(dir, "manifest.txt")
28+
lines = File.readlines(path)
29+
lines.each do |line|
30+
name = line.strip
31+
unless name.empty?
32+
yield name
33+
end
34+
end
35+
return
36+
end
37+
38+
def load_manifest(dir)
39+
each_manifest_name(dir) do |name|
40+
path = File.join(dir, "ruby", name)
41+
if File.exist?(path)
42+
load(path)
43+
end
44+
end
45+
return
46+
end
47+
48+
def load_parent_manifest(dir)
49+
load_manifest(File.dirname(dir))
50+
return
51+
end
52+
end

06_matrix_pagerank/lib/integer.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Bignum
2+
def size
3+
return super
4+
end
5+
end
6+
7+
class Fixnum
8+
def size
9+
return super
10+
end
11+
end
12+
13+
class Integer
14+
def hash
15+
return self
16+
end
17+
18+
def size
19+
raise NoMethodError.new("undefined method `size' for #{self.inspect}:Integer")
20+
end
21+
end

06_matrix_pagerank/lib/memory.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
dir = File.dirname(File.expand_path(__FILE__))
2+
3+
require(File.join(dir, "base.rb"))
4+
5+
class Memory
6+
class Fault < Exception
7+
end
8+
9+
class << self
10+
def new(c)
11+
if c < 0
12+
raise Fault.new("negative capacity: #{c}")
13+
end
14+
values = Array.new(c)
15+
if block_given?
16+
c.times do |i|
17+
values[i] = yield(i)
18+
end
19+
end
20+
return Memory.__new__(values)
21+
end
22+
23+
def [](*values)
24+
return Memory.__new__(values)
25+
end
26+
end
27+
28+
def initialize(values)
29+
super()
30+
@values = values
31+
return
32+
end
33+
34+
def __assert_bounds(i)
35+
if i < 0 || i >= capacity
36+
raise Fault.new("index out of bounds for capacity #{capacity}: #{i}")
37+
end
38+
return
39+
end
40+
41+
def __get(i)
42+
return @values[i]
43+
end
44+
45+
def eql?(o)
46+
if o.is_a?(Memory)
47+
if capacity == o.capacity
48+
capacity.times do |i|
49+
unless @values[i].eql?(o.__get(i))
50+
return false
51+
end
52+
end
53+
return true
54+
end
55+
end
56+
return false
57+
end
58+
59+
def capacity
60+
return @values.size
61+
end
62+
63+
def [](i)
64+
__assert_bounds(i)
65+
return @values[i]
66+
end
67+
68+
def []=(i, e)
69+
__assert_bounds(i)
70+
@values[i] = e
71+
return
72+
end
73+
end

0 commit comments

Comments
 (0)