00001 /* 00002 * Copyright 2005, 2006, Internet2 00003 * Legal conditions are in file LICENSE 00004 * (MD5 = c434f2e53b8089d8b4d0172c7ce07360). 00005 */ 00006 /** 00007 * @file vfer_btree_delay.h 00008 * @author Ivan Beschastnikh 00009 * @brief Header file for btree_delay.c 00010 * 00011 * Defines prototypes of functions implemented in tree_delay.c and the 00012 * AVL tree and supporting data structures 00013 * 00014 * - 04/30/06 ivan Created 00015 */ 00016 00017 #ifndef VFER_BTREE_DELAY_H 00018 #define VFER_BTREE_DELAY_H 00019 00020 #include <sys/types.h> 00021 #include <stdio.h> 00022 #include <stdlib.h> 00023 #include <arpa/inet.h> 00024 00025 /* this is the number of nodes that are statically allocated as a optimization */ 00026 // #define MAX_NODES 8192 00027 00028 /** 00029 * @brief A node in a btree maintaining min delay history 00030 * 00031 * A node in a tree maintains a time stamp which determines how fresh 00032 * the value is, an index for the tree->nodes array (see optimization 00033 * above), left,right,parent pointers, and a height which is 00034 * dynamically maintained 00035 */ 00036 typedef struct node { 00037 int32_t val; /* delay value */ 00038 struct timeval stamp; /* timestamp of the last delay of this value */ 00039 /* int index; */ /* index of this node in the nodes array */ 00040 struct node* left; /* left child node */ 00041 struct node* right; /* right child node */ 00042 struct node* parent; /* parent node of this node */ 00043 int height; /* height of this node's subtree */ 00044 } node_t; 00045 00046 /** 00047 * @brief A btree maitaining min delay history in an AVL balancing algorithmic fashion 00048 * 00049 * a tree has a root pointer, a nodes array (see optimization above), 00050 * a nodes_free counter and free_nodes array (also for optimization) 00051 * and a window which determines the freshness of data values in the 00052 * tree (ie. when to expire them) */ 00053 typedef struct tree{ 00054 node_t* root; /* root node */ 00055 node_t last_val; /* last value added to the tree (in case all tree values expire) and the tree becomes empty */ 00056 /* node_t nodes[MAX_NODES]; */ /* statically allocated node array */ 00057 /* int nodes_free;*/ /* total number of nodes that are 00058 * available (not in the tree) */ 00059 /* int free_nodes[MAX_NODES];*/ /* array of indexes (into nodes 00060 * array) of nodes that are not in 00061 * the tree the first nodes_free 00062 * are >= 0; the reset are set to 00063 * -1 */ 00064 struct timeval window; /* history window = the time value to 00065 * maintain as the current window for 00066 * delay measurements */ 00067 } tree_t; 00068 00069 /* function prototypes */ 00070 void Tree_Init (tree_t* tree, struct timeval window); 00071 void Tree_Set_Window (tree_t* tree, struct timeval window); 00072 void Tree_Insert (tree_t* tree, int32_t val, struct timeval stamp); 00073 int32_t Tree_Min (tree_t* tree, struct timeval stamp); 00074 int32_t Tree_Max(tree_t* tree, struct timeval stamp); 00075 void Tree_Print (tree_t* tree); 00076 00077 /* useful macros */ 00078 00079 /* evaluates to false if tv_old + tv_window < tv_new, ow. evaluates to true */ 00080 #define VALUE_EXPIRED(tv_old, tv_new, tv_window) ( \ 00081 ((tv_old.tv_sec + tv_window.tv_sec) < tv_new.tv_sec) || \ 00082 (((tv_old.tv_sec + tv_window.tv_sec) == tv_new.tv_sec) && \ 00083 ((tv_old.tv_usec + tv_window.tv_usec) < tv_new.tv_usec))) 00084 00085 /* tree_balance = height(right_subtree) - height(left_subtree) -1,0,1 00086 is balanced ; anything else is unbalanced */ 00087 #define NODE_BALANCE(NODE) (NODE_HEIGHT(NODE->right) - NODE_HEIGHT(NODE->left)) 00088 00089 /* evaluates to the height of the node, maps null node to height of 0 */ 00090 #define NODE_HEIGHT(NODE) (NODE == NULL ? 0 : NODE->height) 00091 00092 /* recomputes the height for a node based on the height of the left and right subtrees */ 00093 #define NODE_RECOMPUTE_HEIGHT(NODE) { \ 00094 NODE->height = ((NODE_HEIGHT(NODE->left) > NODE_HEIGHT(NODE->right)) ? NODE_HEIGHT(NODE->left) : NODE_HEIGHT(NODE->right)) + 1; } 00095 00096 00097 #endif /* VFER_BTREE_DELAY_H */