March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
60
Task/Priority-queue/AutoHotkey/priority-queue-1.ahk
Normal file
60
Task/Priority-queue/AutoHotkey/priority-queue-1.ahk
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
;-----------------------------------
|
||||
PQ_TopItem(Queue,Task:=""){ ; remove and return top priority item
|
||||
TopPriority := PQ_TopPriority(Queue)
|
||||
for T, P in Queue
|
||||
if (P = TopPriority) && ((T=Task)||!Task)
|
||||
return T , Queue.Remove(T)
|
||||
return 0
|
||||
}
|
||||
;-----------------------------------
|
||||
PQ_AddTask(Queue,Task,Priority){ ; insert and return new task
|
||||
for T, P in Queue
|
||||
if (T=Task) || !(Priority && Task)
|
||||
return 0
|
||||
return Task, Queue[Task] := Priority
|
||||
}
|
||||
;-----------------------------------
|
||||
PQ_DelTask(Queue, Task){ ; delete and return task
|
||||
for T, P in Queue
|
||||
if (T = Task)
|
||||
return Task, Queue.Remove(Task)
|
||||
}
|
||||
;-----------------------------------
|
||||
PQ_Peek(Queue){ ; peek and return top priority task(s)
|
||||
TopPriority := PQ_TopPriority(Queue)
|
||||
for T, P in Queue
|
||||
if (P = TopPriority)
|
||||
PeekList .= (PeekList?"`n":"") "`t" T
|
||||
return PeekList
|
||||
}
|
||||
;-----------------------------------
|
||||
PQ_Check(Queue,Task){ ; check task and return its priority
|
||||
for T, P in Queue
|
||||
if (T = Task)
|
||||
return P
|
||||
return 0
|
||||
}
|
||||
;-----------------------------------
|
||||
PQ_Edit(Queue,Task,Priority){ ; Update task priority and return its new priority
|
||||
for T, P in Queue
|
||||
if (T = Task)
|
||||
return Priority, Queue[T]:=Priority
|
||||
return 0
|
||||
}
|
||||
;-----------------------------------
|
||||
PQ_View(Queue){ ; view current Queue
|
||||
for T, P in Queue
|
||||
Res .= P " : " T "`n"
|
||||
Sort, Res, FMySort
|
||||
return "Priority Queue=`n" Res
|
||||
}
|
||||
MySort(a,b){
|
||||
RegExMatch(a,"(\d+) : (.*)", x), RegExMatch(b,"(\d+) : (.*)", y)
|
||||
return x1>y1?1:x1<y1?-1: x2>y2?1:x2<y2?-1: 0
|
||||
}
|
||||
;-----------------------------------
|
||||
PQ_TopPriority(Queue){ ; return queue's top priority
|
||||
for T, P in Queue
|
||||
TopPriority := TopPriority?TopPriority:P , TopPriority := TopPriority<P?TopPriority:P
|
||||
return, TopPriority
|
||||
}
|
||||
22
Task/Priority-queue/AutoHotkey/priority-queue-2.ahk
Normal file
22
Task/Priority-queue/AutoHotkey/priority-queue-2.ahk
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
data =
|
||||
(
|
||||
3 Clear drains
|
||||
1 test
|
||||
4 Feed cat
|
||||
5 Make tea
|
||||
1 Solve RC tasks
|
||||
2 Tax return
|
||||
)
|
||||
PQ:=[] ; Create Priority Queue PQ[Task, Priority]
|
||||
loop, parse, data, `n, `r
|
||||
F:= StrSplit(A_LoopField, "`t") , PQ[F[2]] := F[1]
|
||||
PQ_View(PQ)
|
||||
MsgBox, 262208,, % "Top Priority item(s)=`n" PQ_Peek(PQ) "`n`n" PQ_View(PQ)
|
||||
MsgBox, 262208,, % "Add : " PQ_AddTask(PQ, "AutoHotkey", 2) "`n`n" PQ_View(PQ)
|
||||
MsgBox, 262208,, % "Remove Top Item : " PQ_TopItem(PQ) "`n`n" PQ_View(PQ)
|
||||
MsgBox, 262208,, % "Remove specific top item : " PQ_TopItem(PQ,"test") "`n`n" PQ_View(PQ)
|
||||
MsgBox, 262208,, % "Delete Item : " PQ_DelTask(PQ, "Clear drains") "`n`n" PQ_View(PQ)
|
||||
MsgBox, 262208,, % (Task:="Tax return") " new priority = " PQ_Edit(PQ,task, 7) "`n`n" PQ_View(PQ)
|
||||
MsgBox, 262208,, % (Task:="Feed cat") " priority = " PQ_Check(PQ,task)"`n`n" PQ_View(PQ)
|
||||
^Esc::
|
||||
ExitApp
|
||||
|
|
@ -1,13 +1,13 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
const void *PQRetain(CFAllocatorRef allocator, const void *ptr) {
|
||||
return [(id)ptr retain];
|
||||
return (__bridge_retained const void *)(__bridge id)ptr;
|
||||
}
|
||||
void PQRelease(CFAllocatorRef allocator, const void *ptr) {
|
||||
[(id)ptr release];
|
||||
(void)(__bridge_transfer id)ptr;
|
||||
}
|
||||
CFComparisonResult PQCompare(const void *ptr1, const void *ptr2, void *unused) {
|
||||
return [(id)ptr1 compare:(id)ptr2];
|
||||
return [(__bridge id)ptr1 compare:(__bridge id)ptr2];
|
||||
}
|
||||
|
||||
@interface Task : NSObject {
|
||||
|
|
@ -26,10 +26,6 @@ CFComparisonResult PQCompare(const void *ptr1, const void *ptr2, void *unused) {
|
|||
}
|
||||
return self;
|
||||
}
|
||||
- (void)dealloc {
|
||||
[name release];
|
||||
[super dealloc];
|
||||
}
|
||||
- (NSString *)description {
|
||||
return [NSString stringWithFormat:@"%d, %@", priority, name];
|
||||
}
|
||||
|
|
@ -44,25 +40,25 @@ CFComparisonResult PQCompare(const void *ptr1, const void *ptr2, void *unused) {
|
|||
@end
|
||||
|
||||
int main (int argc, const char *argv[]) {
|
||||
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
|
||||
@autoreleasepool {
|
||||
|
||||
CFBinaryHeapCallBacks callBacks = {0, PQRetain, PQRelease, NULL, PQCompare};
|
||||
CFBinaryHeapRef pq = CFBinaryHeapCreate(NULL, 0, &callBacks, NULL);
|
||||
CFBinaryHeapCallBacks callBacks = {0, PQRetain, PQRelease, NULL, PQCompare};
|
||||
CFBinaryHeapRef pq = CFBinaryHeapCreate(NULL, 0, &callBacks, NULL);
|
||||
|
||||
CFBinaryHeapAddValue(pq, [[[Task alloc] initWithPriority:3 andName:@"Clear drains"] autorelease]);
|
||||
CFBinaryHeapAddValue(pq, [[[Task alloc] initWithPriority:4 andName:@"Feed cat"] autorelease]);
|
||||
CFBinaryHeapAddValue(pq, [[[Task alloc] initWithPriority:5 andName:@"Make tea"] autorelease]);
|
||||
CFBinaryHeapAddValue(pq, [[[Task alloc] initWithPriority:1 andName:@"Solve RC tasks"] autorelease]);
|
||||
CFBinaryHeapAddValue(pq, [[[Task alloc] initWithPriority:2 andName:@"Tax return"] autorelease]);
|
||||
CFBinaryHeapAddValue(pq, [[Task alloc] initWithPriority:3 andName:@"Clear drains"]);
|
||||
CFBinaryHeapAddValue(pq, [[Task alloc] initWithPriority:4 andName:@"Feed cat"]);
|
||||
CFBinaryHeapAddValue(pq, [[Task alloc] initWithPriority:5 andName:@"Make tea"]);
|
||||
CFBinaryHeapAddValue(pq, [[Task alloc] initWithPriority:1 andName:@"Solve RC tasks"]);
|
||||
CFBinaryHeapAddValue(pq, [[Task alloc] initWithPriority:2 andName:@"Tax return"]);
|
||||
|
||||
while (CFBinaryHeapGetCount(pq) != 0) {
|
||||
Task *task = (id)CFBinaryHeapGetMinimum(pq);
|
||||
NSLog(@"%@", task);
|
||||
CFBinaryHeapRemoveMinimumValue(pq);
|
||||
}
|
||||
|
||||
CFRelease(pq);
|
||||
|
||||
while (CFBinaryHeapGetCount(pq) != 0) {
|
||||
Task *task = (id)CFBinaryHeapGetMinimum(pq);
|
||||
NSLog(@"%@", task);
|
||||
CFBinaryHeapRemoveMinimumValue(pq);
|
||||
}
|
||||
|
||||
CFRelease(pq);
|
||||
|
||||
[pool drain];
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue