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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/lsystem.rb +68 -3
  3. metadata +4 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e76700ad3abdfd7ffedb930f0a7b29439afed77f
4
- data.tar.gz: 582de0cc9201696a9500ce2b9ebaad5c3a1d1fe3
3
+ metadata.gz: 8c063a6bccb1f4bef70f49df3f389c47f7deeff9
4
+ data.tar.gz: 0f8815e779c945e8ce8b6918c58f7fca51e1f9f5
5
5
  SHA512:
6
- metadata.gz: b8ee05e65bdb4d118843300e9d8eb2e277799a452ce595763327ff0a6a85788b2db3281f39fd24a1fe0a22ea122c49b7952381e0b1c9b47c5c50cc93d480fcb1
7
- data.tar.gz: b83065f74aaf153363e0b92e3d0e1f26f36471f197b4c51a2082686e5157e42d72fceb672e6d14ebeb750d45a59aac501c84f63f39e10691e46a075883ebb613
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
- @productions = productions
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] || 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.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-08 00:00:00.000000000 Z
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.6.12
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: