Class: Coopy::Mover
- Inherits:
-
Object
- Object
- Coopy::Mover
- Defined in:
- lib/lib/coopy/mover.rb
Class Method Summary collapse
- .move_units(units) ⇒ Object
- .move_with_extras(isrc, idest) ⇒ Object
- .move_without_extras(src, dest) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Mover
constructor
A new instance of Mover.
Constructor Details
#initialize ⇒ Mover
Returns a new instance of Mover.
7 8 |
# File 'lib/lib/coopy/mover.rb', line 7 def initialize end |
Class Method Details
.move_units(units) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/lib/coopy/mover.rb', line 10 def Mover.move_units(units) isrc = Array.new idest = Array.new len = units.length ltop = -1 rtop = -1 in_src = {} in_dest = {} begin _g = 0 while(_g < len) i = _g _g+=1 unit = units[i] if unit.l >= 0 && unit.r >= 0 ltop = unit.l if ltop < unit.l rtop = unit.r if rtop < unit.r begin in_src[unit.l] = i i end begin in_dest[unit.r] = i i end end end end v = nil begin _g1 = 0 _g2 = ltop + 1 while(_g1 < _g2) i1 = _g1 _g1+=1 v = in_src[i1] isrc.push(v) if v != nil end end begin _g11 = 0 _g3 = rtop + 1 while(_g11 < _g3) i2 = _g11 _g11+=1 v = in_dest[i2] idest.push(v) if v != nil end end return ::Coopy::Mover.move_without_extras(isrc,idest) end |
.move_with_extras(isrc, idest) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/lib/coopy/mover.rb', line 62 def Mover.move_with_extras(isrc,idest) len = isrc.length len2 = idest.length in_src = {} in_dest = {} begin _g = 0 while(_g < len) i = _g _g+=1 begin in_src[isrc[i]] = i i end end end begin _g1 = 0 while(_g1 < len2) i1 = _g1 _g1+=1 begin in_dest[idest[i1]] = i1 i1 end end end src = Array.new dest = Array.new v = nil begin _g2 = 0 while(_g2 < len) i2 = _g2 _g2+=1 v = isrc[i2] src.push(v) if in_dest.include?(v) end end begin _g3 = 0 while(_g3 < len2) i3 = _g3 _g3+=1 v = idest[i3] dest.push(v) if in_src.include?(v) end end return ::Coopy::Mover.move_without_extras(src,dest) end |
.move_without_extras(src, dest) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/lib/coopy/mover.rb', line 113 def Mover.move_without_extras(src,dest) return nil if src.length != dest.length return [] if src.length <= 1 len = src.length in_src = {} blk_len = {} blk_src_loc = {} blk_dest_loc = {} begin _g = 0 while(_g < len) i = _g _g+=1 begin in_src[src[i]] = i i end end end ct = 0 in_cursor = -2 out_cursor = 0 _next = nil blk = -1 v = nil while(out_cursor < len) v = dest[out_cursor] _next = in_src[v] if _next != in_cursor + 1 blk = v ct = 1 blk_src_loc[blk] = _next blk_dest_loc[blk] = out_cursor else ct+=1 end blk_len[blk] = ct in_cursor = _next out_cursor+=1 end blks = Array.new _it = ::Rb::RubyIterator.new(blk_len.keys) while(_it.has_next) do k = _it._next blks.push(k) end blks.sort {|a,b| return blk_len[b] - blk_len[a] } moved = Array.new while(blks.length > 0) blk1 = blks.shift blen = blks.length ref_src_loc = blk_src_loc[blk1] ref_dest_loc = blk_dest_loc[blk1] i1 = blen - 1 while(i1 >= 0) blki = blks[i1] blki_src_loc = blk_src_loc[blki] to_left_src = blki_src_loc < ref_src_loc to_left_dest = blk_dest_loc[blki] < ref_dest_loc if to_left_src != to_left_dest ct1 = blk_len[blki] begin _g1 = 0 while(_g1 < ct1) j = _g1 _g1+=1 moved.push(src[blki_src_loc]) blki_src_loc+=1 end end blks.slice!(i1,1) end i1-=1 end end return moved end |