Ejemplo n.º 1
0
        /*
         * m_hash_table_foreach_remove_or_steal:
         * @hash_table: our #MHashTable
         * @func: the user's callback function
         * @user_data: data for @func
         * @notify: %true if the destroy notify handlers are to be called
         *
         * Implements the common logic for m_hash_table_foreach_remove() and
         * m_hash_table_foreach_steal().
         *
         * Iterates over every node in the table, calling @func with the key
         * and value of the node (and @user_data).  If @func returns %true the
         * node is removed from the table.
         *
         * If @notify is true then the destroy notify handlers will be called
         * for each removed node.
         */
        private uint ForeachRemoveOrSteal(MHRFunc func,
                                          IntPtr user_data,
                                          bool notify)
        {
            uint deleted = 0;

            for (int i = 0; i < size; i++)
            {
                MHashNode *node = &nodes [i];

                if (node->key_hash > 1 && func(node->key, node->value, user_data))
                {
                    RemoveNode(node, notify);
                    deleted++;
                }
            }

            MaybeResize();

            if (deleted > 0)
            {
                version++;
            }

            return(deleted);
        }
Ejemplo n.º 2
0
        /**
         * m_hash_table_foreach_steal:
         * @hash_table: a #MHashTable.
         * @func: the function to call for each key/value pair.
         * @user_data: user data to pass to the function.
         *
         * Calls the given function for each key/value pair in the #MHashTable.
         * If the function returns %true, then the key/value pair is removed from the
         * #MHashTable, but no key or value destroy functions are called.
         *
         * See #MHashTableIter for an alternative way to loop over the
         * key/value pairs in the hash table.
         *
         * Return value: the number of key/value pairs removed.
         **/
        public uint ForeachSteal(MHRFunc func,
                                 IntPtr user_data)
        {
            if (func == null)
            {
                throw new ArgumentNullException("func");
            }

            return(ForeachRemoveOrSteal(func, user_data, false));
        }
Ejemplo n.º 3
0
        /**
         * m_hash_table_find:
         * @hash_table: a #MHashTable.
         * @predicate:  function to test the key/value pairs for a certain property.
         * @user_data:  user data to pass to the function.
         *
         * Calls the given function for key/value pairs in the #MHashTable until
         * @predicate returns %true.  The function is passed the key and value of
         * each pair, and the given @user_data parameter. The hash table may not
         * be modified while iterating over it (you can't add/remove items).
         *
         * Note, that hash tables are really only optimized for forward lookups,
         * i.e. m_hash_table_lookup().
         * So code that frequently issues m_hash_table_find() or
         * m_hash_table_foreach() (e.g. in the order of once per every entry in a
         * hash table) should probably be reworked to use additional or different
         * data structures for reverse lookups (keep in mind that an O(n) find/foreach
         * operation issued for all n values in a hash table ends up needing O(n*n)
         * operations).
         *
         * Return value: The value of the first key/value pair is returned, for which
         * func evaluates to %true. If no pair with the requested property is found,
         * %null is returned.
         *
         * Since: 2.4
         **/
        public IntPtr Find(MHRFunc predicate,
                           IntPtr user_data)
        {
            if (predicate == null)
            {
                throw new ArgumentNullException("predicate");
            }

            for (int i = 0; i < size; i++)
            {
                MHashNode *node = &nodes [i];

                if (node->key_hash > 1 && predicate(node->key, node->value, user_data))
                {
                    return(node->value);
                }
            }

            return(IntPtr.Zero);
        }
Ejemplo n.º 4
0
Archivo: MHash.cs Proyecto: toshok/mlib
        /*
         * m_hash_table_foreach_remove_or_steal:
         * @hash_table: our #MHashTable
         * @func: the user's callback function
         * @user_data: data for @func
         * @notify: %true if the destroy notify handlers are to be called
         *
         * Implements the common logic for m_hash_table_foreach_remove() and
         * m_hash_table_foreach_steal().
         *
         * Iterates over every node in the table, calling @func with the key
         * and value of the node (and @user_data).  If @func returns %true the
         * node is removed from the table.
         *
         * If @notify is true then the destroy notify handlers will be called
         * for each removed node.
         */
        private uint ForeachRemoveOrSteal(MHRFunc	  func,
					   IntPtr    user_data,
					   bool    notify)
        {
            uint deleted = 0;

            for (int i = 0; i < size; i++) {
            MHashNode *node = &nodes [i];

            if (node->key_hash > 1 && func (node->key, node->value, user_data)) {
                RemoveNode (node, notify);
                deleted++;
            }
            }

            MaybeResize ();

            if (deleted > 0)
            version++;

            return deleted;
        }
Ejemplo n.º 5
0
Archivo: MHash.cs Proyecto: toshok/mlib
        /**
         * m_hash_table_foreach_steal:
         * @hash_table: a #MHashTable.
         * @func: the function to call for each key/value pair.
         * @user_data: user data to pass to the function.
         *
         * Calls the given function for each key/value pair in the #MHashTable.
         * If the function returns %true, then the key/value pair is removed from the
         * #MHashTable, but no key or value destroy functions are called.
         *
         * See #MHashTableIter for an alternative way to loop over the
         * key/value pairs in the hash table.
         *
         * Return value: the number of key/value pairs removed.
         **/
        public uint ForeachSteal(MHRFunc     func,
				  IntPtr    user_data)
        {
            if (func == null)
            throw new ArgumentNullException ("func");

            return ForeachRemoveOrSteal (func, user_data, false);
        }
Ejemplo n.º 6
0
Archivo: MHash.cs Proyecto: toshok/mlib
        /**
         * m_hash_table_find:
         * @hash_table: a #MHashTable.
         * @predicate:  function to test the key/value pairs for a certain property.
         * @user_data:  user data to pass to the function.
         *
         * Calls the given function for key/value pairs in the #MHashTable until
         * @predicate returns %true.  The function is passed the key and value of
         * each pair, and the given @user_data parameter. The hash table may not
         * be modified while iterating over it (you can't add/remove items).
         *
         * Note, that hash tables are really only optimized for forward lookups,
         * i.e. m_hash_table_lookup().
         * So code that frequently issues m_hash_table_find() or
         * m_hash_table_foreach() (e.g. in the order of once per every entry in a
         * hash table) should probably be reworked to use additional or different
         * data structures for reverse lookups (keep in mind that an O(n) find/foreach
         * operation issued for all n values in a hash table ends up needing O(n*n)
         * operations).
         *
         * Return value: The value of the first key/value pair is returned, for which
         * func evaluates to %true. If no pair with the requested property is found,
         * %null is returned.
         *
         * Since: 2.4
         **/
        public IntPtr Find(MHRFunc          predicate,
			    IntPtr         user_data)
        {
            if (predicate == null)
            throw new ArgumentNullException ("predicate");

            for (int i = 0; i < size; i++) {
            MHashNode *node = &nodes [i];

            if (node->key_hash > 1 && predicate (node->key, node->value, user_data))
                return node->value;
            }

            return IntPtr.Zero;
        }