Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): add HashSet type



details:   https://anonhg.NetBSD.org/src/rev/5f57886c7807
branches:  trunk
changeset: 978516:5f57886c7807
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon Nov 23 18:07:10 2020 +0000

description:
make(1): add HashSet type

This makes the code for handling suffixes simpler since it doesn't need
the clumsy API of HashTable_CreateEntry anymore.

diffstat:

 usr.bin/make/hash.h |  34 +++++++++++++++++++++++++++++++++-
 usr.bin/make/suff.c |  26 ++++++++++----------------
 2 files changed, 43 insertions(+), 17 deletions(-)

diffs (120 lines):

diff -r 86acb1daa34a -r 5f57886c7807 usr.bin/make/hash.h
--- a/usr.bin/make/hash.h       Mon Nov 23 17:59:21 2020 +0000
+++ b/usr.bin/make/hash.h       Mon Nov 23 18:07:10 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: hash.h,v 1.34 2020/11/23 17:59:21 rillig Exp $ */
+/*     $NetBSD: hash.h,v 1.35 2020/11/23 18:07:10 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -103,6 +103,11 @@
        HashEntry *entry;       /* Next entry to check in current bucket. */
 } HashIter;
 
+/* A set of strings. */
+typedef struct HashSet {
+       HashTable tbl;
+} HashSet;
+
 MAKE_INLINE void *
 HashEntry_Get(HashEntry *h)
 {
@@ -129,4 +134,31 @@
 void HashIter_Init(HashIter *, HashTable *);
 HashEntry *HashIter_Next(HashIter *);
 
+MAKE_INLINE void
+HashSet_Init(HashSet *set)
+{
+       HashTable_Init(&set->tbl);
+}
+
+MAKE_INLINE void
+HashSet_Done(HashSet *set)
+{
+       HashTable_Done(&set->tbl);
+}
+
+MAKE_INLINE Boolean
+HashSet_Add(HashSet *set, const char *key)
+{
+       Boolean isNew;
+
+       (void)HashTable_CreateEntry(&set->tbl, key, &isNew);
+       return isNew;
+}
+
+MAKE_INLINE Boolean
+HashSet_Contains(HashSet *set, const char *key)
+{
+       return HashTable_FindEntry(&set->tbl, key) != NULL;
+}
+
 #endif /* MAKE_HASH_H */
diff -r 86acb1daa34a -r 5f57886c7807 usr.bin/make/suff.c
--- a/usr.bin/make/suff.c       Mon Nov 23 17:59:21 2020 +0000
+++ b/usr.bin/make/suff.c       Mon Nov 23 18:07:10 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: suff.c,v 1.306 2020/11/23 14:47:12 rillig Exp $        */
+/*     $NetBSD: suff.c,v 1.307 2020/11/23 18:07:10 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -114,7 +114,7 @@
 #include "dir.h"
 
 /*     "@(#)suff.c     8.4 (Berkeley) 3/21/94" */
-MAKE_RCSID("$NetBSD: suff.c,v 1.306 2020/11/23 14:47:12 rillig Exp $");
+MAKE_RCSID("$NetBSD: suff.c,v 1.307 2020/11/23 18:07:10 rillig Exp $");
 
 #define SUFF_DEBUG0(text) DEBUG0(SUFF, text)
 #define SUFF_DEBUG1(fmt, arg1) DEBUG1(SUFF, fmt, arg1)
@@ -1060,9 +1060,9 @@
 static Candidate *
 FindThem(CandidateList *srcs, CandidateSearcher *cs)
 {
-    HashTable seen;
+    HashSet seen;
 
-    HashTable_Init(&seen);
+    HashSet_Init(&seen);
 
     while (!Lst_IsEmpty(srcs)) {
        Candidate *src = Lst_Dequeue(srcs);
@@ -1078,7 +1078,7 @@
         */
        if (Targ_FindNode(src->file) != NULL) {
        found:
-           HashTable_Done(&seen);
+           HashSet_Done(&seen);
            SUFF_DEBUG0("got it\n");
            return src;
        }
@@ -1093,22 +1093,16 @@
 
        SUFF_DEBUG0("not there\n");
 
-       {
-           Boolean isNew;
-
-           HashTable_CreateEntry(&seen, src->file, &isNew);
-           if (isNew)
-               CandidateList_AddCandidatesFor(srcs, src);
-           else {
-               DEBUG1(SUFF, "FindThem: skipping duplicate \"%s\"\n",
-                      src->file);
-           }
+       if (HashSet_Add(&seen, src->file))
+           CandidateList_AddCandidatesFor(srcs, src);
+       else {
+           SUFF_DEBUG1("FindThem: skipping duplicate \"%s\"\n", src->file);
        }
 
        CandidateSearcher_Add(cs, src);
     }
 
-    HashTable_Done(&seen);
+    HashSet_Done(&seen);
     return NULL;
 }
 



Home | Main Index | Thread Index | Old Index