Reduct  v1.0.4-3-gdaf0d70
A functional and immutable language.
Loading...
Searching...
No Matches
Atoms

Detailed Description

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

Data Structures

struct  reduct_atom_t
 Atom structure. More...
 

Macros

#define REDUCT_ATOM_SMALL_MAX   15
 The maximum length of a small atom.
 
#define REDUCT_FNV_PRIME   16777619U
 FNV-1a 32-bit prime.
 
#define REDUCT_FNV_OFFSET   2166136261U
 FNV-1a 32-bit offset basis.
 

Enumerations

enum  reduct_atom_lookup_flags_t { REDUCT_ATOM_LOOKUP_NONE = 0 , REDUCT_ATOM_LOOKUP_QUOTED = 1 << 0 }
 Atom lookup flags. More...
 

Functions

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_treduct_atom_lookup_int (struct reduct *reduct, reduct_int64_t value)
 Lookup an atom by integer value.
 
REDUCT_API reduct_atom_treduct_atom_lookup_float (struct reduct *reduct, reduct_float_t value)
 Lookup an atom by float value.
 
REDUCT_API reduct_atom_treduct_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.
 

Macro Definition Documentation

◆ 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.

Enumeration Type Documentation

◆ 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.

Function Documentation

◆ reduct_hash()

static REDUCT_ALWAYS_INLINE reduct_uint32_t reduct_hash ( const char *  str,
reduct_size_t  len 
)
inlinestatic

Hash a string.

Parameters
strThe string to hash.
lenThe length of the string.
Returns
The hash of the string.

Definition at line 72 of file atom.h.

Here is the caller graph for this function:

◆ reduct_atom_init()

static void reduct_atom_init ( reduct_atom_t atom)
inlinestatic

Initialize an atom.

Parameters
atomPointer to the atom to initialize.

Definition at line 88 of file atom.h.

◆ reduct_atom_deinit()

REDUCT_API void reduct_atom_deinit ( struct reduct *  reduct,
reduct_atom_t atom 
)

Deinitialize an atom.

Parameters
reductPointer to the Reduct structure.
atomPointer to the atom to deinitialize.
Here is the caller graph for this function:

◆ reduct_atom_is_equal()

static REDUCT_ALWAYS_INLINE reduct_bool_t reduct_atom_is_equal ( reduct_atom_t atom,
const char *  str,
reduct_size_t  len 
)
inlinestatic

Check if an atom is equal to a string.

Parameters
atomPointer to the atom.
strThe string to compare.
lenThe 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.

Here is the caller graph for this function:

◆ reduct_atom_lookup_int()

REDUCT_API reduct_atom_t * reduct_atom_lookup_int ( struct reduct *  reduct,
reduct_int64_t  value 
)

Lookup an atom by integer value.

Will create a new atom if it does not exist.

Parameters
reductPointer to the Reduct structure.
valueThe integer value.
Returns
A pointer to the atom.
Here is the caller graph for this function:

◆ reduct_atom_lookup_float()

REDUCT_API reduct_atom_t * reduct_atom_lookup_float ( struct reduct *  reduct,
reduct_float_t  value 
)

Lookup an atom by float value.

Will create a new atom if it does not exist.

Parameters
reductPointer to the Reduct structure.
valueThe float value.
Returns
A pointer to the atom.
Here is the caller graph for this function:

◆ reduct_atom_lookup()

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.

Will create a new atom if it does not exist.

Parameters
reductPointer to the Reduct structure.
strThe string to lookup.
lenThe length of the string.
flagsLookup flags to alter the interning behavior.
Returns
A pointer to the atom.
Here is the caller graph for this function:

◆ reduct_atom_normalize()

REDUCT_API void reduct_atom_normalize ( struct reduct *  reduct,
reduct_atom_t atom 
)

Normalize an atom, determining its shape and parsing escape sequences.

Warning
Should only be called on atoms stored in a reduct_item_t.
Parameters
reductPointer to the Reduct structure.
atomPointer to the atom to normalize.
Here is the caller graph for this function: