Author: sandervanderburg
Date: Thu Feb 24 13:41:16 2011
New Revision: 26092
URL: https://svn.nixos.org/websvn/nix/?rev=26092&sc=1

Log:
Reducing technical debt: do lookups in one function instead of using two 
function calls

Modified:
   disnix/dydisnix/trunk/src/divide/divide.c
   disnix/dydisnix/trunk/src/libinfproperties/infrastructureproperties.c
   disnix/dydisnix/trunk/src/libinfproperties/infrastructureproperties.h
   disnix/dydisnix/trunk/src/libmapping/candidatetargetmapping.c
   disnix/dydisnix/trunk/src/libmapping/candidatetargetmapping.h
   disnix/dydisnix/trunk/src/libservices/serviceproperties.c
   disnix/dydisnix/trunk/src/libservices/serviceproperties.h
   disnix/dydisnix/trunk/src/minsetcover/minsetcover.c

Modified: disnix/dydisnix/trunk/src/divide/divide.c
==============================================================================
--- disnix/dydisnix/trunk/src/divide/divide.c   Thu Feb 24 13:36:18 2011        
(r26091)
+++ disnix/dydisnix/trunk/src/divide/divide.c   Thu Feb 24 13:41:16 2011        
(r26092)
@@ -38,12 +38,9 @@
        for(i = 0; i < candidate_target_array->len; i++)
        {
            DistributionItem *item = g_array_index(candidate_target_array, 
DistributionItem*, i);
-           gint s_index = service_index(service_property_array, item->service);
-           Service *service = g_array_index(service_property_array, Service*, 
s_index);
+           Service *service = lookup_service(service_property_array, 
item->service);
+           ServiceProperty *service_prop = lookup_service_property(service, 
service_property);
 
-           gint service_prop_index = service_property_index(service, 
service_property);
-           ServiceProperty *service_prop = g_array_index(service->property, 
ServiceProperty*, service_prop_index);
-       
            GArray *targets = item->targets;
            unsigned int j;
        
@@ -58,11 +55,8 @@
            for(j = 0; j < targets->len; j++)
            {
                gchar *target_name = g_array_index(targets, gchar*, j);
-               gint i_index = 
infrastructure_index(infrastructure_property_array, target_name);
-               Target *target = g_array_index(infrastructure_property_array, 
Target*, i_index);
-           
-               gint infrastructure_prop_index = 
infrastructure_property_index(target, infrastructure_property);
-               InfrastructureProperty *infrastructure_prop = 
g_array_index(target->property, InfrastructureProperty*, 
infrastructure_prop_index);
+               Target *target = lookup_target(infrastructure_property_array, 
target_name);         
+               InfrastructureProperty *infrastructure_prop = 
lookup_infrastructure_property(target, infrastructure_property);
            
                if(strategy == STRATEGY_GREEDY)
                {
@@ -82,8 +76,7 @@
                    }
                    else
                    {
-                       gint select_infrastructure_prop_index = 
infrastructure_property_index(select_target, infrastructure_property);
-                       InfrastructureProperty *select_infrastructure_prop = 
g_array_index(select_target->property, InfrastructureProperty*, 
select_infrastructure_prop_index);
+                       InfrastructureProperty *select_infrastructure_prop = 
lookup_infrastructure_property(select_target, infrastructure_property);
                    
                        if(atoi(infrastructure_prop->value) > 
atoi(select_infrastructure_prop->value))
                            select_target = target;
@@ -98,9 +91,8 @@
                    }
                    else
                    {
-                       gint select_infrastructure_prop_index = 
infrastructure_property_index(select_target, infrastructure_property);
-                       InfrastructureProperty *select_infrastructure_prop = 
g_array_index(select_target->property, InfrastructureProperty*, 
select_infrastructure_prop_index);
-                   
+                       InfrastructureProperty *select_infrastructure_prop = 
lookup_infrastructure_property(select_target, infrastructure_property);
+                       
                        if(atoi(infrastructure_prop->value) < 
atoi(select_infrastructure_prop->value) && atoi(service_prop->value) <= 
atoi(select_infrastructure_prop->value))
                            select_target = target;
                    }

Modified: disnix/dydisnix/trunk/src/libinfproperties/infrastructureproperties.c
==============================================================================
--- disnix/dydisnix/trunk/src/libinfproperties/infrastructureproperties.c       
Thu Feb 24 13:36:18 2011        (r26091)
+++ disnix/dydisnix/trunk/src/libinfproperties/infrastructureproperties.c       
Thu Feb 24 13:41:16 2011        (r26092)
@@ -147,7 +147,7 @@
     }
 }
 
-gint infrastructure_index(GArray *infrastructure_property_array, gchar *name)
+static gint infrastructure_index(GArray *infrastructure_property_array, gchar 
*name)
 {
     gint left = 0;
     gint right = infrastructure_property_array->len - 1;
@@ -169,7 +169,17 @@
     return -1; /* Target not found */
 }
 
-gint infrastructure_property_index(Target *target, gchar *name)
+Target* lookup_target(GArray *infrastructure_property_array, gchar *name)
+{
+    gint index = infrastructure_index(infrastructure_property_array, name);
+    
+    if(index == -1)
+       return NULL;
+    else
+       return g_array_index(infrastructure_property_array, Target*, index);
+}
+
+static gint infrastructure_property_index(Target *target, gchar *name)
 {
     GArray *property = target->property;
     
@@ -193,6 +203,16 @@
     return -1; /* Target property not found */
 }
 
+InfrastructureProperty *lookup_infrastructure_property(Target *target, gchar 
*name)
+{
+    gint index = infrastructure_property_index(target, name);
+    
+    if(index == -1)
+       return NULL;
+    else
+       return g_array_index(target->property, InfrastructureProperty*, index);
+}
+
 void substract_target_value(Target *target, gchar *property_name, int amount)
 {
     gchar buffer[BUFFER_SIZE];

Modified: disnix/dydisnix/trunk/src/libinfproperties/infrastructureproperties.h
==============================================================================
--- disnix/dydisnix/trunk/src/libinfproperties/infrastructureproperties.h       
Thu Feb 24 13:36:18 2011        (r26091)
+++ disnix/dydisnix/trunk/src/libinfproperties/infrastructureproperties.h       
Thu Feb 24 13:41:16 2011        (r26092)
@@ -24,10 +24,10 @@
 
 void print_infrastructure_property_array(GArray 
*infrastructure_property_array);
 
-gint infrastructure_index(GArray *infrastructure_property_array, gchar *name);
+void substract_target_value(Target *target, gchar *property_name, int amount);
 
-gint infrastructure_property_index(Target *target, gchar *name);
+Target* lookup_target(GArray *infrastructure_property_array, gchar *name);
 
-void substract_target_value(Target *target, gchar *property_name, int amount);
+InfrastructureProperty *lookup_infrastructure_property(Target *target, gchar 
*name);
 
 #endif

Modified: disnix/dydisnix/trunk/src/libmapping/candidatetargetmapping.c
==============================================================================
--- disnix/dydisnix/trunk/src/libmapping/candidatetargetmapping.c       Thu Feb 
24 13:36:18 2011        (r26091)
+++ disnix/dydisnix/trunk/src/libmapping/candidatetargetmapping.c       Thu Feb 
24 13:41:16 2011        (r26092)
@@ -168,7 +168,7 @@
     g_print("}\n");
 }
 
-int distribution_item_index(GArray *candidate_target_array, gchar *service)
+gint distribution_item_index(GArray *candidate_target_array, gchar *service)
 {
     gint left = 0;
     gint right = candidate_target_array->len - 1;
@@ -189,3 +189,13 @@
     
     return -1; /* service not found */
 }
+
+DistributionItem *lookup_distribution_item(GArray *candidate_target_array, 
gchar *service)
+{
+    gint index = distribution_item_index(candidate_target_array, service);
+    
+    if(index == -1)
+       return NULL;
+    else
+       return g_array_index(candidate_target_array, DistributionItem*, index);
+}

Modified: disnix/dydisnix/trunk/src/libmapping/candidatetargetmapping.h
==============================================================================
--- disnix/dydisnix/trunk/src/libmapping/candidatetargetmapping.h       Thu Feb 
24 13:36:18 2011        (r26091)
+++ disnix/dydisnix/trunk/src/libmapping/candidatetargetmapping.h       Thu Feb 
24 13:41:16 2011        (r26092)
@@ -22,6 +22,8 @@
 
 void print_expr_of_candidate_target_array(GArray *candidate_target_array);
 
-int distribution_item_index(GArray *candidate_target_array, gchar *service);
+gint distribution_item_index(GArray *candidate_target_array, gchar *service);
+
+DistributionItem *lookup_distribution_item(GArray *candidate_target_array, 
gchar *service);
 
 #endif

Modified: disnix/dydisnix/trunk/src/libservices/serviceproperties.c
==============================================================================
--- disnix/dydisnix/trunk/src/libservices/serviceproperties.c   Thu Feb 24 
13:36:18 2011        (r26091)
+++ disnix/dydisnix/trunk/src/libservices/serviceproperties.c   Thu Feb 24 
13:41:16 2011        (r26092)
@@ -144,7 +144,7 @@
     }
 }
 
-gint service_index(GArray *service_property_array, gchar *name)
+static gint service_index(GArray *service_property_array, gchar *name)
 {
     gint left = 0;
     gint right = service_property_array->len - 1;
@@ -166,7 +166,17 @@
     return -1; /* Service not found */
 }
 
-gint service_property_index(Service *service, gchar *name)
+Service *lookup_service(GArray *service_property_array, gchar *name)
+{
+    gint index = service_index(service_property_array, name);
+    
+    if(index == -1)
+       return NULL;
+    else
+       return g_array_index(service_property_array, Service*, index);  
+}
+
+static gint service_property_index(Service *service, gchar *name)
 {
     GArray *property = service->property;
     
@@ -189,3 +199,13 @@
     
     return -1; /* Service property not found */
 }
+
+ServiceProperty *lookup_service_property(Service *service, gchar *name)
+{
+    gint index = service_property_index(service, name);
+    
+    if(index == -1)
+       return NULL;
+    else
+       return g_array_index(service->property, ServiceProperty*, index);
+}

Modified: disnix/dydisnix/trunk/src/libservices/serviceproperties.h
==============================================================================
--- disnix/dydisnix/trunk/src/libservices/serviceproperties.h   Thu Feb 24 
13:36:18 2011        (r26091)
+++ disnix/dydisnix/trunk/src/libservices/serviceproperties.h   Thu Feb 24 
13:41:16 2011        (r26092)
@@ -24,8 +24,8 @@
 
 void print_service_property_array(GArray *service_property_array);
 
-gint service_index(GArray *service_property_array, gchar *name);
+Service *lookup_service(GArray *service_property_array, gchar *name);
 
-gint service_property_index(Service *service, gchar *name);
+ServiceProperty *lookup_service_property(Service *service, gchar *name);
 
 #endif

Modified: disnix/dydisnix/trunk/src/minsetcover/minsetcover.c
==============================================================================
--- disnix/dydisnix/trunk/src/minsetcover/minsetcover.c Thu Feb 24 13:36:18 
2011        (r26091)
+++ disnix/dydisnix/trunk/src/minsetcover/minsetcover.c Thu Feb 24 13:41:16 
2011        (r26092)
@@ -47,10 +47,9 @@
            for(i = 0; i < target_mapping_array->len; i++)
            {
                TargetMappingItem *target_mapping = 
g_array_index(target_mapping_array, TargetMappingItem*, i);
-               int target_index = 
infrastructure_index(infrastructure_property_array, target_mapping->target);
-               Target *target = g_array_index(infrastructure_property_array, 
Target*, target_index);
-               int infrastructure_prop_index = 
infrastructure_property_index(target, target_property);
-               InfrastructureProperty *infrastructure_prop = 
g_array_index(target->property, InfrastructureProperty*, 
infrastructure_prop_index);
+               Target *target = lookup_target(infrastructure_property_array, 
target_mapping->target);
+               InfrastructureProperty *infrastructure_prop = 
lookup_infrastructure_property(target, target_property);
+               
                int count = 0;
                double cost;
                unsigned int j;
@@ -77,9 +76,8 @@
            for(i = 0; i < min_cost_target_mapping->services->len; i++)
            {
                gchar *service = 
g_array_index(min_cost_target_mapping->services, gchar*, i);
-               int index = distribution_item_index(result, service);
-               DistributionItem *item = g_array_index(result, 
DistributionItem*, index);
-           
+               DistributionItem *item = lookup_distribution_item(result, 
service);
+               
                if(g_hash_table_lookup(covered_services_table, service) == NULL)
                {
                    g_array_append_val(item->targets, 
min_cost_target_mapping->target);
_______________________________________________
nix-commits mailing list
[email protected]
http://mail.cs.uu.nl/mailman/listinfo/nix-commits

Reply via email to