File: PriorityQueue.c

package info (click to toggle)
openmpi 5.0.8-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 201,684 kB
  • sloc: ansic: 613,078; makefile: 42,353; sh: 11,194; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,179; python: 1,859; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (174 lines) | stat: -rw-r--r-- 3,780 bytes parent folder | download | duplicates (5)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <stdlib.h>
#include "PriorityQueue.h"

/*
  This comparison function is used to sort elements in key descending order.
*/
int compfunc(const FiboNode * const, const FiboNode * const);



static int compFunc(const FiboNode * const node1, const FiboNode * const node2)
{
  return 
    ( ( ((QueueElement*)(node1))->key > ((QueueElement*)(node2))->key ) ? -1 : 1); 
}

int PQ_init(PriorityQueue * const q, int size)
{
  int i;
  q->size = size;
  q->elements = malloc(sizeof(QueueElement *) * size);
  for(i=0; i < size; i++)
    q->elements[i]=NULL;
  return tm_fiboTreeInit((FiboTree *)q, compFunc);
}

void PQ_exit(PriorityQueue * const q)
{
  
  int i;
  for(i = 0; i < q->size; i++)
    {
      if(q->elements[i] != NULL)
	free(q->elements[i]);
    }
  if(q->elements != NULL)
    free(q->elements);
  tm_fiboTreeExit((FiboTree *)q);
}
void PQ_free(PriorityQueue * const q)
{
  int i;
  for(i = 0; i < q->size; i++)
    {
      if(q->elements[i] != NULL)
	free(q->elements[i]);
    }
  tm_fiboTreeFree((FiboTree *)q);
}

int PQ_isEmpty(PriorityQueue * const q)
{
  FiboTree * tree = (FiboTree *)q;
/* if the tree root is linked to itself then the tree is empty */
  if(&(tree->rootdat) == (tree->rootdat.linkdat.nextptr)) 
    return 1;
  return 0;
}

void PQ_insertElement(PriorityQueue * const q, QueueElement * const e)
{
  if(e->value >= 0 && e->value < q->size)
    {
      fiboTreeAdd((FiboTree *)q, (FiboNode *)(e));
      q->elements[e->value] = e;
      e->isInQueue = 1;
    }
}
void PQ_deleteElement(PriorityQueue * const q, QueueElement * const e)
{
  tm_fiboTreeDel((FiboTree *)q, (FiboNode *)(e));
  q->elements[e->value] = NULL;
  e->isInQueue = 0;
}

void PQ_insert(PriorityQueue * const q, int val, double key)
{
  if( val >= 0 && val < q->size)
    {
      QueueElement * e = malloc(sizeof(QueueElement));
      e->value = val;
      e->key = key;
      PQ_insertElement(q, e);
    }
}

void PQ_delete(PriorityQueue * const q, int val)
{
  QueueElement * e = q->elements[val];
  PQ_deleteElement(q, e);
  free(e);
}

QueueElement * PQ_findMaxElement(PriorityQueue * const q)
{
  QueueElement * e = (QueueElement *)(tm_fiboTreeMin((FiboTree *)q));
  return e;
}
QueueElement * PQ_deleteMaxElement(PriorityQueue * const q)
{
  QueueElement * e = (QueueElement *)(tm_fiboTreeMin((FiboTree *)q));
  if(e != NULL)
    {
      PQ_deleteElement(q, e);
    }
  return e;
}

double PQ_findMaxKey(PriorityQueue * const q)
{
  QueueElement * e = PQ_findMaxElement(q);
  if(e!=NULL)
    return e->key;
  return 0;
}

int PQ_deleteMax(PriorityQueue * const q)
{
  QueueElement * e = PQ_deleteMaxElement(q);
  int res = -1;
  if(e != NULL)
    res = e->value;
  free(e);
  return res;
}

void PQ_increaseElementKey(PriorityQueue * const q, QueueElement * const e, double i)
{
  if(e->isInQueue)
    {
      PQ_deleteElement(q, e);
      e->key += i;
      PQ_insertElement(q, e);
    }
}
void PQ_decreaseElementKey(PriorityQueue * const q, QueueElement * const e, double i)
{
  if(e->isInQueue)
    {
      PQ_deleteElement(q, e);
      e->key -= i;
      PQ_insertElement(q, e);
    }
}
void PQ_adjustElementKey(PriorityQueue * const q, QueueElement * const e, double i)
{
  if(e->isInQueue)
    {    
      PQ_deleteElement(q, e);
      e->key = i;
      PQ_insertElement(q, e);
    }
}

void PQ_increaseKey(PriorityQueue * const q, int val, double i)
{
  QueueElement * e = q->elements[val];
  if(e != NULL)
    PQ_increaseElementKey(q, e, i);
}

void PQ_decreaseKey(PriorityQueue * const q, int val, double i)
{
  QueueElement * e = q->elements[val];
  if(e != NULL)
    PQ_decreaseElementKey(q, e, i);
}

void PQ_adjustKey(PriorityQueue * const q, int val, double i)
{
  QueueElement * e = q->elements[val];
  if(e != NULL)
    PQ_adjustElementKey(q, e, i);
}