Refactor: change the map hash from a local variable to private variable.
This commit is contained in:
parent
ccc712b198
commit
79cd1ca304
1 changed files with 31 additions and 34 deletions
|
@ -44,33 +44,31 @@ module Network
|
||||||
# list of commits. As well as returns date list
|
# list of commits. As well as returns date list
|
||||||
# corelated with time set on commits.
|
# corelated with time set on commits.
|
||||||
#
|
#
|
||||||
# @param [Array<Graph::Commit>] commits to index
|
|
||||||
#
|
|
||||||
# @return [Array<TimeDate>] list of commit dates corelated with time on commits
|
# @return [Array<TimeDate>] list of commit dates corelated with time on commits
|
||||||
def index_commits
|
def index_commits
|
||||||
days = []
|
days = []
|
||||||
map = {}
|
@map = {}
|
||||||
|
|
||||||
@commits.reverse.each_with_index do |c,i|
|
@commits.reverse.each_with_index do |c,i|
|
||||||
c.time = i
|
c.time = i
|
||||||
days[i] = c.committed_date
|
days[i] = c.committed_date
|
||||||
map[c.id] = c
|
@map[c.id] = c
|
||||||
end
|
end
|
||||||
|
|
||||||
@_reserved = {}
|
@reserved = {}
|
||||||
days.each_index do |i|
|
days.each_index do |i|
|
||||||
@_reserved[i] = []
|
@reserved[i] = []
|
||||||
end
|
end
|
||||||
|
|
||||||
commits_sort_by_ref.each do |commit|
|
commits_sort_by_ref.each do |commit|
|
||||||
if map.include? commit.id then
|
if @map.include? commit.id then
|
||||||
place_chain(map[commit.id], map)
|
place_chain(commit)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# find parent spaces for not overlap lines
|
# find parent spaces for not overlap lines
|
||||||
@commits.each do |c|
|
@commits.each do |c|
|
||||||
c.parent_spaces.concat(find_free_parent_spaces(c, map))
|
c.parent_spaces.concat(find_free_parent_spaces(c))
|
||||||
end
|
end
|
||||||
|
|
||||||
days
|
days
|
||||||
|
@ -114,12 +112,12 @@ module Network
|
||||||
heads.include?(@ref)
|
heads.include?(@ref)
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_free_parent_spaces(commit, map)
|
def find_free_parent_spaces(commit)
|
||||||
spaces = []
|
spaces = []
|
||||||
|
|
||||||
commit.parents.each do |p|
|
commit.parents.each do |p|
|
||||||
if map.include?(p.id) then
|
if @map.include?(p.id) then
|
||||||
parent = map[p.id]
|
parent = @map[p.id]
|
||||||
|
|
||||||
range = if commit.time < parent.time then
|
range = if commit.time < parent.time then
|
||||||
commit.time..parent.time
|
commit.time..parent.time
|
||||||
|
@ -164,23 +162,22 @@ module Network
|
||||||
|
|
||||||
# Add space mark on commit and its parents
|
# Add space mark on commit and its parents
|
||||||
#
|
#
|
||||||
# @param [Graph::Commit] the commit object.
|
# @param [::Commit] the commit object.
|
||||||
# @param [Hash<String,Graph::Commit>] map of commits
|
def place_chain(commit, parent_time = nil)
|
||||||
def place_chain(commit, map, parent_time = nil)
|
leaves = take_left_leaves(commit)
|
||||||
leaves = take_left_leaves(commit, map)
|
|
||||||
if leaves.empty?
|
if leaves.empty?
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
time_range = leaves.last.time..leaves.first.time
|
time_range = leaves.last.time..leaves.first.time
|
||||||
space_base = get_space_base(leaves, map)
|
space_base = get_space_base(leaves)
|
||||||
space = find_free_space(time_range, 2, space_base)
|
space = find_free_space(time_range, 2, space_base)
|
||||||
leaves.each do |l|
|
leaves.each do |l|
|
||||||
l.spaces << space
|
l.spaces << space
|
||||||
# Also add space to parent
|
# Also add space to parent
|
||||||
l.parents.each do |p|
|
l.parents.each do |p|
|
||||||
if map.include?(p.id)
|
if @map.include?(p.id)
|
||||||
parent = map[p.id]
|
parent = @map[p.id]
|
||||||
if parent.space > 0
|
if parent.space > 0
|
||||||
parent.spaces << space
|
parent.spaces << space
|
||||||
end
|
end
|
||||||
|
@ -192,8 +189,8 @@ module Network
|
||||||
min_time = leaves.last.time
|
min_time = leaves.last.time
|
||||||
parents = leaves.last.parents.collect
|
parents = leaves.last.parents.collect
|
||||||
parents.each do |p|
|
parents.each do |p|
|
||||||
if map.include? p.id
|
if @map.include? p.id
|
||||||
parent = map[p.id]
|
parent = @map[p.id]
|
||||||
if parent.time < min_time
|
if parent.time < min_time
|
||||||
min_time = parent.time
|
min_time = parent.time
|
||||||
end
|
end
|
||||||
|
@ -209,19 +206,19 @@ module Network
|
||||||
|
|
||||||
# Visit branching chains
|
# Visit branching chains
|
||||||
leaves.each do |l|
|
leaves.each do |l|
|
||||||
parents = l.parents.collect.select{|p| map.include? p.id and map[p.id].space.zero?}
|
parents = l.parents.collect.select{|p| @map.include? p.id and @map[p.id].space.zero?}
|
||||||
for p in parents
|
for p in parents
|
||||||
place_chain(map[p.id], map, l.time)
|
place_chain(p, l.time)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_space_base(leaves, map)
|
def get_space_base(leaves)
|
||||||
space_base = 1
|
space_base = 1
|
||||||
if leaves.last.parents.size > 0
|
if leaves.last.parents.size > 0
|
||||||
first_parent = leaves.last.parents.first
|
first_parent = leaves.last.parents.first
|
||||||
if map.include?(first_parent.id)
|
if @map.include?(first_parent.id)
|
||||||
first_p = map[first_parent.id]
|
first_p = @map[first_parent.id]
|
||||||
if first_p.space > 0
|
if first_p.space > 0
|
||||||
space_base = first_p.space
|
space_base = first_p.space
|
||||||
end
|
end
|
||||||
|
@ -232,7 +229,7 @@ module Network
|
||||||
|
|
||||||
def mark_reserved(time_range, space)
|
def mark_reserved(time_range, space)
|
||||||
for day in time_range
|
for day in time_range
|
||||||
@_reserved[day].push(space)
|
@reserved[day].push(space)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -241,7 +238,7 @@ module Network
|
||||||
|
|
||||||
reserved = []
|
reserved = []
|
||||||
for day in time_range
|
for day in time_range
|
||||||
reserved += @_reserved[day]
|
reserved += @reserved[day]
|
||||||
end
|
end
|
||||||
reserved.uniq!
|
reserved.uniq!
|
||||||
|
|
||||||
|
@ -260,19 +257,19 @@ module Network
|
||||||
# Takes most left subtree branch of commits
|
# Takes most left subtree branch of commits
|
||||||
# which don't have space mark yet.
|
# which don't have space mark yet.
|
||||||
#
|
#
|
||||||
# @param [Graph::Commit] the commit object.
|
# @param [::Commit] the commit object.
|
||||||
# @param [Hash<String,Graph::Commit>] map of commits
|
|
||||||
#
|
#
|
||||||
# @return [Array<Graph::Commit>] list of branch commits
|
# @return [Array<Network::Commit>] list of branch commits
|
||||||
def take_left_leaves(commit, map)
|
def take_left_leaves(raw_commit)
|
||||||
|
commit = @map[raw_commit.id]
|
||||||
leaves = []
|
leaves = []
|
||||||
leaves.push(commit) if commit.space.zero?
|
leaves.push(commit) if commit.space.zero?
|
||||||
|
|
||||||
while true
|
while true
|
||||||
return leaves if commit.parents.count.zero?
|
return leaves if commit.parents.count.zero?
|
||||||
return leaves unless map.include? commit.parents.first.id
|
return leaves unless @map.include? commit.parents.first.id
|
||||||
|
|
||||||
commit = map[commit.parents.first.id]
|
commit = @map[commit.parents.first.id]
|
||||||
|
|
||||||
return leaves unless commit.space.zero?
|
return leaves unless commit.space.zero?
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue