Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 11 Jul 2013 07:11:48 GMT
From:      mattbw@FreeBSD.org
To:        svn-soc-all@FreeBSD.org
Subject:   socsvn commit: r254608 - soc2013/mattbw/backend
Message-ID:  <201307110711.r6B7Bmrk012034@socsvn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: mattbw
Date: Thu Jul 11 07:11:48 2013
New Revision: 254608
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=254608

Log:
  use bsearch for finding group mapping

Modified:
  soc2013/mattbw/backend/group.c
  soc2013/mattbw/backend/group_map.awk
  soc2013/mattbw/backend/group_map.h

Modified: soc2013/mattbw/backend/group.c
==============================================================================
--- soc2013/mattbw/backend/group.c	Thu Jul 11 06:48:53 2013	(r254607)
+++ soc2013/mattbw/backend/group.c	Thu Jul 11 07:11:48 2013	(r254608)
@@ -32,18 +32,19 @@
 
 static const char ORIGIN_SEPARATOR = '/';
 
+static int	map_compare(const void *key, const void *mapping);
 static PkGroupEnum group_from_port_dir(const char *port_dir);
 
 /* Reports the PackageKit groups available on this backend as a bitfield. */
 PkBitfield
 group_bitfield(void)
 {
-	const struct group_mapping *map;
+	size_t		i;
 	PkBitfield	bits;
 
 	bits = 0;
-	for (map = group_mappings; map->key[0] != '\0'; map++)
-		pk_bitfield_add(bits, map->group);
+	for (i = 0; i < num_group_mappings; i++)
+		pk_bitfield_add(bits, group_mappings[i].group);
 
 	return bits;
 }
@@ -79,13 +80,13 @@
 }
 
 /*
- * Maps from packages to PackageKit groups.  PKG_LOAD_CATEGORIES must have been
- * set whilst loading the package for this to work properly.
+ * Maps from packages to PackageKit groups.  PKG_LOAD_CATEGORIES must have
+ * been set whilst loading the package for this to work properly.
  */
 PkGroupEnum
 group_of_pkg(struct pkg *pkg)
 {
-	PkGroupEnum group;
+	PkGroupEnum	group;
 	struct pkg_category *category;
 
 	assert(pkg != NULL);
@@ -100,11 +101,10 @@
 
 		group = group_from_port_dir(pkg_category_name(category));
 	}
-
-	/* Fallback to checking the origin if category mapping failed. */
+	/* Fall back to checking the origin if category mapping failed. */
 	if (group == PK_GROUP_ENUM_UNKNOWN) {
-		const char *origin;
-	
+		const char     *origin;
+
 		origin = NULL;
 		if (pkg_get(pkg, PKG_ORIGIN, &origin) == EPKG_OK) {
 			assert(origin != NULL);
@@ -112,7 +112,6 @@
 			group = group_from_origin(origin);
 		}
 	}
-
 	return group;
 }
 
@@ -168,6 +167,13 @@
 	return regex;
 }
 
+static int
+map_compare(const void *key, const void *mapping)
+{
+	return strcmp((const char *)key,
+	    ((const struct group_mapping *)mapping)->key);
+}
+
 /*
  * Maps from port/origin directories to PackageKit groups.
  */
@@ -178,9 +184,10 @@
 
 	assert(port_dir != NULL);
 
-	for (result = group_mappings;
-	    result->key[0] != '\0' && strcmp(result->key, port_dir) != 0;
-	    result++);
-
-	return result->group;
+	result = bsearch(port_dir,
+	    group_mappings,
+	    num_group_mappings,
+	    sizeof(struct group_mapping),
+	    map_compare);
+	return (result == NULL ? PK_GROUP_ENUM_UNKNOWN : result->group);
 }

Modified: soc2013/mattbw/backend/group_map.awk
==============================================================================
--- soc2013/mattbw/backend/group_map.awk	Thu Jul 11 06:48:53 2013	(r254607)
+++ soc2013/mattbw/backend/group_map.awk	Thu Jul 11 07:11:48 2013	(r254608)
@@ -3,15 +3,16 @@
 	print "/* Auto-generated, do not edit directly.  Edit 'groups' instead */"
 	print "#include \"group_map.h\""
 	print "const struct group_mapping group_mappings[] = {"
+	COUNT = 0;
 }
 {
 	sub("#.*$", "", $0);
 	if (length($0) > 0) {
 		print "\t{\"" $1 "\", " $2 "},";
-		MAP[tolower($1)] = toupper($2);
+		COUNT++;
 	}
 }
 END {
-	print "\t{\"\", PK_GROUP_ENUM_UNKNOWN}";
 	print "};";
+	print "const size_t num_group_mappings = " COUNT ";";
 }

Modified: soc2013/mattbw/backend/group_map.h
==============================================================================
--- soc2013/mattbw/backend/group_map.h	Thu Jul 11 06:48:53 2013	(r254607)
+++ soc2013/mattbw/backend/group_map.h	Thu Jul 11 07:11:48 2013	(r254608)
@@ -29,5 +29,6 @@
 };
 
 extern const struct group_mapping group_mappings[];
+extern const size_t num_group_mappings;
 
 #endif				/* _PKGNG_BACKEND_GROUP_MAP_H_ */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201307110711.r6B7Bmrk012034>