ArkScript
A small, lisp-inspired, functional scripting language
Dict.hpp
Go to the documentation of this file.
1/**
2 * @file Dict.hpp
3 * @author Lex Plateau (lexplt.dev@gmail.com)
4 * @brief Define how dictionaries are handled
5 * @date 2025-08-03
6 *
7 * @copyright Copyright (c) 2025
8 *
9 */
10
11#ifndef ARK_VM_VALUE_DICT_HPP
12#define ARK_VM_VALUE_DICT_HPP
13
14#include <Ark/VM/Value.hpp>
16
17#include <ankerl/unordered_dense.h>
18
19#include <vector>
20
21namespace Ark
22{
23 class VM;
24}
25
26namespace Ark::internal
27{
29 {
30 public:
31 Dict() = default;
32
33 /**
34 * @brief Assign a key to a value inside the dict
35 *
36 * @param key
37 * @param value
38 */
39 void set(const Value& key, const Value& value);
40
41 /**
42 * @brief Try to get a value from a given key. If no value is found, return Nil
43 *
44 * @param key
45 * @return const Value&
46 */
47 const Value& get(const Value& key);
48
49 /**
50 * @brief Check that a key exists
51 *
52 * @param key
53 * @return true if the dict has the key
54 * @return false otherwise
55 */
56 [[nodiscard]] bool contains(const Value& key) const;
57
58 /**
59 * @brief Remove an entry from the dict via its key
60 *
61 * @param key
62 */
63 void remove(const Value& key);
64
65 /**
66 * @brief Get a list of the dict keys
67 *
68 * @return std::vector<Value>
69 */
70 std::vector<Value> keys();
71
72 /**
73 * @brief Compute the number of (key, value) pairs in the dict
74 *
75 * @return std::size_t
76 */
77 [[nodiscard]] std::size_t size() const;
78
79 /**
80 * @brief Convert the dictionary to a string for pretty printing
81 *
82 * @param vm
83 * @return std::string
84 */
85 std::string toString(VM& vm) const;
86
87 friend bool operator==(const Dict&, const Dict&) noexcept;
88
89 private:
90 ankerl::unordered_dense::map<Value, Value> m_dict;
91 };
92
93 inline bool operator==(const Dict& A, const Dict& B) noexcept
94 {
95 return A.m_dict == B.m_dict;
96 }
97}
98
99#endif // ARK_VM_VALUE_DICT_HPP
#define ARK_API
Definition Module.hpp:22
ArkScript configuration macros.
Default value type handled by the virtual machine.
The ArkScript virtual machine, executing ArkScript bytecode.
Definition VM.hpp:46
ankerl::unordered_dense::map< Value, Value > m_dict
Definition Dict.hpp:90
bool operator==(const Namespace &A, const Namespace &B)
Definition Namespace.hpp:21