-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.h
120 lines (106 loc) · 3.75 KB
/
types.h
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
#ifndef __TYPES__
#define __TYPES__
#include <stdlib.h>
#include <string.h>
#include "error.h"
//*****************************************************************************************
// GLOBAL CONSTANTS
// Value used to denote that a vertex is not matched.
const int UNMATCHED = 2147483647;
// A big number.
const double _INFINITY = 1000000000.0;
// The type for the edge costs.
typedef double costType;
//*****************************************************************************************
// PERSON STRUCTURE
/*
Structure that contains the necessary information of a Person Vertex.
This information is the number of neighbors that the person has,
an array for the indices of the neighbors, and an array for the edge
costs of the person with its neighbors in the same order.
*/
typedef struct
{
// Number of neighbors of the person.
int num_neighbors;
// Array of neighbors indices (the indices are in the range {0, ... , num_objects - 1}.
int *neighbors;
// Array of edge costs with the neighbors, in the same order as the 'neighbors' array.
// i.e. this person has an edge with object neighbor[i] with cost costs[i].
costType *costs;
} Person;
//***********************************
// PERSON METHODS
// Safe destructor: free the memory and set members to default values.
void Person_Clear(Person *P)
{
if (P->num_neighbors > 0)
{
free((char*) P->neighbors);
free((char*) P->costs);
}
P->num_neighbors = 0;
P->neighbors = NULL;
P->costs = NULL;
}
// Safe allocate arrays for neighbors and costs of size 'num_neighbors' each.
// The member 'num_neighbors' of the person will be set to the correct value.
// In case of error, an Error instance will be returned.
Error Person_Allocate(Person *P, int num_neighbors)
{
Error error = {-1, "Error: no memory for the allocation of a person in 'Person_Allocate'"};
// Clear the person just in case.
Person_Clear(P);
// Only try to allocate space for >= 0 number of neighbors.
if (num_neighbors >= 0)
{
// Try to allocate the neighbors.
P->neighbors = (int*) malloc(num_neighbors * sizeof(int));
if (P->neighbors == NULL) return error;
// Try to allocate the costs.
P->costs = (costType*) malloc(num_neighbors * sizeof(costType));
if (P->costs == NULL)
{
free((char *) P->neighbors);
P->neighbors = NULL;
return error;
}
P->num_neighbors = num_neighbors;
error.code = 1;
strcpy(error.msg, "OK");
return error;
}
// Error if negative number of neighbors.
strcpy(error.msg, "Error: can't allocate space for a negative number of neighbors of a person in 'Person_Allocate'");
return error;
}
//*****************************************************************************************
// GRAPH STRUCTURE
/*
Since the Auction algorithm only works with the adjacency lists of the Persons,
then we will only store such adjacency lists. A Graph structure is simply a
pointer to an array of Person structures.
*/
typedef Person* Graph;
//***********************************
// GRAPH METHODS
// Safe destructor: free the memory allocated for each person in the Graph and the Graph (array) itself.
// The number of elements in the array is needed ('num_persons' argument).
// Also set members to default values.
void Graph_Clear(Graph *Persons, int num_persons)
{
for(int person_i = 0; person_i < num_persons; ++person_i)
Person_Clear(&(*Persons)[person_i]);
free((char*) *Persons);
*Persons = NULL;
}
// Safe allocate the array of Person structures for 'num_persons' elements.
// Every byte initialized to zero for safe memory free in case of errors.
// If the memory was not allocated the pointer will point to NULL.
void Graph_Allocate(Graph *Persons, int num_persons)
{
if (*Persons != NULL) free((char*) *Persons);
*Persons = NULL;
*Persons = (Graph) calloc(num_persons * sizeof(Person), 1);
}
#endif