aboutsummaryrefslogtreecommitdiff
path: root/libcontextprovider
diff options
context:
space:
mode:
Diffstat (limited to 'libcontextprovider')
-rw-r--r--libcontextprovider/intset.c71
-rw-r--r--libcontextprovider/intset.h4
-rw-r--r--libcontextprovider/intset.vapi4
3 files changed, 77 insertions, 2 deletions
diff --git a/libcontextprovider/intset.c b/libcontextprovider/intset.c
index 6b0e91e1..043b0785 100644
--- a/libcontextprovider/intset.c
+++ b/libcontextprovider/intset.c
@@ -226,7 +226,7 @@ context_provider_intset_is_subset_of (const ContextProviderIntSet *left, const C
guint offset;
gboolean ret = TRUE;
- if (left->size > right->size) {
+ if (right->size > left->size) {
return FALSE;
} else {
for (offset = 0; offset < right->size; offset++) {
@@ -290,6 +290,75 @@ context_provider_intset_foreach (const ContextProviderIntSet *set, ContextProvid
}
}
+
+static void
+addint (guint i, gpointer data)
+{
+ GArray *array = (GArray *) data;
+ g_array_append_val (array, i);
+}
+
+/**
+ * context_provider_intset_to_array:
+ * @set: set to convert
+ *
+ * <!--Returns: says it all-->
+ *
+ * Returns: a GArray of guint (which must be freed by the caller) containing
+ * the same integers as @set.
+ */
+GArray *
+context_provider_intset_to_array (const ContextProviderIntSet *set)
+{
+ GArray *array;
+
+ g_return_val_if_fail (set != NULL, NULL);
+
+ array = g_array_new (FALSE, TRUE, sizeof (guint));
+
+ context_provider_intset_foreach (set, addint, array);
+
+ return array;
+}
+
+
+/**
+ * context_provider_intset_from_array:
+ * @array: An array of guint
+ *
+ * <!--Returns: says it all-->
+ *
+ * Returns: A set containing the same integers as @array.
+ */
+
+ContextProviderIntSet *
+context_provider_intset_from_array (const GArray *array)
+{
+ ContextProviderIntSet *set;
+ guint max, i;
+
+ g_return_val_if_fail (array != NULL, NULL);
+
+ /* look at the 1st, last and middle values in the array to get an
+ * approximation of the largest */
+ max = 0;
+ if (array->len > 0)
+ max = g_array_index (array, guint, 0);
+ if (array->len > 1)
+ max = MAX (max, g_array_index (array, guint, array->len - 1));
+ if (array->len > 2)
+ max = MAX (max, g_array_index (array, guint, (array->len - 1) >> 1));
+ set = _context_provider_intset_new_with_size (1 + (max >> 5));
+
+ for (i = 0; i < array->len; i++)
+ {
+ context_provider_intset_add (set, g_array_index (array, guint, i));
+ }
+
+ return set;
+}
+
+
/**
* context_provider_intset_size:
* @set: A set of integers
diff --git a/libcontextprovider/intset.h b/libcontextprovider/intset.h
index e324952f..f9fbf074 100644
--- a/libcontextprovider/intset.h
+++ b/libcontextprovider/intset.h
@@ -52,11 +52,13 @@ void context_provider_intset_add (ContextProviderIntSet *set, guint element);
gboolean context_provider_intset_remove (ContextProviderIntSet *set, guint element);
gboolean context_provider_intset_is_member (const ContextProviderIntSet *set, guint element);
-gboolean context_provider_intset_is_subset_of (const ContextProviderIntSet *left, const ContextProviderIntSet *right);
+gboolean context_provider_intset_is_subset (const ContextProviderIntSet *left, const ContextProviderIntSet *right);
gboolean context_provider_intset_is_disjoint (const ContextProviderIntSet *left, const ContextProviderIntSet *right);
void context_provider_intset_foreach (const ContextProviderIntSet *set, ContextProviderIntFunc func,
gpointer userdata);
+GArray *context_provider_intset_to_array (const ContextProviderIntSet *set);
+ContextProviderIntSet *context_provider_intset_from_array (const GArray *array);
guint context_provider_intset_size (const ContextProviderIntSet *set);
diff --git a/libcontextprovider/intset.vapi b/libcontextprovider/intset.vapi
index 9dc3fbc5..f1ac044f 100644
--- a/libcontextprovider/intset.vapi
+++ b/libcontextprovider/intset.vapi
@@ -9,6 +9,8 @@ namespace ContextProvider {
public IntSet ();
[CCode (cname = "context_provider_intset_sized_new")]
public IntSet.sized (uint size);
+ [CCode (cname = "context_provider_intset_from_array")]
+ public IntSet.from_array (GLib.Array array);
[CCode (cname = "context_provider_intset_intersection")]
public IntSet.intersection (IntSet left, IntSet right);
[CCode (cname = "context_provider_intset_symmetric_difference")]
@@ -38,6 +40,8 @@ namespace ContextProvider {
public bool remove (uint element);
[CCode (cname = "context_provider_intset_size")]
public uint size ();
+ [CCode (cname = "context_provider_intset_to_array")]
+ public weak GLib.Array to_array ();
}
[CCode (cheader_filename = "intset.h", is_value_type="1")]
public struct IntSetIter {