Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a route that combines paths that start the same.

  • Loading branch information...
commit 10d3c8addd8c5547df8e1d7a6bde812611015c50 1 parent fc083f0
@pangloss authored
Showing with 94 additions and 0 deletions.
  1. +1 −0  lib/pacer/loader.rb
  2. +93 −0 lib/pacer/transform/combined_path.rb
View
1  lib/pacer/loader.rb
@@ -81,6 +81,7 @@ module SideEffect end
require 'pacer/transform/process'
require 'pacer/transform/join'
require 'pacer/transform/path'
+require 'pacer/transform/combined_path'
require 'pacer/transform/scatter'
require 'pacer/transform/has_count_cap'
require 'pacer/transform/sort_section'
View
93 lib/pacer/transform/combined_path.rb
@@ -0,0 +1,93 @@
+module Pacer
+ module Core
+ module Graph
+ module PathRoute
+ def combine(*exts)
+ chain_route transform: :combine_path
+ end
+ end
+ end
+ end
+
+ module Transform
+ module CombinePath
+ protected
+
+ def attach_pipe(end_pipe)
+ pipe = CombinePathPipe.new
+ pipe.setStarts end_pipe
+ pipe
+ end
+
+
+ class CombinePathPipe < RubyPipe
+ def initialize
+ super
+ self.building_path = nil
+ self.prev_path = nil
+ end
+
+ # NOTE: doesn't handle variable length paths yet...
+ def processNextStart()
+ while true
+ path = starts.next
+ if building_path
+ if path.first == building_path.first.first
+ add_path path
+ else
+ return next_path(path)
+ end
+ else
+ next_path(path)
+ end
+ end
+ rescue Pacer::EmptyPipe, java.util.NoSuchElementException
+ if building_path
+ building_path
+ else
+ raise EmptyPipe.instance
+ end
+ end
+
+ private
+
+ attr_accessor :building_path, :prev_path
+
+ def make(path)
+ path.reverse.inject([]) { |inner, e| [e, inner] }
+ end
+
+ def add_path(path)
+ working = building_path
+ path.length.times do |pos|
+ current = path[pos]
+ prev = prev_path[pos]
+ if current == prev
+ working = working.last
+ else
+ working << make(path[pos..-1])
+ break
+ end
+ end
+ self.prev_path = path
+ end
+
+ def next_path(path)
+ finished = building_path
+ self.building_path = make path
+ self.prev_path = path
+ finished
+ end
+ end
+ end
+ end
+end
+
+
+ [a [b [c]
+ [d]]
+ [e [f
+ g]]]
+
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.