Commit graph

70 commits

Author SHA1 Message Date
Ben Wiederhake d43d51eedc AK: Add FIXMEs to HashMap copy-construct and copy-assign
This underlines that we still copy-construct and copy-assign HashMaps.

Primarily, this makes it easier to develop towards OOM-safe(r) internal
data structures, by providing a reminder (the FIXME) and an easy error-
checking switch (just change it to "delete" to see some of the errors).
2023-05-19 22:33:57 +02:00
Ben Wiederhake 6421899078 AK: Rewrite HashMap::clone signature with template-args and const 2023-05-19 22:33:57 +02:00
Linus Groh bfdbb4b6ed AK: Add missing const qualifier to HashCompatible HashMap::contains() 2023-03-03 11:02:21 +00:00
Kenneth Myhra 9d13537fc7 AK: Add function 'shallow_clone()' to HashMap
This makes a shallow clone of the HashMap, the items temselves are not
cloned in any way.
2023-02-12 00:18:09 +00:00
Timothy Flynn 2af7447dfd AK: Define HashMap::take to find and remove a value from the map 2023-02-02 19:14:00 +00:00
Timothy Flynn 2f67f2ba3d AK: Return a constant reference from HashMap's constant get() override
We cannot return a mutable reference from a constant function.
2023-02-02 19:14:00 +00:00
Nico Weber 44de4d163b AK: Make HashMap::try_ensure work with a fallible construction callback
Co-authored-by: Timothy Flynn <trflynn89@pm.me>
2023-01-24 14:45:27 +00:00
Eli Youngs 950b36f95d AK: Add a try_ensure() method to HashMap 2022-12-16 10:41:56 -07:00
Ali Mohammad Pur 5809b4aafa AK: Let HashMap also take a ValueTraits
We were previously using Traits<V>, take that frrom the template
parameters instead.
This is needed by the Jakt runtime.
2022-12-11 20:44:54 +03:30
Thomas Queiroz 54c12b76ed AK: Remove HashMap::ensure_capacity
This is not perfect, since the constuctor can still fail.
2022-12-10 14:29:46 +01:00
Linus Groh d26aabff04 Everywhere: Run clang-format 2022-12-03 23:52:23 +00:00
Andreas Kling ae3ffdd521 AK: Make it possible to not using AK classes into the global namespace
This patch adds the `USING_AK_GLOBALLY` macro which is enabled by
default, but can be overridden by build flags.

This is a step towards integrating Jakt and AK types.
2022-11-26 15:51:34 +01:00
Vitaly Dyachkov a0a4d169f4 AK+LibGUI: Pass predicate to *_matching() methods by const reference 2022-05-08 17:02:00 +02:00
Ali Mohammad Pur 1a74895680 AK: Return Optional<ConstPeekType> for HashMap::get() const
While the previous implementation always copied the object, returning a
non-const reference to a const object is not valid.
2022-04-04 12:48:31 +02:00
Idan Horowitz 086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
Andreas Kling eb829924da AK: Remove return value from HashTable::remove() and HashMap::remove()
This was only used by remove_all_matching(), where it's no longer used.
2022-03-07 00:08:22 +01:00
Andreas Kling 623bdd8b6a AK: Simplify HashTable::remove_all_matching()
Just walk the table from start to finish, deleting buckets as we go.
This removes the need for remove() to return an iterator, which is
preventing me from implementing hash table auto-shrinking.
2022-03-07 00:08:22 +01:00
Idan Horowitz 9b0d90a71d AK: Support using custom comparison operations for hash compatible keys 2022-01-29 23:01:23 +02:00
Idan Horowitz 7356110033 AK: Support setting with non copyable keys in HashMap 2022-01-21 16:27:21 +01:00
Andreas Kling 5279a04c78 AK: Make Hash{Map,Table}::remove_all_matching() return removal success
These functions now return whether one or more entries were removed.
2022-01-05 18:57:14 +01:00
Andreas Kling 376e5ef912 AK: Add HashMap::remove_all_matching(predicate)
This removes all matching entries from a hash map in a single pass.
2022-01-05 18:57:14 +01:00
Hendiadyoin1 c673b7220a AK: Enable fast path for removal by hash-compatible key in HashMap/Table 2021-12-15 23:35:14 -08:00
Hendiadyoin1 d50360f5dd AK: Allow hash-compatible key types in Hash[Table|Map] lookup
This will allow us to avoid some potentially expensive type conversion
during lookup, like form String to StringView, which would allocate
memory otherwise.
2021-12-15 13:09:49 +03:30
Hendiadyoin1 f76241914c AK: Allow to clear HashTables/Maps with capacity 2021-11-11 09:19:17 +01:00
Andreas Kling 9d1f238450 AK: Make HashTable and HashMap try_* functions return ErrorOr<T>
This allows us to use TRY() and MUST() with them.
2021-11-11 01:27:46 +01:00
Andrew Kaster da87497e61 AK+LibC: Remove SERENITY_LIBC_BUILD guard around <initializer_list>
This was required before commit 5f724b6ca1
when we were building LibC before libstdc++ headers were available in
the sysroot. However as noted in that commit, we never actually needed
to be building LibC before libstdc++, so we can go ahead and remove this
ancient hack.
2021-09-20 00:39:46 +00:00
Brian Gianforcaro 54fe0c8855 AK: Add the ability to hash the contents of a AK::HashMap 2021-09-12 16:39:23 +02:00
Hediadyoin1 1aa527f5b6 AK: Add OOM safe interface to HashTable/Map
This adds a new HashSetResult only returned by try_set, to signal
allocation failure during setting.
2021-09-10 14:33:53 +00:00
Idan Horowitz 679bde06ed AK: Remove a redundant double find-call in HashMap::ensure
If the value was found there's no reason to search for it again.
2021-09-10 15:26:41 +03:00
Andreas Kling 1a71e20f93 AK: Add HashMap::ensure(key, callback)
This function ensures that a key is present in the HashMap.
If it's not present, it is inserted, and the corresponding value
is initialized with whatever the callback returns.

It allows us to express this:

    auto it = map.find(key);
    if (it == map.end()) {
        map.set(it, make_a_value());
        it = map.find(key);
    }
    auto& value = it->value;

Like this:

    auto& value = map.ensure(key, [] { return make_a_value(); });

Note that the callback is only invoked if we have to insert a missing
key into the HashMap. This is important in case constructing the default
value is expensive or otherwise undesirable.
2021-09-04 20:30:56 +02:00
Andreas Kling f65b039c44 AK: Sprinkle [[nodiscard]] on HashMap and HashTable 2021-07-21 18:18:29 +02:00
Andreas Kling a5c9a31001 AK: Remove unused HashMap::remove_one_randomly() 2021-07-21 18:08:11 +02:00
ngc6302h 3b81ba7c4f HashMap: Rename finders with a more accurate and self-descripting name 2021-07-13 17:31:00 +02:00
Idan Horowitz f1f00be691 AK: Add a missing using AK::OrderedHashMap statement 2021-06-15 23:51:20 +01:00
Hediadyoin1 4a81c79909 AK: Add Ordering support to HashTable and HashMap
Adds a IsOrdered flag to Hashtable and HashMap, which allows iteration
in insertion order
2021-06-15 22:16:55 +02:00
Ali Mohammad Pur 5bf37d758c AK: Let HashMap export its key and value types 2021-05-18 18:48:15 +01:00
Itamar 484823e9d5 AK: Add a non-const overload to HapMap::get()
Additionally, the const version of get() returns Optional<ConstPeekType>
for smart pointers.

For example, if the value in the HashMap is OwnPtr<u32>,
HashMap::get() const returns Optional<const u32*>.
2021-05-08 18:10:56 +02:00
Brian Gianforcaro 1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00
Brian Gianforcaro a8671ec166 AK: Annotate HashMap functions with [[nodiscard]] 2021-04-11 12:50:33 +02:00
Ben Wiederhake c6a42ab5c3 Everywhere: Remove unnecessary headers 4/4
Arbitrarily split up to make git bisect easier.

These unnecessary #include's were found by combining an automated tool (which
determined likely candidates) and some brain power (which decided whether
the #include is also semantically superfluous).
2021-02-08 18:03:57 +01:00
Lenny Maiorani e6f907a155 AK: Simplify constructors and conversions from nullptr_t
Problem:
- Many constructors are defined as `{}` rather than using the ` =
  default` compiler-provided constructor.
- Some types provide an implicit conversion operator from `nullptr_t`
  instead of requiring the caller to default construct. This violates
  the C++ Core Guidelines suggestion to declare single-argument
  constructors explicit
  (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c46-by-default-declare-single-argument-constructors-explicit).

Solution:
- Change default constructors to use the compiler-provided default
  constructor.
- Remove implicit conversion operators from `nullptr_t` and change
  usage to enforce type consistency without conversion.
2021-01-12 09:11:45 +01:00
Linus Groh 1ed72cc580 AK: Add HashMap(std::initializer_list<Entry>) constructor
This allows us to construct a HashMap from an initializer list like so:

    HashMap<K, V> hash_map = { { K, V }, { K, V } { K, V } };
2020-12-29 15:22:15 +01:00
Lenny Maiorani f5ced347e6 AK: Prefer using instead of typedef
Problem:
- `typedef` is a keyword which comes from C and carries with it old
  syntax that is hard to read.
- Creating type aliases with the `using` keyword allows for easier
  future maintenance because it supports template syntax.
- There is inconsistent use of `typedef` vs `using`.

Solution:
- Use `clang-tidy`'s checker called `modernize-use-using` to update
  the syntax to use the newer syntax.
- Remove unused functions to make `clang-tidy` happy.
- This results in consistency within the codebase.
2020-11-12 10:19:04 +01:00
Ben Wiederhake 8940bc3503 Meta+AK: Make clang-format-10 clean 2020-09-25 21:18:17 +02:00
Tom dadd53e4f2 AK: HashTable/HashMap return whether action was performed for set/remove
This allows performing an action based on whether something
was actually added or removed without having to look it up
prior to calling set() or remove().
2020-07-09 21:58:07 +02:00
Andreas Kling b813b2f871 AK: Make HashTable and HashMap use size_t for size and capacity 2020-02-24 09:42:52 +01:00
Andreas Kling e61cdf5c39 AK: Add HashMap, HashTable and Traits to Forward.h 2020-02-16 02:01:18 +01:00
Andreas Kling 6cbd72f54f AK: Remove bitrotted Traits::dump() mechanism
This was only used by HashTable::dump() which I used when doing the
first HashTable implementation. Removing this allows us to also remove
most includes of <AK/kstdio.h>.
2020-02-10 11:55:34 +01:00
Andreas Kling 94ca55cefd Meta: Add license header to source files
As suggested by Joshua, this commit adds the 2-clause BSD license as a
comment block to the top of every source file.

For the first pass, I've just added myself for simplicity. I encourage
everyone to add themselves as copyright holders of any file they've
added or modified in some significant way. If I've added myself in
error somewhere, feel free to replace it with the appropriate copyright
holder instead.

Going forward, all new source files should include a license header.
2020-01-18 09:45:54 +01:00
Andreas Kling bb32fd8bfa AK: Add HashMap::find() with customizable finder callback
This will allow clients to search the map without having to instantiate
a key value.
2019-08-25 06:45:27 +02:00