Atoms represent all strings within a Reduct expression, as such it also represents anything that a string can be, including integers, floats and intrinsics.
Interning
To improve performance, we "intern" all atoms. Meaning that instead of needing to use strcmp() or similar to compare atoms, we store all atoms in a hash map.
When a new string is encountered, we look it up in the map. As such, any instance of the same string will always be represented by the same reduct_atom_t structure. Turning a string comparison into a pointer comparison and avoiding redundant parsing of numeric or intrinsic values.
- See also
- Wikipedia String Interning
|
| static REDUCT_ALWAYS_INLINE reduct_uint32_t | reduct_hash (const char *str, reduct_size_t len) |
| | Hash a string.
|
| |
| static void | reduct_atom_init (reduct_atom_t *atom) |
| | Initialize an atom.
|
| |
| REDUCT_API void | reduct_atom_deinit (struct reduct *reduct, reduct_atom_t *atom) |
| | Deinitialize an atom.
|
| |
| static REDUCT_ALWAYS_INLINE reduct_bool_t | reduct_atom_is_equal (reduct_atom_t *atom, const char *str, reduct_size_t len) |
| | Check if an atom is equal to a string.
|
| |
| REDUCT_API reduct_atom_t * | reduct_atom_lookup_int (struct reduct *reduct, reduct_int64_t value) |
| | Lookup an atom by integer value.
|
| |
| REDUCT_API reduct_atom_t * | reduct_atom_lookup_float (struct reduct *reduct, reduct_float_t value) |
| | Lookup an atom by float value.
|
| |
| REDUCT_API reduct_atom_t * | reduct_atom_lookup (struct reduct *reduct, const char *str, reduct_size_t len, reduct_atom_lookup_flags_t flags) |
| | Lookup an atom in the Reduct structure.
|
| |
| REDUCT_API void | reduct_atom_normalize (struct reduct *reduct, reduct_atom_t *atom) |
| | Normalize an atom, determining its shape and parsing escape sequences.
|
| |
◆ REDUCT_ATOM_SMALL_MAX
| #define REDUCT_ATOM_SMALL_MAX 15 |
The maximum length of a small atom.
Definition at line 32 of file atom.h.
◆ REDUCT_FNV_PRIME
| #define REDUCT_FNV_PRIME 16777619U |
FNV-1a 32-bit prime.
Definition at line 62 of file atom.h.
◆ REDUCT_FNV_OFFSET
| #define REDUCT_FNV_OFFSET 2166136261U |
FNV-1a 32-bit offset basis.
Definition at line 63 of file atom.h.
◆ reduct_atom_lookup_flags_t
Atom lookup flags.
| Enumerator |
|---|
| REDUCT_ATOM_LOOKUP_NONE | No flags.
|
| REDUCT_ATOM_LOOKUP_QUOTED | Atom should be explicitly quoted.
|
Definition at line 37 of file atom.h.
◆ reduct_hash()
Hash a string.
- Parameters
-
| str | The string to hash. |
| len | The length of the string. |
- Returns
- The hash of the string.
Definition at line 72 of file atom.h.
◆ reduct_atom_init()
Initialize an atom.
- Parameters
-
| atom | Pointer to the atom to initialize. |
Definition at line 88 of file atom.h.
◆ reduct_atom_deinit()
Deinitialize an atom.
- Parameters
-
| reduct | Pointer to the Reduct structure. |
| atom | Pointer to the atom to deinitialize. |
◆ reduct_atom_is_equal()
Check if an atom is equal to a string.
- Parameters
-
| atom | Pointer to the atom. |
| str | The string to compare. |
| len | The length of the string. |
- Returns
REDUCT_TRUE if the atom is equal to the string, REDUCT_FALSE otherwise.
Definition at line 112 of file atom.h.
◆ reduct_atom_lookup_int()
Lookup an atom by integer value.
Will create a new atom if it does not exist.
- Parameters
-
| reduct | Pointer to the Reduct structure. |
| value | The integer value. |
- Returns
- A pointer to the atom.
◆ reduct_atom_lookup_float()
Lookup an atom by float value.
Will create a new atom if it does not exist.
- Parameters
-
| reduct | Pointer to the Reduct structure. |
| value | The float value. |
- Returns
- A pointer to the atom.
◆ reduct_atom_lookup()
Lookup an atom in the Reduct structure.
Will create a new atom if it does not exist.
- Parameters
-
| reduct | Pointer to the Reduct structure. |
| str | The string to lookup. |
| len | The length of the string. |
| flags | Lookup flags to alter the interning behavior. |
- Returns
- A pointer to the atom.
◆ reduct_atom_normalize()
Normalize an atom, determining its shape and parsing escape sequences.
- Warning
- Should only be called on atoms stored in a
reduct_item_t.
- Parameters
-
| reduct | Pointer to the Reduct structure. |
| atom | Pointer to the atom to normalize. |