git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Julian Phillips <julian@quantumfyre.co.uk>
To: git@vger.kernel.org
Subject: [RFC/PATCH 1/2] ref-dict: Add a set of functions for working with a ref dictionary
Date: Wed, 16 Sep 2009 08:53:02 +0100	[thread overview]
Message-ID: <20090916075304.58044.664.julian@quantumfyre.co.uk> (raw)
In-Reply-To: <20090916074737.58044.42776.julian@quantumfyre.co.uk>

These are a set of helper functions that use the generic hash table
functions to provide a quick and easy mapping from ref name to sha1.

Signed-off-by: Julian Phillips <julian@quantumfyre.co.uk>
---
 Makefile   |    1 +
 ref-dict.c |   76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ref-dict.h |   13 ++++++++++
 3 files changed, 90 insertions(+), 0 deletions(-)
 create mode 100644 ref-dict.c
 create mode 100644 ref-dict.h

diff --git a/Makefile b/Makefile
index d4958b8..815e4c9 100644
--- a/Makefile
+++ b/Makefile
@@ -531,6 +531,7 @@ LIB_OBJS += reachable.o
 LIB_OBJS += read-cache.o
 LIB_OBJS += reflog-walk.o
 LIB_OBJS += refs.o
+LIB_OBJS += ref-dict.o
 LIB_OBJS += remote.o
 LIB_OBJS += replace_object.o
 LIB_OBJS += rerere.o
diff --git a/ref-dict.c b/ref-dict.c
new file mode 100644
index 0000000..b9cab4b
--- /dev/null
+++ b/ref-dict.c
@@ -0,0 +1,76 @@
+/*
+ * ref-dict.c
+ *
+ * A Hash-based dictionary for storing name-sha1 data for refs.
+ *
+ * Copyright (C) 2009 Julian Phillips
+ */
+
+#include "cache.h"
+#include "hash.h"
+#include "remote.h"
+#include "ref-dict.h"
+
+/* hash_name based on the function of the same name in name-hash.c */
+static unsigned int hash_name(const char *name)
+{
+	unsigned int hash = 0x123;
+	int namelen = strlen(name);
+
+	do {
+		unsigned char c = *name++;
+		hash = hash*101 + c;
+	} while (--namelen);
+	return hash;
+}
+
+/*
+ * A convienience function for creating a ref_dict from a ref_list.
+ */
+void ref_dict_create(struct hash_table *dict, const struct ref *ref_list)
+{
+	struct ref *ref;
+
+	init_hash(dict);
+
+	for (ref = (struct ref *)ref_list; ref; ref = ref->next) {
+		ref_dict_add(dict, ref->name, ref->old_sha1);
+	}
+}
+
+/*
+ * Add an entry to the ref_dict, recording that name maps to sha1.
+ */
+void ref_dict_add(struct hash_table *dict, const char *name,
+		const unsigned char *sha1)
+{
+	struct ref **ref;
+	struct ref *new_ref = alloc_ref(name);
+
+	hashcpy(new_ref->old_sha1, sha1);
+	
+	ref = (struct ref **)insert_hash(hash_name(name), new_ref, dict);
+	if (ref) {
+		new_ref->next = *ref;
+		*ref = new_ref;
+	}
+}
+
+/*
+ * Find the sha1 for the given name.  Returns 1 if found and copies the sha1
+ * into the space pointed to by sha1, returns 0 otherwise and sha1 is untouched.
+ */
+int ref_dict_get(const struct hash_table *dict, const char *name,
+		unsigned char *sha1)
+{
+	struct ref *ref = lookup_hash(hash_name(name), dict);
+
+	for (; ref; ref = ref->next) {
+		if (!strcmp(name, ref->name)) {
+			hashcpy(sha1, ref->old_sha1);
+			return 1;
+		}
+	}
+
+	return 0;
+}
diff --git a/ref-dict.h b/ref-dict.h
new file mode 100644
index 0000000..ca1e9a7
--- /dev/null
+++ b/ref-dict.h
@@ -0,0 +1,13 @@
+#ifndef REF_DICT_H
+#define REF_DICT_H
+
+#include "cache.h"
+#include "hash.h"
+
+void ref_dict_create(struct hash_table *dict, const struct ref *ref_list);
+void ref_dict_add(struct hash_table *dict, const char *name,
+		const unsigned char *sha1);
+int ref_dict_get(const struct hash_table *dict, const char *name,
+		unsigned char *sha1);
+
+#endif /* REF_DICT_H */
-- 
1.6.4.2

  reply	other threads:[~2009-09-16  7:58 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-16  7:53 [RFC/PATCH 0/2] Speed up fetch with large number of tags Julian Phillips
2009-09-16  7:53 ` Julian Phillips [this message]
2009-09-16  7:53 ` [RFC/PATCH 2/2] fetch: Speed up fetch by using ref dictionary Julian Phillips
2009-09-16  9:44 ` [RFC/PATCH 0/2] Speed up fetch with large number of tags Junio C Hamano
2009-09-16 22:32   ` Julian Phillips
2009-09-16 22:42     ` Shawn O. Pearce
2009-09-16 22:52       ` Junio C Hamano
2009-09-16 23:03         ` Shawn O. Pearce
2009-09-16 23:19           ` Junio C Hamano
2009-09-16 22:53     ` [RFC/PATCH v2] fetch: Speed up fetch by rewriting find_non_local_tags Julian Phillips
2009-09-16 23:15       ` Junio C Hamano
2009-09-16 23:46         ` Julian Phillips
2009-09-17  1:30           ` Julian Phillips
2009-09-17  7:13             ` Johan Herland
2009-09-17  7:33               ` [RFC/PATCH v3] " Julian Phillips
2009-09-16 22:46   ` [RFC/PATCH 0/2] Speed up fetch with large number of tags Shawn O. Pearce
2009-09-22 20:36     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090916075304.58044.664.julian@quantumfyre.co.uk \
    --to=julian@quantumfyre.co.uk \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).