HPCToolkit
BalancedTree.h
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // Balanced Trees (Red-black) in the style of CLR
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 // Author:
59 // Based on code by Nathan Froyd.
60 // Nathan Tallent, Rice University.
61 //
62 //***************************************************************************
63 
64 #ifndef prof_lean_BalancedTree_h
65 #define prof_lean_BalancedTree_h
66 
67 //************************* System Include Files ****************************
68 
69 #include <stddef.h>
70 #include <stdbool.h>
71 
72 //*************************** User Include Files ****************************
73 
74 #include <include/uint.h>
75 
76 #include "pfq-rwlock.h"
77 #include "spinlock.h"
78 
79 
80 //*************************** Forward Declarations **************************
81 
82 //***************************************************************************
83 
84 //***************************************************************************
85 //
86 //***************************************************************************
87 
88 typedef void* (*BalancedTree_alloc_fn_t)(size_t);
89 
90 typedef enum {
91 
94 
96 
97 
98 typedef struct BalancedTreeNode
99 {
100  // node structure
105 
106  // node data
107  void* key;
108  void* data;
109 
111 
112 
113 
114 static inline BalancedTreeNode_t*
116 {
118  x->data = alloc(dataSz);
119  return x;
120 }
121 
122 
123 void
125  void* key, BalancedTreeNode_t* parent);
126 
127 
128 //***************************************************************************
129 //
130 //***************************************************************************
131 
132 typedef struct BalancedTree
133 {
136 
138  size_t nodeDataSz; // size of BalancedTreeNode_t.data
139 
142 
144 
145 
146 void
148  BalancedTree_alloc_fn_t allocFn, size_t nodeDataSz);
149 
150 
151 static inline uint
153 {
154  return tree->size;
155 }
156 
157 
158 static inline BalancedTreeNode_t*
160 {
162  BalancedTreeNode_t* curr = tree->root;
163  while (curr != NULL && curr->key != key) {
164  if (key < curr->key)
165  curr = curr->left;
166  else
167  curr = curr->right;
168  }
170  return curr;
171 }
172 
173 
174 // BalancedTree_insert: Insert `key' into `tree' (if it does not
175 // already exist). Returns the node containing the key, which may
176 // have already existed or be newly allocated.
179 
180 
181 #endif /* prof_lean_BalancedTree_h */
BalancedTreeColor_t
Definition: BalancedTree.h:90
spinlock_t spinlock
Definition: BalancedTree.h:141
static BalancedTreeNode_t * BalancedTreeNode_alloc(BalancedTree_alloc_fn_t alloc, size_t dataSz)
Definition: BalancedTree.h:115
static uint BalancedTree_size(BalancedTree_t *tree)
Definition: BalancedTree.h:152
struct BalancedTree BalancedTree_t
struct BalancedTreeNode * parent
Definition: BalancedTree.h:101
void *(* BalancedTree_alloc_fn_t)(size_t)
Definition: BalancedTree.h:88
BalancedTree_alloc_fn_t allocFn
Definition: BalancedTree.h:137
struct BalancedTreeNode BalancedTreeNode_t
pfq_rwlock_t rwlock
Definition: BalancedTree.h:140
struct BalancedTreeNode * right
Definition: BalancedTree.h:103
size_t nodeDataSz
Definition: BalancedTree.h:138
static BalancedTreeNode_t * BalancedTree_find(BalancedTree_t *tree, void *key)
Definition: BalancedTree.h:159
BalancedTreeNode_t * BalancedTree_insert(BalancedTree_t *tree, void *key)
Definition: BalancedTree.c:118
struct BalancedTreeNode * left
Definition: BalancedTree.h:102
unsigned int uint
Definition: uint.h:124
void BalancedTreeNode_init(BalancedTreeNode_t *x, void *key, BalancedTreeNode_t *parent)
Definition: BalancedTree.c:85
void pfq_rwlock_read_lock(pfq_rwlock_t *l)
Definition: pfq-rwlock.c:122
void pfq_rwlock_read_unlock(pfq_rwlock_t *l)
Definition: pfq-rwlock.c:134
mem_alloc alloc
#define NULL
Definition: ElfHelper.cpp:85
BalancedTreeNode_t * root
Definition: BalancedTree.h:134
void BalancedTree_init(BalancedTree_t *tree, BalancedTree_alloc_fn_t allocFn, size_t nodeDataSz)
Definition: BalancedTree.c:103
bitree_uwi_t * tree
BalancedTreeColor_t color
Definition: BalancedTree.h:104