add


Description:

public bool add (owned T value)

This is a convenience function for using a GenericSet as a set.

It is equivalent to calling replace with key as both the key and the value.

In particular, this means that if key already exists in the hash table, then the old copy of key in the hash table is freed and key replaces it in the table.

When a hash table only ever contains keys that have themselves as the corresponding value it is able to be stored more efficiently. See the discussion in the section description.

Starting from GLib 2.40, this function returns a boolean value to indicate whether the newly added value was already in the hash table or not.

Example: Add a element to the set:

public static int main (string[] args) {
GenericSet<string> table = new GenericSet<string> (str_hash, str_equal);

string keyval1 = "first string";
string keyval2 = "second string";
string keyval3 = "third string";
string keyval4 = "third string";

// Use (owned) to avoid unnecessary copies:
table.add ((owned) keyval1);
table.add ((owned) keyval2);
table.add ((owned) keyval3);
table.add ((owned) keyval4);


// Fields are owned by GenericSet:
assert (keyval1 == null && keyval2 == null && keyval3 == null);

// Output:
// ``second string => 0x809d5d0``
// ``third string => 0x809d600``
// ``first string => 0x809d5b8``
table.foreach ((key) => {
print ("%s => %p\n", key, key);
});

return 0;
}

valac --pkg glib-2.0 GLib.GenericSet.add.vala

Parameters:

hash_table

a GenericSet

key

a key to insert

Returns:

true if the key did not exist yet