parametrized the lib

This commit is contained in:
Enrico Fasoli 2015-03-13 11:44:12 +01:00
parent 96e78618c7
commit 8efd9c2cfa
1 changed files with 24 additions and 21 deletions

View File

@ -1,47 +1,50 @@
#!/usr/bin/env coffee #!/usr/bin/env coffee
chalk = require 'chalk'
console.log "pert" verbose = no
list = [{id: 0, depends: [], duration: 2}, { id: 1, depends: [0], duration: 3},{id: 2, depends: [0,1], duration: 4}] log = (x...) -> if verbose then console.log chalk.bold("Log:"), x...
err = (x...) -> console.log chalk.bold (chalk.red "Error:"), x...
# Returns the highest number in an array of numbers
maxa = (l) -> return Math.max.apply null, l maxa = (l) -> return Math.max.apply null, l
# Find the activity with given id # Find the activity with given id
toActivity = (id) -> toActivity = (id,list) ->
if !list? then err "list is",list
item = {} item = {}
list.forEach (x) -> if x.id is id then item = x list.forEach (x) -> if x.id is id then item = x
return item return item
# Find the item # Find the item
calculateEndDay = (item) -> calculateEndDay = (item,list) ->
if !item.startDay? if !item.startDay?
console.log "calculating start day of",item.id log "calculating start day of",item.id
item.startDay = calculateStartDay item item.startDay = calculateStartDay item, list
console.log "Start day of",item.id,":",item.startDay log "start day of",item.id,"is",item.startDay
item.endDay = item.startDay + item.duration item.endDay = item.startDay + item.duration
console.log "End Day of",item.id,":",item.endDay log "end day of",item.id,"is",item.endDay
return item.endDay return item.endDay
# Find out which day the activity starts # Find out which day the activity starts
calculateStartDay = (item) -> calculateStartDay = (item,list) ->
if item.depends.length is 0 then return item.startDay = 0 if item.depends.length is 0 then return item.startDay = 0
console.log "Deps:",item.depends.map(toActivity) max = maxa item.depends.map((x) -> toActivity x,list).map((x) -> calculateEndDay x, list)
console.log "EndDays:",item.depends.map(toActivity).map(calculateEndDay) log "start day of",item.id,"is",max
max = maxa item.depends.map(toActivity).map(calculateEndDay)
console.log max
# write max delay time to each depend # write max delay time to each depend
item.depends.forEach (x) -> item.depends.forEach (x) ->
console.log "Writing permittedDelay to dependency", x log "writing permittedDelay to dependency", x, "of", item
i = toActivity x i = toActivity x, list
i.permittedDelay = max - calculateEndDay(i) i.permittedDelay = max - calculateEndDay i, list
log "permitted delay of",x,"is",i.permittedDelay
return item.startDay = max return item.startDay = max
# Find out which activity has the highest id # Find out which activity has the highest id
highestID = -> return maxa(list.map (x) -> x.id) highestID = (list) -> return maxa(list.map (x) -> x.id)
calculate = -> calculate = (list) ->
calculateEndDay toActivity(highestID()) calculateEndDay (toActivity highestID(list), list), list
list.forEach (x) -> if !x.permittedDelay? then x.final = true
return list return list
console.log calculate() ex = [{id: 0, depends: [], duration: 2}, { id: 1, depends: [0], duration: 3},{id: 2, depends: [0,1], duration: 4}]
console.log calculate ex