Commit 79cd1ca3043822d5f763b8a1900fd2bea52b0ff0
1 parent
ccc712b1
Exists in
master
and in
4 other branches
Refactor: change the map hash from a local variable to private variable.
Showing
1 changed file
with
31 additions
and
34 deletions
Show diff stats
app/models/network/graph.rb
... | ... | @@ -44,33 +44,31 @@ module Network |
44 | 44 | # list of commits. As well as returns date list |
45 | 45 | # corelated with time set on commits. |
46 | 46 | # |
47 | - # @param [Array<Graph::Commit>] commits to index | |
48 | - # | |
49 | 47 | # @return [Array<TimeDate>] list of commit dates corelated with time on commits |
50 | 48 | def index_commits |
51 | 49 | days = [] |
52 | - map = {} | |
50 | + @map = {} | |
53 | 51 | |
54 | 52 | @commits.reverse.each_with_index do |c,i| |
55 | 53 | c.time = i |
56 | 54 | days[i] = c.committed_date |
57 | - map[c.id] = c | |
55 | + @map[c.id] = c | |
58 | 56 | end |
59 | 57 | |
60 | - @_reserved = {} | |
58 | + @reserved = {} | |
61 | 59 | days.each_index do |i| |
62 | - @_reserved[i] = [] | |
60 | + @reserved[i] = [] | |
63 | 61 | end |
64 | 62 | |
65 | 63 | commits_sort_by_ref.each do |commit| |
66 | - if map.include? commit.id then | |
67 | - place_chain(map[commit.id], map) | |
64 | + if @map.include? commit.id then | |
65 | + place_chain(commit) | |
68 | 66 | end |
69 | 67 | end |
70 | 68 | |
71 | 69 | # find parent spaces for not overlap lines |
72 | 70 | @commits.each do |c| |
73 | - c.parent_spaces.concat(find_free_parent_spaces(c, map)) | |
71 | + c.parent_spaces.concat(find_free_parent_spaces(c)) | |
74 | 72 | end |
75 | 73 | |
76 | 74 | days |
... | ... | @@ -114,12 +112,12 @@ module Network |
114 | 112 | heads.include?(@ref) |
115 | 113 | end |
116 | 114 | |
117 | - def find_free_parent_spaces(commit, map) | |
115 | + def find_free_parent_spaces(commit) | |
118 | 116 | spaces = [] |
119 | 117 | |
120 | 118 | commit.parents.each do |p| |
121 | - if map.include?(p.id) then | |
122 | - parent = map[p.id] | |
119 | + if @map.include?(p.id) then | |
120 | + parent = @map[p.id] | |
123 | 121 | |
124 | 122 | range = if commit.time < parent.time then |
125 | 123 | commit.time..parent.time |
... | ... | @@ -164,23 +162,22 @@ module Network |
164 | 162 | |
165 | 163 | # Add space mark on commit and its parents |
166 | 164 | # |
167 | - # @param [Graph::Commit] the commit object. | |
168 | - # @param [Hash<String,Graph::Commit>] map of commits | |
169 | - def place_chain(commit, map, parent_time = nil) | |
170 | - leaves = take_left_leaves(commit, map) | |
165 | + # @param [::Commit] the commit object. | |
166 | + def place_chain(commit, parent_time = nil) | |
167 | + leaves = take_left_leaves(commit) | |
171 | 168 | if leaves.empty? |
172 | 169 | return |
173 | 170 | end |
174 | 171 | |
175 | 172 | time_range = leaves.last.time..leaves.first.time |
176 | - space_base = get_space_base(leaves, map) | |
173 | + space_base = get_space_base(leaves) | |
177 | 174 | space = find_free_space(time_range, 2, space_base) |
178 | 175 | leaves.each do |l| |
179 | 176 | l.spaces << space |
180 | 177 | # Also add space to parent |
181 | 178 | l.parents.each do |p| |
182 | - if map.include?(p.id) | |
183 | - parent = map[p.id] | |
179 | + if @map.include?(p.id) | |
180 | + parent = @map[p.id] | |
184 | 181 | if parent.space > 0 |
185 | 182 | parent.spaces << space |
186 | 183 | end |
... | ... | @@ -192,8 +189,8 @@ module Network |
192 | 189 | min_time = leaves.last.time |
193 | 190 | parents = leaves.last.parents.collect |
194 | 191 | parents.each do |p| |
195 | - if map.include? p.id | |
196 | - parent = map[p.id] | |
192 | + if @map.include? p.id | |
193 | + parent = @map[p.id] | |
197 | 194 | if parent.time < min_time |
198 | 195 | min_time = parent.time |
199 | 196 | end |
... | ... | @@ -209,19 +206,19 @@ module Network |
209 | 206 | |
210 | 207 | # Visit branching chains |
211 | 208 | leaves.each do |l| |
212 | - parents = l.parents.collect.select{|p| map.include? p.id and map[p.id].space.zero?} | |
209 | + parents = l.parents.collect.select{|p| @map.include? p.id and @map[p.id].space.zero?} | |
213 | 210 | for p in parents |
214 | - place_chain(map[p.id], map, l.time) | |
211 | + place_chain(p, l.time) | |
215 | 212 | end |
216 | 213 | end |
217 | 214 | end |
218 | 215 | |
219 | - def get_space_base(leaves, map) | |
216 | + def get_space_base(leaves) | |
220 | 217 | space_base = 1 |
221 | 218 | if leaves.last.parents.size > 0 |
222 | 219 | first_parent = leaves.last.parents.first |
223 | - if map.include?(first_parent.id) | |
224 | - first_p = map[first_parent.id] | |
220 | + if @map.include?(first_parent.id) | |
221 | + first_p = @map[first_parent.id] | |
225 | 222 | if first_p.space > 0 |
226 | 223 | space_base = first_p.space |
227 | 224 | end |
... | ... | @@ -232,7 +229,7 @@ module Network |
232 | 229 | |
233 | 230 | def mark_reserved(time_range, space) |
234 | 231 | for day in time_range |
235 | - @_reserved[day].push(space) | |
232 | + @reserved[day].push(space) | |
236 | 233 | end |
237 | 234 | end |
238 | 235 | |
... | ... | @@ -241,7 +238,7 @@ module Network |
241 | 238 | |
242 | 239 | reserved = [] |
243 | 240 | for day in time_range |
244 | - reserved += @_reserved[day] | |
241 | + reserved += @reserved[day] | |
245 | 242 | end |
246 | 243 | reserved.uniq! |
247 | 244 | |
... | ... | @@ -260,19 +257,19 @@ module Network |
260 | 257 | # Takes most left subtree branch of commits |
261 | 258 | # which don't have space mark yet. |
262 | 259 | # |
263 | - # @param [Graph::Commit] the commit object. | |
264 | - # @param [Hash<String,Graph::Commit>] map of commits | |
260 | + # @param [::Commit] the commit object. | |
265 | 261 | # |
266 | - # @return [Array<Graph::Commit>] list of branch commits | |
267 | - def take_left_leaves(commit, map) | |
262 | + # @return [Array<Network::Commit>] list of branch commits | |
263 | + def take_left_leaves(raw_commit) | |
264 | + commit = @map[raw_commit.id] | |
268 | 265 | leaves = [] |
269 | 266 | leaves.push(commit) if commit.space.zero? |
270 | 267 | |
271 | 268 | while true |
272 | 269 | return leaves if commit.parents.count.zero? |
273 | - return leaves unless map.include? commit.parents.first.id | |
270 | + return leaves unless @map.include? commit.parents.first.id | |
274 | 271 | |
275 | - commit = map[commit.parents.first.id] | |
272 | + commit = @map[commit.parents.first.id] | |
276 | 273 | |
277 | 274 | return leaves unless commit.space.zero? |
278 | 275 | ... | ... |