Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 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 61 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 | 2x 2x 13x 26x 26x 15x 4x 8x 2x 4x 2x 4x 1x 26x 31x 31x 31x 16x 15x | /**
* 태스크 큐 관리
* 우선순위 기반 태스크 큐 구현
*/
import { Task, TaskPriority } from "../types/index.js";
/**
* 태스크 큐 인터페이스
*/
export interface ITaskQueue<T = any, R = any> {
/** 태스크 추가 */
enqueue(task: Task<T, R>): void;
/** 우선순위가 가장 높은 태스크 반환 및 제거 */
dequeue(): Task<T, R> | undefined;
/** 큐가 비어있는지 확인 */
isEmpty(): boolean;
/** 큐의 크기 반환 */
size(): number;
/** 특정 태스크 ID로 태스크 제거 */
remove(taskId: string): boolean;
/** 모든 태스크 반환 */
getAll(): Task<T, R>[];
/** 큐 비우기 */
clear(): void;
}
/**
* 우선순위 기반 태스크 큐 클래스
*/
export class TaskQueue<T = any, R = any> implements ITaskQueue<T, R> {
/** 태스크 배열 */
private queue: Task<T, R>[] = [];
/** 태스크 추가 */
enqueue(task: Task<T, R>): void {
this.queue.push(task);
this.sort();
}
/** 우선순위가 가장 높은 태스크 반환 및 제거 */
dequeue(): Task<T, R> | undefined {
return this.queue.shift();
}
/** 큐가 비어있는지 확인 */
isEmpty(): boolean {
return this.queue.length === 0;
}
/** 큐의 크기 반환 */
size(): number {
return this.queue.length;
}
/** 특정 태스크 ID로 태스크 제거 */
remove(taskId: string): boolean {
const initialSize = this.queue.length;
this.queue = this.queue.filter((task) => task.id !== taskId);
return this.queue.length < initialSize;
}
/** 모든 태스크 반환 */
getAll(): Task<T, R>[] {
return [...this.queue];
}
/** 큐 비우기 */
clear(): void {
this.queue = [];
}
/** 우선순위에 따라 큐 정렬 */
private sort(): void {
this.queue.sort((a, b) => {
// 우선순위 비교 (낮은 값이 높은 우선순위)
const priorityA =
a.priority !== undefined ? a.priority : TaskPriority.NORMAL;
const priorityB =
b.priority !== undefined ? b.priority : TaskPriority.NORMAL;
// 우선순위가 다르면 우선순위로 정렬
if (priorityA !== priorityB) {
return priorityA - priorityB;
}
// 우선순위가 같으면 제출 시간으로 정렬 (먼저 제출된 것이 우선)
return a.submittedAt - b.submittedAt;
});
}
}
|