lindenmayer 0.0.0 → 0.0.1
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.
- checksums.yaml +4 -4
- data/lib/lsystem.rb +68 -3
- metadata +4 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8c063a6bccb1f4bef70f49df3f389c47f7deeff9
|
|
4
|
+
data.tar.gz: 0f8815e779c945e8ce8b6918c58f7fca51e1f9f5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 38416478976bccaa4f6d44d7693902a92b1bcf60d89ebea822bf03795a1aaae2627ff83e19e4a4ef3b4d64f596356dd3aabc63b7ae35a89815489fa3759800bc
|
|
7
|
+
data.tar.gz: b8e1c46c9f278263be3524aa4cedb8ed605160504dff0678230ce0d4c6969cbc1aef0cbf60c18e6035dcacd255836f8c8c3e428d667dbe93e125966931d81390
|
data/lib/lsystem.rb
CHANGED
|
@@ -1,17 +1,82 @@
|
|
|
1
1
|
require 'pry'
|
|
2
|
+
require 'ostruct'
|
|
2
3
|
|
|
3
4
|
module Lindenmayer
|
|
5
|
+
|
|
6
|
+
# Basic Production
|
|
7
|
+
class Production
|
|
8
|
+
attr_reader :transform
|
|
9
|
+
|
|
10
|
+
# transform - (string) Replacement if matching
|
|
11
|
+
def initialize(transform)
|
|
12
|
+
@transform = transform
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Context-Sensitive Production, checks if value matches in context
|
|
17
|
+
# Handles left-only, right-only, and left-right context
|
|
18
|
+
class ContextSensitiveProduction
|
|
19
|
+
attr_reader :key
|
|
20
|
+
|
|
21
|
+
# key - (string) Full key, e.g. "AB<C>DE"
|
|
22
|
+
# transform - (string) Replacement if matching
|
|
23
|
+
def initialize(key, transform)
|
|
24
|
+
@lookahead = key.include?('>') ? key.rpartition('>').last : nil
|
|
25
|
+
@lookbehind = key.include?('<') ? key.partition('<').first : nil
|
|
26
|
+
@key = key.rpartition('<').last.partition('>').first
|
|
27
|
+
@transform = transform
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def transform(idx, context)
|
|
31
|
+
if (@lookbehind.nil? ||
|
|
32
|
+
includes_full_context?(context[0, idx], @lookbehind)) &&
|
|
33
|
+
(@lookahead.nil? ||
|
|
34
|
+
includes_full_context?(context[(idx + 1)..-1], @lookahead))
|
|
35
|
+
@transform
|
|
36
|
+
else
|
|
37
|
+
@key
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def includes_full_context?(context, required_context)
|
|
44
|
+
required_context = required_context.dup.split('')
|
|
45
|
+
|
|
46
|
+
context.split('').each do |char|
|
|
47
|
+
required_context.shift if required_context[0] == char
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
required_context.empty?
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
4
54
|
# Lindenmayer System
|
|
5
55
|
class LSystem
|
|
6
56
|
def initialize(axiom, productions)
|
|
7
57
|
@axiom = axiom
|
|
8
|
-
|
|
58
|
+
|
|
59
|
+
@cs_productions = {}
|
|
60
|
+
cs_keys = productions.keys.select { |k| k.match(/[<>]/) }
|
|
61
|
+
cs_keys.each do |key|
|
|
62
|
+
value = productions.delete(key)
|
|
63
|
+
cs_prod = ContextSensitiveProduction.new(key, value)
|
|
64
|
+
@cs_productions[cs_prod.key] = cs_prod
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
@productions = productions.map { |key, transform| [key, Production.new(transform)] }.to_h
|
|
9
68
|
end
|
|
10
69
|
|
|
11
70
|
def iterate(count = 1)
|
|
12
71
|
count.times do
|
|
13
|
-
@axiom = @axiom.split('').map do |c|
|
|
14
|
-
@productions[c]
|
|
72
|
+
@axiom = @axiom.split('').each_with_index.map do |c, c_idx|
|
|
73
|
+
if @productions[c]
|
|
74
|
+
@productions[c].transform
|
|
75
|
+
elsif @cs_productions[c]
|
|
76
|
+
@cs_productions[c].transform(c_idx, @axiom)
|
|
77
|
+
else
|
|
78
|
+
c
|
|
79
|
+
end
|
|
15
80
|
end.join
|
|
16
81
|
end
|
|
17
82
|
@axiom
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: lindenmayer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Zach Dicklin
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2017-05-
|
|
11
|
+
date: 2017-05-09 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: L-System implementation
|
|
14
14
|
email: zdicklin@gmail.com
|
|
@@ -37,8 +37,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
37
37
|
version: '0'
|
|
38
38
|
requirements: []
|
|
39
39
|
rubyforge_project:
|
|
40
|
-
rubygems_version: 2.
|
|
40
|
+
rubygems_version: 2.2.2
|
|
41
41
|
signing_key:
|
|
42
42
|
specification_version: 4
|
|
43
43
|
summary: Lindenmayer System
|
|
44
44
|
test_files: []
|
|
45
|
+
has_rdoc:
|