Skip to content

Commit

Permalink
Merge pull request #5 from naoki-kishi/2-3
Browse files Browse the repository at this point in the history
問題2-3を解いた
  • Loading branch information
p1ass committed Jun 26, 2019
2 parents 4c83a2f + cf9b2da commit 2f4204b
Show file tree
Hide file tree
Showing 5 changed files with 5,487 additions and 14 deletions.
45 changes: 32 additions & 13 deletions worker/domain/priority_queue.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,62 @@
package domain

type JobPriorityQueue []*Job
type JobPriorityQueue struct {
data []*Job
unusedCap int
}

func NewJobPriorityQueue() *JobPriorityQueue {
return &JobPriorityQueue{}
}

func (pq JobPriorityQueue) Less(i, j int) bool {
if pq[i].Priority > pq[j].Priority {
job1 := pq.data[i]
job2 := pq.data[j]

//優先順位が高いものを先に処理する
if job1.Priority > job2.Priority {
return true
} else if pq[i].Priority < pq[j].Priority {
} else if job1.Priority < job2.Priority {
return false
} else {
}

if pq[i].Created.Before(pq[j].Created) {
// 終戦順位が同じもの同士のときは、空いているキャパシティをなるべく埋めるようにする
task1 := job1.Tasks[job1.CurrentTask]
task2 := job2.Tasks[job2.CurrentTask]
if task1 <= pq.unusedCap && task2 > pq.unusedCap {
return true
} else if task1 > pq.unusedCap && task2 <= pq.unusedCap {
return false
} else {
if task1 < task2 {
return true
} else {
return false
}
return false
}
}

func (pq JobPriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq.data[i], pq.data[j] = pq.data[j], pq.data[i]
}

func (pq JobPriorityQueue) Len() int {
return len(pq)
return len(pq.data)
}

func (pq *JobPriorityQueue) Push(val interface{}) {
if v, ok := val.(*Job); ok {
*pq = append(*pq, v)
pq.data = append(pq.data, v)
}
}

func (pq *JobPriorityQueue) Pop() interface{} {
old := *pq
size := len(*pq)
job := old[size-1]
*pq = old[:size-1]
size := len(pq.data)
job := pq.data[size-1]
pq.data = pq.data[:size-1]
return job
}

func (pq *JobPriorityQueue) SetUnusedCap(unusedCap int) {
pq.unusedCap = unusedCap
}
7 changes: 7 additions & 0 deletions worker/domain/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func (w *Worker) ExecuteAllJob(secs int) int {

w.workingJobs = newWorkingJob
w.currentPoint = sumPoint

w.updatePQ()
w.moveJobToWorking()
}
return w.currentPoint
Expand Down Expand Up @@ -88,3 +90,8 @@ func (w *Worker) moveJobToWorking() int {
func (w *Worker) NumOfJob() int {
return len(w.workingJobs)
}

func (w *Worker) updatePQ() {
w.jobPQ.SetUnusedCap(w.capacity - w.currentPoint)
heap.Init(w.jobPQ)
}
39 changes: 38 additions & 1 deletion worker/domain/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func TestWorker_ExecuteAllJob(t *testing.T) {
},
},
currentPoint: 8,
capacity: 10,
capacity: 11,
},
args: args{
secs: 1,
Expand Down Expand Up @@ -244,6 +244,43 @@ func TestWorker_ExecuteAllJob(t *testing.T) {
wantNum: 1,
wantQueueNum: 1,
},
{
name: "優先順位が同じ時余っているキャパシティ以下のタスクを持っているJobを先に処理する",
fields: fields{
workingJobs: []*Job{
{
ID: 0,
Created: time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC),
Priority: 0,
Tasks: []int{2, 5},
CurrentTask: 0,
},
},
jobQueue: []*Job{
{
ID: 1,
Created: time.Date(0, 1, 1, 0, 0, 1, 0, time.UTC),
Priority: 1,
Tasks: []int{10, 1},
CurrentTask: 0,
}, {
ID: 1,
Created: time.Date(0, 1, 1, 0, 0, 2, 0, time.UTC),
Priority: 1,
Tasks: []int{8, 1},
CurrentTask: 0,
},
},
currentPoint: 1,
capacity: 15,
},
args: args{
secs: 1,
},
wantPoint: 9,
wantNum: 2,
wantQueueNum: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading

0 comments on commit 2f4204b

Please sign in to comment.