Qore Programming Language Reference Manual  1.7.0
Qore::TreeMap Class Reference

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

Public Member Methods

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

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-ERRORobjects 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
paththe path to lookup
unmatchedan 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
paththe path to which value will be mapped
valuethe 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
paththe path to remove
Returns
the value removed from the TreeMap or NOTHING if no exact match was found
See also
TreeMap::put()