Penlight Documentation

# Class `pl.OrderedMap`

OrderedMap, a map which preserves ordering.

Derived from pl.Map.

Dependencies: pl.utils, pl.tablex, pl.class, pl.List, pl.Map

## Methods

pl.orderedmap:_init (t) | construct an OrderedMap.
pl.orderedmap:update (t) | update an OrderedMap using a table.
pl.orderedmap:set (key, val) | set the key's value.
pl.orderedmap:insert (pos, key, val) | insert a key/value pair before a given position.
pl.orderedmap:keys () | return the keys in order.
pl.orderedmap:values () | return the values in order.
pl.orderedmap:sort (cmp) | sort the keys.
pl.orderedmap:iter () | iterate over key-value pairs in order.

## Metamethods

pl.orderedmap:__pairs () | iterate over an ordered map (5.2).
pl.orderedmap:__tostring () | string representation of an ordered map.

## Methods

pl.orderedmap:_init (t)
  construct an OrderedMap. Will throw an error if the argument is bad.

  ### Parameters:

  - t optional initialization table, same as for OrderedMap:update
pl.orderedmap:update (t)
  update an OrderedMap using a table. If the table is itself an OrderedMap, then its entries will be appended. if it s a table of the form `{{key1=val1},{key2=val2},...}` these will be appended.

  Otherwise, it is assumed to be a map-like table, and order of extra entries is arbitrary.

  ### Parameters:

  - t table a table.

  ### Returns:

  1. the map, or nil in case of error
  2. the error message
pl.orderedmap:set (key, val)
  set the key's value. This key will be appended at the end of the map.

  If the value is nil, then the key is removed.

  ### Parameters:

  - key the key
  - val the value

  ### Returns:

  the map
pl.orderedmap:insert (pos, key, val)
  insert a key/value pair before a given position. Note: if the map already contains the key, then this effectively moves the item to the new position by first removing at the old position. Has no effect if the key does not exist and val is nil

  ### Parameters:

  - pos integer a position starting at 1
  - key the key
  - val the value; if nil use the old value
pl.orderedmap:keys ()
  return the keys in order. (Not a copy!)

  ### Returns:

  List
pl.orderedmap:values ()
  return the values in order. this is relatively expensive.

  ### Returns:

  List
pl.orderedmap:sort (cmp)
  sort the keys.

  ### Parameters:

  - cmp function a comparison function as for table.sort

  ### Returns:

  the map
pl.orderedmap:iter ()
  iterate over key-value pairs in order.

## Metamethods

pl.orderedmap:__pairs ()
  iterate over an ordered map (5.2).
pl.orderedmap:__tostring ()
  string representation of an ordered map.

generated by LDoc 1.5.0