]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/idxset.c
namereg: choose default sink/source as soon as one becomes available
[pulseaudio] / src / pulsecore / idxset.c
index 2de64069924bfae37a2eedfe36e3616d7de97faa..408011f6b4974ef1cb1f306a86d5069277d2bb48 100644 (file)
@@ -80,7 +80,7 @@ unsigned pa_idxset_trivial_hash_func(const void *p) {
 }
 
 int pa_idxset_trivial_compare_func(const void *a, const void *b) {
-    return a != b;
+    return a < b ? -1 : (a > b ? 1 : 0);
 }
 
 pa_idxset* pa_idxset_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func) {
@@ -386,8 +386,11 @@ void* pa_idxset_steal_first(pa_idxset *s, uint32_t *idx) {
 void* pa_idxset_first(pa_idxset *s, uint32_t *idx) {
     pa_assert(s);
 
-    if (!s->iterate_list_head)
+    if (!s->iterate_list_head) {
+        if (idx)
+            *idx = PA_IDXSET_INVALID;
         return NULL;
+    }
 
     if (idx)
         *idx = s->iterate_list_head->idx;
@@ -402,20 +405,41 @@ void *pa_idxset_next(pa_idxset *s, uint32_t *idx) {
     pa_assert(s);
     pa_assert(idx);
 
+    if (*idx == PA_IDXSET_INVALID)
+        return NULL;
+
     hash = *idx % NBUCKETS;
 
-    if (!(e = index_scan(s, hash, *idx)))
-        return NULL;
+    if ((e = index_scan(s, hash, *idx))) {
+
+        e = e->iterate_next;
+
+        if (e) {
+            *idx = e->idx;
+            return e->data;
+        } else {
+            *idx = PA_IDXSET_INVALID;
+            return NULL;
+        }
+
+    } else {
+
+        /* If the entry passed doesn't exist anymore we try to find
+         * the next following */
+
+        for ((*idx)++; *idx < s->current_index; (*idx)++) {
+
+            hash = *idx % NBUCKETS;
+
+            if ((e = index_scan(s, hash, *idx))) {
+                *idx = e->idx;
+                return e->data;
+            }
+        }
 
-    if (!e->iterate_next) {
         *idx = PA_IDXSET_INVALID;
         return NULL;
     }
-
-    e = e->iterate_next;
-
-    *idx = e->idx;
-    return e->data;
 }
 
 unsigned pa_idxset_size(pa_idxset*s) {
@@ -429,3 +453,17 @@ pa_bool_t pa_idxset_isempty(pa_idxset *s) {
 
     return s->n_entries == 0;
 }
+
+pa_idxset *pa_idxset_copy(pa_idxset *s) {
+    pa_idxset *copy;
+    struct idxset_entry *i;
+
+    pa_assert(s);
+
+    copy = pa_idxset_new(s->hash_func, s->compare_func);
+
+    for (i = s->iterate_list_head; i; i = i->iterate_next)
+        pa_idxset_put(copy, i->data, NULL);
+
+    return copy;
+}