OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
pqueue-ll.c
Go to the documentation of this file.
1 #include "pqueue.h"
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 
6 typedef struct pqueue_node pqueue_node;
7 
8 struct pqueue_node {
9  double priority;
10  void *item;
12 };
13 
14 struct pqueue {
16 };
17 
19  pqueue *ret = (pqueue*) malloc(sizeof(pqueue));
20  ret->start = NULL;
21  return ret;
22 }
23 
25  pqueue_node *tmp = pq->start;
26  while (tmp) {
27  pqueue_node *tmp_was = tmp;
28  tmp = tmp->next;
29  free(tmp_was);
30  };
31  free(pq);
32 }
33 
34 void pqueue_push(pqueue *pq, double priority, void *item) {
35  pqueue_node *new = (pqueue_node*) malloc(sizeof(pqueue_node));
36  new->priority = priority;
37  new->item = item;
38  new->next = NULL;
39  if (pq->start == NULL) {
40  pq->start = new;
41  } else if (priority <= pq->start->priority) {
42  new->next = pq->start;
43  pq->start = new;
44  } else {
45  pqueue_node *q = pq->start;
46  while (q->next != NULL && q->next->priority <= priority) {
47  q = q->next;
48  }
49  new->next = q->next;
50  q->next = new;
51  }
52 }
53 
54 void pqueue_repush(pqueue *pq, double priority, void *item) {
55  pqueue_node *current, *current_parent = NULL;
56  for (current = pq->start; current != NULL; current_parent = current, current = current->next) {
57  if (current->item == item) {
58  if (current->priority != priority) {
59  if (pq->start->priority > priority) {
60  current->priority = priority;
61  if (current_parent) {
62  current_parent->next = current->next;
63  current->next = pq->start;
64  pq->start = current;
65  }
66  } else {
67  pqueue_node *new_parent;
68  if (priority > current->priority) {
69  new_parent = current;
70  } else {
71  new_parent = pq->start;
72  }
73  while (new_parent->next != NULL && new_parent->next->priority <= priority) {
74  new_parent = new_parent->next;
75  }
76  current->priority = priority;
77  if (new_parent != current) {
78  if (current_parent) {
79  current_parent->next = current->next;
80  } else {
81  pq->start = pq->start->next;
82  }
83  pqueue_node *tmp = new_parent->next;
84  new_parent->next = current;
85  current->next = tmp;
86  }
87  }
88  break;
89  }
90  }
91  }
92 }
93 
94 void* pqueue_pull(pqueue *pq) {
95  if (pq->start == NULL) {
96  return NULL;
97  } else {
98  pqueue_node *old_start = pq->start;
99  pq->start = pq->start->next;
100  void *tmp = old_start->item;
101  free(old_start);
102  return tmp;
103  }
104 }
105 
106 void pqueue_display(pqueue *pq, void (*print_item)(void *item)) {
107  printf("[");
108  pqueue_node *tmp;
109  for (tmp = pq->start; tmp != NULL; tmp = tmp->next) {
110  printf("p%.2f => ", tmp->priority);
111  if (print_item) {
112  print_item(tmp->item);
113  } else {
114  printf("%p", tmp->item);
115  }
116  if (tmp->next != NULL) {
117  printf(", ");
118  }
119  }
120  printf("]");
121 }
data_t q
Definition: decode_rs.h:74
void pqueue_push(pqueue *pq, double priority, void *item)
Definition: pqueue-ll.c:34
MOD_PR02AQUA Production where MYD is the prefix denoting AQUA file output The nominal number of executions is per day Each execution current
void * item
Definition: pqueue-ll.c:10
#define NULL
Definition: decode_rs.h:63
void pqueue_destroy(pqueue *pq)
Definition: pqueue-ll.c:24
double priority
Definition: pqueue-ll.c:9
void pqueue_repush(pqueue *pq, double priority, void *item)
Definition: pqueue-ll.c:54
data_t tmp
Definition: decode_rs.h:74
pqueue_node * next
Definition: pqueue-ll.c:11
pqueue_node * start
Definition: pqueue-ll.c:15
pqueue * pqueue_create()
Definition: pqueue-ll.c:18
void pqueue_display(pqueue *pq, void(*print_item)(void *item))
Definition: pqueue-ll.c:106
void * pqueue_pull(pqueue *pq)
Definition: pqueue-ll.c:94