Patch to allow GRelations > size 2




Hi all,

I've been playing around with GRelations of late - potentially quite 
useful, but completely hobbled by the size == 2 restriction.

I've attached a quick and dirty patch to glib-1.2.6 to allow relations 
of size 3 and 4; The hashing function is just the addition of the 
integer values of the pointers - crude but effective (or at least, more 
effective than not having a hashing function at all :-). I submit this 
unto the Powers That Be for rolling into CVS.

However, this is really just a stop-gap measure (in particular, an ); if 
glib provides for relations up to size n, someone will always want to 
use a relation of size n+1. 

It would seem to make sense (at least to me) to have a 
g_relation_new_full function, which allows the user to define and use 
their own hashing function, of a size appropriate for their application. 
If anyone else thinks this is a good idea, I will put together a patch.

Enjoy the patch
Russ Magee %-)

--------CUT HERE--------
diff -c -r glib-1.2.6/grel.c glib-1.2.6-grel-patch/grel.c
*** glib-1.2.6/grel.c   Thu Feb 17 14:00:20 2000
--- glib-1.2.6-grel-patch/grel.c        Thu Feb 17 13:59:00 2000
***************
*** 71,76 ****
--- 71,114 ----
    return (gulong)a[0] ^ (gulong)a[1];
  }
  
+ static gboolean
+ tuple_equal_3 (gconstpointer v_a,
+              gconstpointer v_b)
+ {
+   gpointer* a = (gpointer*) v_a;
+   gpointer* b = (gpointer*) v_b;
+   
+   return a[0] == b[0] && a[1] == b[1] && a[2] == b[2];
+ }
+ 
+ static guint
+ tuple_hash_3 (gconstpointer v_a)
+ {
+   gpointer* a = (gpointer*) v_a;
+   
+   return (gulong)a[0] + (gulong)a[1] + (gulong)a[2];
+ }
+ 
+ 
+ static gboolean
+ tuple_equal_4 (gconstpointer v_a,
+              gconstpointer v_b)
+ {
+   gpointer* a = (gpointer*) v_a;
+   gpointer* b = (gpointer*) v_b;
+   
+   return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3];
+ }
+ 
+ static guint
+ tuple_hash_4 (gconstpointer v_a)
+ {
+   gpointer* a = (gpointer*) v_a;
+   
+   return (gulong)a[0] + (gulong)a[1] + (gulong)a[2] + (gulong)a[3];
+ }
+ 
+ 
  static GHashFunc
  tuple_hash (gint fields)
  {
***************
*** 78,83 ****
--- 116,125 ----
      {
      case 2:
        return tuple_hash_2;
+     case 3:
+       return tuple_hash_3;
+     case 4:
+       return tuple_hash_4;
      default:
        g_error ("no tuple hash for %d", fields);
      }
***************
*** 92,97 ****
--- 134,143 ----
      {
      case 2:
        return tuple_equal_2;
+     case 3:
+       return tuple_equal_3;
+     case 4:
+       return tuple_equal_4;
      default:
        g_error ("no tuple equal for %d", fields);
      }
--------CUT HERE--------

-----------------------------------------------------------
Russell Keith-Magee
PhD Research Student
School of Computing, Curtin University of Technology
email: keithmag@cs.curtin.edu.au OR russ.magee@computer.org
WWW:   http://www.cs.curtin.edu.au/~keithmag
Ph: +61 8 9266 2129    FAX +61 8 9266 2819
-----------------------------------------------------------
I only know what I read in the papers.
		-- Will Rogers



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]