Fix canonicalize loop-detection corner case.
[gnulib.git] / lib / hash-triple.c
1 /* Hash functions for file-related triples: name, device, inode.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* written by Jim Meyering */
19
20 #include <config.h>
21
22 #include "hash-triple.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "hash-pjw.h"
28 #include "same.h"
29 #include "same-inode.h"
30
31 #define STREQ(a, b) (strcmp ((a), (b)) == 0)
32
33 /* Hash an F_triple, and *do* consider the file name.  */
34 size_t
35 triple_hash (void const *x, size_t table_size)
36 {
37   struct F_triple const *p = x;
38   size_t tmp = hash_pjw (p->name, table_size);
39
40   /* Ignoring the device number here should be fine.  */
41   return (tmp ^ p->st_ino) % table_size;
42 }
43
44 /* Hash an F_triple, without considering the file name.  */
45 size_t
46 triple_hash_no_name (void const *x, size_t table_size)
47 {
48   struct F_triple const *p = x;
49
50   /* Ignoring the device number here should be fine.  */
51   return p->st_ino % table_size;
52 }
53
54 /* Compare two F_triple structs.  */
55 bool
56 triple_compare (void const *x, void const *y)
57 {
58   struct F_triple const *a = x;
59   struct F_triple const *b = y;
60   return (SAME_INODE (*a, *b) && same_name (a->name, b->name)) ? true : false;
61 }
62
63 bool
64 triple_compare_ino_str (void const *x, void const *y)
65 {
66   struct F_triple const *a = x;
67   struct F_triple const *b = y;
68   return (SAME_INODE (*a, *b) && STREQ (a->name, b->name)) ? true : false;
69 }
70
71 /* Free an F_triple.  */
72 void
73 triple_free (void *x)
74 {
75   struct F_triple *a = x;
76   free (a->name);
77   free (a);
78 }