Skip to content

Commit

Permalink
remove compsci and NEWLINE dependency
Browse files Browse the repository at this point in the history
- remove CompSci.numeric! and friends
  • Loading branch information
rickhull committed Jun 21, 2024
1 parent 9fa30fe commit 1baf003
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 42 deletions.
26 changes: 1 addition & 25 deletions lib/compsci.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
module CompSci
NEWLINE = $/ # platform-default line separator

# thanks apeiros
# https://gist.github.com/rickhull/d0b579aa08c85430b0dc82a791ff12d6
def self.power_of?(num, base)
return false if base <= 1
mod = 0
num, mod = num.divmod(base) until num == 1 || mod > 0
num, mod = num.divmod(base) until (num == 1) || (mod > 0)
mod == 0
end

def self.numeric!(num)
raise(ArgumentError, num.inspect) unless num.is_a?(Numeric)
num
end

def self.positive!(num)
numeric!(num)
raise(ArgumentError, num.inspect) unless num >= 0
num
end

def self.string!(str)
raise(ArgumentError, str.inspect) unless str.is_a?(String)
str
end

def self.nonempty!(str)
string!(str)
raise(ArgumentError, str.inspect) if str.empty?
str
end
end
2 changes: 1 addition & 1 deletion lib/compsci/fsm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def follow(src, value)

# initial, final, graph
def to_s
[[@initial, @final].inspect, @graph].join(NEWLINE)
[[@initial, @final].inspect, @graph].join($/)
end

def eval(state)
Expand Down
4 changes: 1 addition & 3 deletions lib/compsci/node.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'compsci'

module CompSci
# has a value and an array of children; allows child gaps
class Node
Expand Down Expand Up @@ -51,7 +49,7 @@ def display(width: 80)
memo + Array.new(@children.size) { |i| n and n.children[i] }
}
end
lines.join(NEWLINE)
lines
end
end

Expand Down
4 changes: 1 addition & 3 deletions lib/compsci/simplex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# which was forked in 2017 from https://github.com/danlucraft/simplex
# which had its last commit in 2013

require 'compsci'

module CompSci
class Simplex
DEFAULT_MAX_PIVOTS = 10_000
Expand Down Expand Up @@ -168,7 +166,7 @@ def formatted_tableau
lines = result.map { |ary| ary.join(" ") }
max_line_length = lines.map(&:length).max
lines.insert(1, "-"*max_line_length)
lines.join(NEWLINE)
lines
end
end
end
6 changes: 0 additions & 6 deletions sig/compsci.rbs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
module CompSci
NEWLINE: String

def self.power_of?: (Numeric, Numeric) -> bool
def self.numeric!: (untyped) -> Numeric
def self.positive!: (untyped) -> Numeric
def self.string!: (untyped) -> String
def self.nonempty!: (untyped) -> String
end
4 changes: 1 addition & 3 deletions test/flex_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@
item_count = 31
# tree already has a root node
(item_count - 1).times { @root.push(rand(99), @child_slots) }
str = @root.display
line_count = str.split(NEWLINE).size
expect(line_count).must_equal Math.log(item_count + 1, 2).ceil
expect(@root.display.size).must_equal Math.log(item_count + 1, 2).ceil
end

describe "searching" do
Expand Down
2 changes: 1 addition & 1 deletion test/simplex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def test_manual_iteration
[0, 0, 1]
)
while simplex.can_improve?
assert simplex.formatted_tableau.is_a?(String)
assert simplex.formatted_tableau.is_a?(Array)
simplex.pivot
end
result = simplex.solution
Expand Down

0 comments on commit 1baf003

Please sign in to comment.