Loading...
Searching...
No Matches
Qore::TreeMap Class Reference

A container for efficient path prefix lookup. More...

`#include <QC_TreeMap.dox.h>`

## Public Member Functions

| constructor ()
| Creates an empty TreeMap container.
| copy ()
| Throws an exception; objects of this class cannot be copied.
| destructor ()
| Releases any resource held by the instance.
auto | get (string path, *reference< *string > unmatched)
| Retrieves a value from the TreeMap and optionally returns the unmatched path suffix.
*hash | getAll ()
| Retrieves the entire TreeMap as a hash; returns NOTHING if the TreeMap is empty.
nothing | put (string path, auto value)
| Puts the mapping of `path` to `value` into the container.
auto | take (string path)
| Removes a value from the TreeMap and returns the value removed.

## Detailed Description

A container for efficient path prefix lookup.

The primary use of this class is in HttpServer for matching request URIs to handlers.

Example:

TreeMap tm();
tm.put("path/to/resource", handler1);
tm.put("scripts", handler2);
tm.put("scripts/special", handler3);
tm.get("path/to/resource/x"); # returns handler1
tm.get("path/to/resource?arg=1"); # returns handler1
tm.get("path/to/res"); # returns NOTHING
tm.get("path/to/resourcex"); # returns NOTHING
tm.get("path"); # returns NOTHING
tm.get("scripts/special/main.js"); # returns handler3
tm.get("scripts/normal/main.js"); # returns handler2

## Member Function Documentation

## constructor()

Qore::TreeMap::constructor | ( |  | ) |

Creates an empty TreeMap container.

Creates an empty TreeMap container.

## copy()

Qore::TreeMap::copy | ( |  | ) |

Throws an exception; objects of this class cannot be copied.

Exceptions

  TREEMAP-COPY-ERROR | objects of this class cannot be copied

## destructor()

Qore::TreeMap::destructor | ( |  | ) |

Releases any resource held by the instance.

Releases any resource held by the instance.

## get()

auto Qore::TreeMap::get | ( | string | path,
|  | *reference< *string > | unmatched
| ) |  |

Retrieves a value from the TreeMap and optionally returns the unmatched path suffix.

Code Flags:
  RET_VALUE_ONLY

Example:
  TreeMap tm();
  tm.put("abc", "1");
  tm.put("def/g/hi", "5");
  auto v;
  *string unmatched;
  # returns "1"
  v = tm.get("abc");
  # returns "1" and unmatched is ""
  v = tm.get("abc", \unmatched);
  # returns "1" and unmatched is "cde/"
  v = tm.get("abc/cde/", \unmatched);
  *hash h;
  v = tm.get("def/g/hi/five", \h.unmatched); # returns "5" and h.unmatched is "five"

Looks for an entry whose key is the longest prefix of `path`.

Parameters

  path | the path to lookup
  unmatched | an optional reference to the path suffix; if `path` is not found, then NOTHING is assigned to the reference

Returns
  the value pointed to by the longest prefix of `path` or NOTHING if no such mapping exists

Since
  Qore 0.8.13

## getAll()

*hash Qore::TreeMap::getAll | ( |  | ) |

Retrieves the entire TreeMap as a hash; returns NOTHING if the TreeMap is empty.

Code Flags:
  CONSTANT

Returns
  the entire TreeMap as a hash; returns NOTHING if the TreeMap is empty

## put()

nothing Qore::TreeMap::put | ( | string | path,
|  | auto | value
| ) |  |

Puts the mapping of `path` to `value` into the container.

Parameters

  path | the path to which `value` will be mapped
  value | the value to put into the TreeMap

See also
  TreeMap::remove()

## take()

auto Qore::TreeMap::take | ( | string | path | ) |

Removes a value from the TreeMap and returns the value removed.

`path` must be an exact match

Parameters

  path | the path to remove

Returns
  the value removed from the TreeMap or NOTHING if no exact match was found

See also
  TreeMap::put()