data_structures
graph_adjacent.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* graph_adjacent.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/22 15:51:34 by unite #+# #+# */
10 /* Updated: 2020/09/07 21:49:18 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "graph.h"
15 
16 int graph_adjacent(const t_graph *graph, const void *v1, const void *v2)
17 {
18  size_t i;
19  const t_array *adjacency;
20 
21  if (!rbt_contains(graph->adj, v1) || !rbt_contains(graph->adj, v2))
22  ds_exit_set(EINVAL);
23  adjacency = rbt_get(graph->adj, v1);
24  i = 0;
25  while (i < array_size(adjacency))
26  {
27  if (graph->type->cmp(v2, array_get(adjacency, i)) == 0)
28  return (1);
29  i++;
30  }
31  return (0);
32 }
rbt_contains
int rbt_contains(const t_rbt *rbt, const void *key)
Does the tree contain the specified key?
Definition: rbt_contains.c:16
array_get
void * array_get(const t_array *array, size_t index)
Returns the item at the specified position in this list.
Definition: array_get.c:16
array_size
size_t array_size(const t_array *array)
Returns the number of elements in this array.
Definition: array_size.c:16
s_type::cmp
int(* cmp)(const void *, const void *)
(optional) A function ponter used to compare members of this data type
Definition: types.h:52
s_graph::adj
t_rbt * adj
The set of arrays, where each arrays represents the adjacency of a given vertex.
Definition: graph.h:43
s_graph
A graph implemented using a set of arrays.
Definition: graph.h:39
s_graph::type
const t_type * type
The type of vertices in the graph.
Definition: graph.h:44
s_array
A resizable array.
Definition: array.h:47
graph_adjacent
int graph_adjacent(const t_graph *graph, const void *v1, const void *v2)
Are two vertices in the graph adjacent to each other?
Definition: graph_adjacent.c:16
rbt_get
void * rbt_get(const t_rbt *rbt, const void *key)
Returns the value associated with a specified key.
Definition: rbt_get.c:16
graph.h
ds_exit_set
void ds_exit_set(int err)
Set errno to the specified value, print the error message, and exit the process.
Definition: ds_exit_set.c:22