This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository efl.

View the commit online.

commit 347aa9992e3d1ad011ce7c2cd6357518927d8693
Author: Cedric BAIL <[email protected]>
AuthorDate: Fri Nov 6 00:05:47 2026 -0600

    ecore: a boolean model keeps its marks aligned on insert
    
    Removals shifted the stored bits down but an inserted child left them
    where they were, so every mark after the insertion point ended up on the
    wrong child. Shift them up on child added and give the new child the
    default value.
    
    While there, fill newly allocated buffers with 0xFF for a true default:
    memset with 1 only set the first bit of each byte.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 src/lib/ecore/efl_boolean_model.c        | 110 ++++++++++++++++++++++-----
 src/tests/efl/efl_test_composite_model.c | 125 +++++++++++++++++++++++++++++++
 2 files changed, 216 insertions(+), 19 deletions(-)

diff --git a/src/lib/ecore/efl_boolean_model.c b/src/lib/ecore/efl_boolean_model.c
index 47f66e1d3d..da0ab6093f 100644
--- a/src/lib/ecore/efl_boolean_model.c
+++ b/src/lib/ecore/efl_boolean_model.c
@@ -64,6 +64,32 @@ _storage_range_free(Eina_Rbtree *node, void *data EINA_UNUSED)
    free(node);
 }
 
+static Efl_Boolean_Model_Storage_Range *
+_storage_allocate(Efl_Boolean_Model_Value *v, unsigned int index, Eina_Bool default_value)
+{
+   Efl_Boolean_Model_Storage_Range *lookup = NULL;
+
+   // For simplicity we do not create a sparse array, every boolean potentially needed are allocated
+   // FIXME: keep it a sparse allocated buffer and only allocate needed buffer on demand
+   do
+     {
+        lookup = calloc(1, sizeof (Efl_Boolean_Model_Storage_Range));
+        if (!lookup) return NULL;
+
+        lookup->offset = v->last ? v->last->offset + v->last->length + 1 : 0;
+        lookup->length = sizeof (lookup->buffer) * 8; // Number of bits in the buffer
+        // Initialize the buffer to the right default value
+        memset(&lookup->buffer[0], default_value ? 0xFF : 0x00, sizeof (lookup->buffer));
+
+        v->buffers_root = eina_rbtree_inline_insert(v->buffers_root, EINA_RBTREE_GET(lookup),
+                                                    EINA_RBTREE_CMP_NODE_CB(_storage_range_cmp), NULL);
+        v->last = lookup;
+     }
+   while (v->last->offset + v->last->length < index);
+
+   return lookup;
+}
+
 static Efl_Boolean_Model_Storage_Range *
 _storage_lookup(Efl_Boolean_Model_Data *pd,
                 const char *property,
@@ -95,25 +121,7 @@ _storage_lookup(Efl_Boolean_Model_Data *pd,
    // The value is the same as the default value, why bother allocate
    if (*default_value == value) return NULL;
 
-   // For simplicity we do not create a sparse array, every boolean potentially needed are allocated
-   // FIXME: keep it a sparse allocated buffer and only allocate needed buffer on demand
-   do
-     {
-        lookup = calloc(1, sizeof (Efl_Boolean_Model_Storage_Range));
-        if (!lookup) return NULL;
-
-        lookup->offset = v->last ? v->last->offset + v->last->length + 1 : 0;
-        lookup->length = sizeof (lookup->buffer) * 8; // Number of bits in the buffer
-        // Initialize the buffer to the right default value
-        memset(&lookup->buffer[0], *default_value, sizeof (lookup->buffer));
-
-        v->buffers_root = eina_rbtree_inline_insert(v->buffers_root, EINA_RBTREE_GET(lookup),
-                                                    EINA_RBTREE_CMP_NODE_CB(_storage_range_cmp), NULL);
-        v->last = lookup;
-     }
-   while (v->last->offset + v->last->length < index);
-
-   return lookup;
+   return _storage_allocate(v, index, *default_value);
 }
 
 static Eina_Iterator *
@@ -336,6 +344,69 @@ _child_removed(void *data, const Efl_Event *event)
    eina_array_flush(&updated);
 }
 
+static Eina_Bool
+_value_bit_get(Efl_Boolean_Model_Value *v, unsigned int index)
+{
+   Efl_Boolean_Model_Storage_Range *sr;
+   unsigned int offset;
+
+   sr = (void*) eina_rbtree_inline_lookup(v->buffers_root, &index, sizeof (unsigned int),
+                                          EINA_RBTREE_CMP_KEY_CB(_storage_range_key), NULL);
+   if (!sr) return !!v->default_value;
+
+   offset = index - sr->offset;
+   return !!(sr->buffer[offset >> 3] & (((unsigned char)1) << (offset & 0x7)));
+}
+
+static void
+_value_bit_set(Efl_Boolean_Model_Value *v, unsigned int index, Eina_Bool flag)
+{
+   Efl_Boolean_Model_Storage_Range *sr;
+   unsigned int offset;
+
+   sr = (void*) eina_rbtree_inline_lookup(v->buffers_root, &index, sizeof (unsigned int),
+                                          EINA_RBTREE_CMP_KEY_CB(_storage_range_key), NULL);
+   if (!sr)
+     {
+        // Not stored means default, nothing to do when that is what is wanted
+        if (!!v->default_value == !!flag) return;
+        sr = _storage_allocate(v, index, v->default_value);
+        if (!sr) return;
+     }
+
+   offset = index - sr->offset;
+   if (flag) sr->buffer[offset >> 3] |= ((unsigned char)1) << (offset & 0x7);
+   else sr->buffer[offset >> 3] &= ~(((unsigned char)1) << (offset & 0x7));
+}
+
+static void
+_child_added(void *data, const Efl_Event *event)
+{
+   Efl_Boolean_Model_Data *pd = data;
+   Efl_Model_Children_Event *ev = event->info;
+   Efl_Boolean_Model_Value *v;
+   Eina_Iterator *it;
+   unsigned int total;
+
+   if (!pd->values) return;
+
+   // The child is already counted, so the last index is the new end
+   total = efl_model_children_count_get(event->object);
+   if (!total) return;
+
+   it = eina_hash_iterator_data_new(pd->values);
+   EINA_ITERATOR_FOREACH(it, v)
+     {
+        unsigned int i;
+
+        // Everything from the insertion point is now one child further
+        for (i = total - 1; i > ev->index; i--)
+          _value_bit_set(v, i, _value_bit_get(v, i - 1));
+        _value_bit_set(v, ev->index, !!v->default_value);
+     }
+   eina_iterator_free(it);
+}
+
 static Eo *
 _efl_boolean_model_efl_object_constructor(Eo *obj, Efl_Boolean_Model_Data *pd)
 {
@@ -352,6 +423,7 @@ _efl_boolean_model_efl_object_constructor(Eo *obj, Efl_Boolean_Model_Data *pd)
      pd->parent = efl_data_scope_get(parent, EFL_BOOLEAN_MODEL_CLASS);
 
    efl_event_callback_add(obj, EFL_MODEL_EVENT_CHILD_REMOVED, _child_removed, pd);
+   efl_event_callback_add(obj, EFL_MODEL_EVENT_CHILD_ADDED, _child_added, pd);
 
    return obj;
 }
diff --git a/src/tests/efl/efl_test_composite_model.c b/src/tests/efl/efl_test_composite_model.c
index a6af00bf05..e10256b7e2 100644
--- a/src/tests/efl/efl_test_composite_model.c
+++ b/src/tests/efl/efl_test_composite_model.c
@@ -578,10 +578,135 @@ EFL_START_TEST(efl_test_generic_model_child_added)
 }
 EFL_END_TEST
 
+static Eina_Bool
+_marked_get(Eo *child)
+{
+   Eina_Value *r = efl_model_property_get(child, "marked");
+   Eina_Bool b = EINA_FALSE;
+
+   ck_assert(eina_value_bool_convert(r, &b));
+   eina_value_free(r);
+   return b;
+}
+
+static Eina_Value
+_insert_check(Eo *model, void *data EINA_UNUSED, const Eina_Value v)
+{
+   Efl_Model_Children_Event ev = { 0 };
+   Eo *base = efl_ui_view_model_get(model);
+   Eo *kids[5] = { NULL };
+   unsigned int i, len;
+   Eo *c;
+
+   EINA_VALUE_ARRAY_FOREACH(&v, len, i, c)
+     {
+        ck_assert_int_lt(i, 5);
+        kids[i] = c;
+     }
+   ck_assert_int_eq(len, 5);
+
+   efl_model_property_set(kids[2], "marked", eina_value_bool_new(EINA_TRUE));
+   ck_assert(_marked_get(kids[2]));
+
+   ev.index = 0;
+   efl_event_callback_call(base, EFL_MODEL_EVENT_CHILD_ADDED, &ev);
+
+   ck_assert_int_eq(efl_composite_model_index_get(kids[2]), 3);
+   // The old child 2 is now child 3 and keeps its mark, whatever now sits at 2 does not
+   ck_assert(_marked_get(kids[2]));
+   ck_assert_int_eq(efl_composite_model_index_get(kids[1]), 2);
+   ck_assert(!_marked_get(kids[1]));
+
+   ecore_main_loop_quit();
+   return v;
+}
+
+/* A child inserted in front of the others pushes them one index further, and
+ * the marks go with the children rather than staying at their index.
+ * Efl.Generic_Model only appends, so the insertion is announced the way a
+ * model that can insert does it. */
+EFL_START_TEST(efl_test_boolean_model_insert)
+{
+   Efl_Generic_Model *base_model;
+   Efl_Boolean_Model *model;
+   Eina_Future *future;
+   int i;
+
+   base_model = efl_add_ref(EFL_GENERIC_MODEL_CLASS, efl_main_loop_get());
+   for (i = 0; i < 5; i++)
+     {
+        ck_assert(!!efl_model_child_add(base_model));
+     }
+
+   model = efl_add_ref(EFL_BOOLEAN_MODEL_CLASS, efl_main_loop_get(),
+                       efl_ui_view_model_set(efl_added, base_model),
+                       efl_boolean_model_boolean_add(efl_added, "marked", EINA_FALSE));
+   ck_assert(!!model);
+
+   future = efl_model_children_slice_get(model, 0, 5);
+   efl_future_then(model, future, .success_type = EINA_VALUE_TYPE_ARRAY,
+                   .success = _insert_check);
+
+   ecore_main_loop_begin();
+
+   efl_unref(model);
+   efl_unref(base_model);
+}
+EFL_END_TEST
+
+static Eina_Value
+_true_default_check(Eo *model EINA_UNUSED, void *data EINA_UNUSED, const Eina_Value v)
+{
+   unsigned int i, len;
+   Eo *c;
+
+   EINA_VALUE_ARRAY_FOREACH(&v, len, i, c)
+     {
+        // Changing the first child allocates the buffer the others share
+        if (!i) efl_model_property_set(c, "marked", eina_value_bool_new(EINA_FALSE));
+        ck_assert(_marked_get(c) == !!i);
+     }
+   ck_assert_int_eq(len, 10);
+
+   ecore_main_loop_quit();
+   return v;
+}
+
+/* A boolean that is true by default is true for every child sharing a buffer
+ * with one that was changed, not only for the first of each byte. */
+EFL_START_TEST(efl_test_boolean_model_true_default)
+{
+   Efl_Generic_Model *base_model;
+   Efl_Boolean_Model *model;
+   int i;
+
+   base_model = efl_add_ref(EFL_GENERIC_MODEL_CLASS, efl_main_loop_get());
+   for (i = 0; i < 10; i++)
+     {
+        ck_assert(!!efl_model_child_add(base_model));
+     }
+
+   model = efl_add_ref(EFL_BOOLEAN_MODEL_CLASS, efl_main_loop_get(),
+                       efl_ui_view_model_set(efl_added, base_model),
+                       efl_boolean_model_boolean_add(efl_added, "marked", EINA_TRUE));
+   ck_assert(!!model);
+
+   efl_future_then(model, efl_model_children_slice_get(model, 0, 10),
+                   .success_type = EINA_VALUE_TYPE_ARRAY,
+                   .success = _true_default_check);
+   ecore_main_loop_begin();
+
+   efl_unref(model);
+   efl_unref(base_model);
+}
+EFL_END_TEST
+
 void
 efl_test_case_boolean_model(TCase *tc)
 {
    tcase_add_test(tc, efl_test_boolean_model);
+   tcase_add_test(tc, efl_test_boolean_model_insert);
+   tcase_add_test(tc, efl_test_boolean_model_true_default);
    tcase_add_test(tc, efl_test_filter_model);
    tcase_add_test(tc, efl_test_generic_model_child_added);
    tcase_add_test(tc, efl_test_composite_model_source_properties_first);

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to