« Home » « Learn » « Download » « Github »

logo

Cello High Level C

Examples

Usage

var prices = new(Tree, String, Int);
set(prices, $S("Apple"),  $I(12));
set(prices, $S("Banana"), $I( 6));
set(prices, $S("Pear"),   $I(55));

foreach (key in prices) {
  var price = get(prices, key);
  println("Price of %$ is %$", key, price);
}

Manipulation

var t = new(Tree, String, Int);
set(t, $S("Hello"), $I(2));
set(t, $S("There"), $I(5));

show($I(len(t))); /* 2 */
show($I(mem(t, $S("Hello")))); /* 1 */

rem(t, $S("Hello"));

show($I(len(t))); /* 1 */
show($I(mem(t, $S("Hello")))); /* 0 */
show($I(mem(t, $S("There")))); /* 1 */

resize(t, 0);

show($I(len(t))); /* 0 */
show($I(mem(t, $S("Hello")))); /* 0 */
show($I(mem(t, $S("There")))); /* 0 */

Tree


Balanced Binary Tree

The Tree type is a self balancing binary tree implemented as a red-black tree. It provides key-value access and requires the Cmp class to be defined on the key type.

Element lookup and insertion are provided as an O(log(n)) operation. This means in general a Tree is slower than a Table but it has several other nice properties such as being able to iterate over the items in order and not having large pauses for rehashing on some insertions.

This is largely equivalent to the C++ construct std::map

Derives

Implements

  • Assignassign
  • Cmpcmp eq neq gt lt ge le
  • Docname brief description definition
  • Getget set mem rem key_type val_type
  • Hashhash hash_data
  • Iterforeach iter_init iter_next iter_type
  • Lenlen
  • Markmark
  • Newnew del construct destruct
  • Resizeresize
  • Showshow look print scan

Back