# ui/autolayout/layoutsymbol (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/ui/autolayout/layoutsymbol/

A symbol inside the solver. (LayoutSymbol of the original ui/autolayout/layoutsymbol.js)
- Points to rows and columns of the tableau. Numbers are assigned globally, increasing by one, and equality and hashing go by number.
- Differences from the original:
  - The original is an object, so Map keys use object identity. The Rust version is a Copy value holding a number and a kind, and is told apart by number.
  - Invalid symbols share the single number 0. (the original assigns a new number on every invalid(). Invalid symbols are not used as keys, so the result is the same)
  - The original setType is unused and would mean something different on a Copy value, so it was not ported.
  - LayoutSymbolMap is an addition in the Rust version. It keeps insertion order like the Maps the original uses for rows and cells.
    (removing and reinserting moves a key to the end, changing the value of an existing key keeps its place) The order in which the solver walks cells and rows
    decides which symbol is chosen, so it walks in the same order as the original.

## LayoutSymbol (struct) (Clone, Copy, Debug, PartialEq, Eq, Hash)

A symbol inside the solver.
- A value used inside the solver. User code usually uses only LayoutVariable and LayoutSolver.

Example: Symbols told apart by number

```rust
use vanilla::ui::autolayout::layoutsymbol::LayoutSymbol;
use vanilla::ui::autolayout::layoutsymboltype::LayoutSymbolType;

let first = LayoutSymbol::new(LayoutSymbolType::External);
let second = LayoutSymbol::new(LayoutSymbolType::External);
assert_ne!(first, second);
assert_eq!(first, first.clone());
assert!(LayoutSymbol::invalid().is_invalid());
assert_eq!(LayoutSymbol::invalid().get_id(), 0);
```

- `pub fn new(symbol_type: LayoutSymbolType) -> LayoutSymbol` (constructor, since 0.4.0): Constructor. (assigns a new number)
  - `symbol_type` LayoutSymbolType: Symbol kind.
  - returns LayoutSymbol: A symbol with a new number (increasing by one from 1).
- `pub fn invalid() -> LayoutSymbol` (static, since 0.4.0): Invalid symbol.
  - returns LayoutSymbol: A symbol with number 0 and kind Invalid. (always the same value)
- `pub fn get_id(&self) -> u64` (method, since 0.4.0): Returns the number.
  - returns u64: Number. (0 for the invalid symbol)
- `pub fn get_type(&self) -> LayoutSymbolType` (method, since 0.4.0): Returns the kind.
  - returns LayoutSymbolType: Kind.
- `pub fn is_invalid(&self) -> bool` (method, since 0.4.0): Whether it is the invalid symbol.
  - returns bool: true if the kind is Invalid.

## LayoutSymbolMap (struct) (Clone, Debug, Default)

A symbol key map that keeps insertion order. (in place of the original `Map<LayoutSymbol, V>`)
- Keeps entries in a slot list (Vec) in insertion order, and finds them directly with a hash map from number to slot index.
- Removing leaves the slot empty, and when empty slots pile up, they are compacted while keeping the order.

Example: A map that keeps insertion order

```rust
use vanilla::ui::autolayout::layoutsymbol::{LayoutSymbol, LayoutSymbolMap};
use vanilla::ui::autolayout::layoutsymboltype::LayoutSymbolType;

let first = LayoutSymbol::new(LayoutSymbolType::Slack);
let second = LayoutSymbol::new(LayoutSymbolType::Error);
let mut map = LayoutSymbolMap::new();
map.insert(first, 1.0);
map.insert(second, 2.0);
map.insert(first, 10.0);
let values: Vec<f64> = map.iter().map(|(_, value)| *value).collect();
assert_eq!(values, vec![10.0, 2.0]);

assert_eq!(map.remove(&first), Some(10.0));
map.insert(first, 3.0);
let values: Vec<f64> = map.iter().map(|(_, value)| *value).collect();
assert_eq!(values, vec![2.0, 3.0]);
assert_eq!(map.len(), 2);
assert_eq!(map.get(&LayoutSymbol::invalid()), None);
```

- `pub fn new() -> LayoutSymbolMap<V>` (constructor, since 0.4.0): Constructor.
  - returns LayoutSymbolMap<V>: Empty map.
- `pub fn len(&self) -> usize` (method, since 0.4.0): Number of entries.
  - returns usize: Number of keys held. (removed slots are not counted)
- `pub fn is_empty(&self) -> bool` (method, since 0.4.0): Whether it is empty.
  - returns bool: true if there are no keys.
- `pub fn contains_key(&self, symbol: &LayoutSymbol) -> bool` (method, since 0.4.0): Whether the key exists.
  - `symbol` &LayoutSymbol: Key to look for.
  - returns bool: true if the key exists.
- `pub fn get(&self, symbol: &LayoutSymbol) -> Option<&V>` (method, since 0.4.0): Finds a value.
  - `symbol` &LayoutSymbol: Key to look for.
  - returns Option<&V>: Reference to the value. None if the key does not exist.
- `pub fn get_mut(&mut self, symbol: &LayoutSymbol) -> Option<&mut V>` (method, since 0.4.0): Finds a value. (mutably)
  - `symbol` &LayoutSymbol: Key to look for.
  - returns Option<&mut V>: Mutable reference to the value. None if the key does not exist.
- `pub fn insert(&mut self, symbol: LayoutSymbol, value: V)` (method, since 0.4.0): Inserts. (an existing key keeps its place and only its value changes, a new key is appended at the end)
  - `symbol` LayoutSymbol: Key.
  - `value` V: Value to set.
- `pub fn remove(&mut self, symbol: &LayoutSymbol) -> Option<V>` (method, since 0.4.0): Removes. (returns the removed value)
  - `symbol` &LayoutSymbol: Key to remove.
  - returns Option<V>: The removed value. None if the key does not exist.
- `pub fn clear(&mut self)` (method, since 0.4.0): Clears all.
- `pub fn iter(&self) -> impl Iterator<Item = (LayoutSymbol, &V)>` (method, since 0.4.0): Iterates in insertion order.
  - returns impl Iterator<Item = (LayoutSymbol, &V)>: Iterator yielding (key, value reference) in insertion order.
- `pub fn iter_mut(&mut self) -> impl Iterator<Item = (LayoutSymbol, &mut V)>` (method, since 0.4.0): Iterates in insertion order. (mutably)
  - returns impl Iterator<Item = (LayoutSymbol, &mut V)>: Iterator yielding (key, mutable value reference) in insertion order.
