2644b5f677b05b109ac75f295d3911593b0b762f
[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
26 #include "hash-pjw.h"
27 #include "same.h"
28 #include "same-inode.h"
29
30 /* Hash an F_triple, and *do* consider the file name.  */
31 size_t
32 triple_hash (void const *x, size_t table_size)
33 {
34   struct F_triple const *p = x;
35   size_t tmp = hash_pjw (p->name, table_size);
36
37   /* Ignoring the device number here should be fine.  */
38   return (tmp ^ p->st_ino) % table_size;
39 }
40
41 /* Hash an F_triple, without considering the file name.  */
42 size_t
43 triple_hash_no_name (void const *x, size_t table_size)
44 {
45   struct F_triple const *p = x;
46
47   /* Ignoring the device number here should be fine.  */
48   return p->st_ino % table_size;
49 }
50
51 /* Compare two F_triple structs.  */
52 bool
53 triple_compare (void const *x, void const *y)
54 {
55   struct F_triple const *a = x;
56   struct F_triple const *b = y;
57   return (SAME_INODE (*a, *b) && same_name (a->name, b->name)) ? true : false;
58 }
59
60 /* Free an F_triple.  */
61 void
62 triple_free (void *x)
63 {
64   struct F_triple *a = x;
65   free (a->name);
66   free (a);
67 }