--- /dev/null
+#ifndef __PQUEUE_H_
+#define __PQUEUE_H_
+
+#include<hashmap.h>
+
+struct priority_queue_entry {
+ void *p;
+ int priority;
+ size_t index;
+};
+
+struct priority_queue {
+ struct priority_queue_entry **queue;
+ size_t size;
+
+ struct hash_map *map;
+};
+
+int pqueue_access(struct priority_queue*,const void*,size_t);
+int pqueue_add(struct priority_queue*,const void*,size_t,void*);
+void *pqueue_find(struct priority_queue*,const void*,size_t);
+void pqueue_free(struct priority_queue*);
+int pqueue_init(struct priority_queue**,size_t);
+
+#endif
--- /dev/null
+#include<pqueue.h>
+
+static int pqueue_swap(struct priority_queue*,size_t,size_t);
+
+int pqueue_access(struct priority_queue *queue, const void *key, size_t key_size) {
+ struct priority_queue_entry *p;
+ size_t index;
+
+ if(NULL==queue) { return -1; }
+ if((NULL==key)||(0==key_size)) { return -1; }
+
+ p = hashmap_find(queue->map,key,key_size);
+ if(NULL==p) { return 0; }
+
+ p->priority++;
+
+ index = p->index;
+ while(index>0) {
+ if(queue->queue[index-1]->priority<p->priority) {
+ index--;
+ }
+ }
+
+ if(p->index!=index) {
+ pqueue_swap(queue,index,p->index);
+ }
+
+ return 1;
+}
+
+int pqueue_add(struct priority_queue *queue, const void *key, size_t key_size, void *value) {
+ struct priority_queue_entry *p;
+
+ if(NULL==queue) { return -1; }
+ if((NULL==key)||(0==key_size)) { return -1; }
+
+ if(hashmap_find(queue->map,key,key_size)!=NULL) { return 0; }
+
+ if(priority_queue_entry_init(&p)<0) { return -1; }
+ p->p = value;
+
+ for(size_t i=0;i<queue->size;i++) {
+ if(queue->queue[i]!=NULL) {
+ queue->queue[i] = p;
+ p->index = i;
+ return 1;
+ }
+ }
+
+ return -1;
+}
+
+void *pqueue_evict(struct priority_queue *queue, const void *key, size_t key_size) {
+ struct priority_queue_entry *entry;
+ void *p;
+
+ entry = hashmap_find(queue->map,key,key_size);
+ if(entry!=NULL) {
+ p = entry->p;
+
+ for(size_t i=entry->index;i<(queue->size-1);i++) {
+ if(NULL==queue->queue[i+1]) {
+ pqueue_swap(queue,entry->index,i+1);
+ break;
+ }
+ }
+
+ free(entry);
+ return p;
+ }
+
+ return NULL;
+}
+
+void *pqueue_find(struct priority_queue *queue, const void *key, size_t key_size) {
+ return NULL;
+}
+
+void pqueue_free(struct priority_queue *queue) {
+ if(queue->map!=NULL) { hashmap_free(queue->map); }
+ if(queue->queue!=NULL) { free(queue->queue); }
+
+ free(queue);
+}
+
+int pqueue_init(struct priority_queue **p, size_t size) {
+ if(p==NULL) { return -1; }
+ if(0==size) { return -1; }
+
+ *p = malloc(sizeof(struct priority_queue));
+ if(NULL==(*p)) {
+ perror("malloc");
+ return -1;
+ }
+
+ (*p)->queue = NULL;
+ (*p)->size = size;
+ (*p)->map = NULL;
+
+ if(hashmap_init(&((*p)->map),size)<0) { pqueue_free(*p); }
+
+ (*p)->queue = malloc(sizeof(void*)*size);
+ if(NULL==(*p)->queue) { pqueue_free(*p); }
+
+ for(size_t i=0;i<size;i++) {
+ (*p)->queue[i] = NULL;
+ }
+
+ return 1;
+}
+
+static void pqueue_swap(struct priority_queue *queue, size_t a, size_t b) {
+ struct priority_queue_entry *p;
+
+ queue->queue[a] = p;
+
+ queue->queue[a] = queue->queue[b];
+ queue->queue[a]->index = a;
+
+ queue->queue[b] = p;
+ queue->queue[b]->index = b;
+}