/usr/include/oce/NCollection_UBTreeFiller.hxx is in liboce-foundation-dev 0.18.2-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | // Created on: 2002-10-18
// Created by: Michael SAZONOV
// Copyright (c) 2002-2014 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#ifndef NCollection_UBTreeFiller_HeaderFile
#define NCollection_UBTreeFiller_HeaderFile
#include <NCollection_UBTree.hxx>
#include <NCollection_Vector.hxx>
#include <stdlib.h>
#include <stdio.h>
#if defined(__BORLANDC__) && (__BORLANDC__ < 0x0569)
#define rand_r(x) rand()
#endif
// MinGw defines rand_r in stdlib.h only in newer versions and only if _POSIX
// or _POSIX_THREAD_SAFE_FUNCTIONS is defined. Therefore we paste the
// definition from MinGw64 with g++ 5.1.0 here.
#if defined(__MINGW64__) || defined(__MINGW32__)
#ifndef rand_r
#define rand_r(__seed) (__seed == __seed ? rand () : rand ())
#endif
#endif
/**
* This class is used to fill an UBTree in a random order.
* The quality of a tree is much better (from the point of view of
* the search time) if objects are added to it in a random order to
* avoid adding a chain of neerby objects one following each other.
*
* This class collects objects to be added, and then add them to the tree
* in a random order.
*/
template <class TheObjType, class TheBndType> class NCollection_UBTreeFiller
{
public:
// ---------- PUBLIC TYPES ----------
//! Structure of pair (object, bnd box)
struct ObjBnd
{
TheObjType myObj;
TheBndType myBnd;
ObjBnd (const TheObjType& theObj, const TheBndType& theBnd)
: myObj(theObj), myBnd(theBnd) {}
ObjBnd ()
: myObj(TheObjType()), myBnd(TheBndType()) {}
};
//! UBTree algorithm
typedef NCollection_UBTree<TheObjType, TheBndType> UBTree;
typedef TYPENAME UBTree::TreeNode UBTreeNode;
// ---------- PUBLIC METHODS ----------
/**
* Constructor.
* @param theTree
* Tree instance that is to be filled.
* @param theAlloc
* Allocator for the Filler data.
* @param isFullRandom
* Takes effect when the number of items is large (order of 50,000). When
* it is True, the code uses the maximal randomization allowing a better
* balanced tree. If False, the randomization/tree balance are worse but
* the tree filling is faster due to better utilisation of CPU L1/L2 cache.
*/
NCollection_UBTreeFiller (UBTree& theTree,
const Handle(NCollection_BaseAllocator)& theAlloc=0L,
const Standard_Boolean isFullRandom = Standard_True)
: myTree(theTree), mySeqPtr(1000, theAlloc),
mySeed(1), myIsFullRandom (isFullRandom)
{
#ifndef _REENTRANT
// We use srand/rand for a single threaded application
// and rand_r for a multi threaded one.
// _REENTRANT must be defined for a multi threaded application.
srand (mySeed);
#endif
}
//! Adds a pair (theObj, theBnd) to my sequence
void Add (const TheObjType& theObj, const TheBndType& theBnd)
{ mySeqPtr.Append (ObjBnd (theObj, theBnd)); }
/**
* Fills the tree with the objects from my sequence. This method clears
* the internal buffer of added items making sure that no item would be added
* twice.
* @return
* the number of objects added to the tree.
*/
Standard_EXPORT Standard_Integer Fill ();
/**
* Remove all data from Filler, partculary if the Tree no more needed
* so the destructor of this Filler should not populate the useless Tree.
*/
void Reset() { mySeqPtr.Clear(); }
/**
* Check the filled tree for the total number of items and the balance
* outputting these results to ostream.
* @return
* the tree size (the same value is returned by method Fill()).
*/
Standard_EXPORT Standard_Integer CheckTree (Standard_OStream& theStream);
/**
* Destructor. Fills the tree with accumulated items if they have not been
* passed by a previous call of method Fill().
*/
~NCollection_UBTreeFiller ()
{
if (mySeqPtr.Length() > 0)
#ifdef OCCT_DEBUG_UBTREE
cout << "~NCollection_UBTreeFiller: " << Fill()
<< " objects added to the tree" << endl;
#else
Fill();
#endif
}
private:
// Assignment operator is made empty and private in order to
// avoid warning on MSVC (C4512)
void operator = (const NCollection_UBTreeFiller&) {}
static Standard_Real checkNode (const UBTreeNode& theNode,
const Standard_Integer theLength,
Standard_Integer& theNumber);
private:
// ---------- PRIVATE FIELDS ----------
UBTree& myTree;
NCollection_Vector<ObjBnd> mySeqPtr;
int mySeed; //!< seed for rand
Standard_Boolean myIsFullRandom;
};
#ifdef _REENTRANT
inline int take_random (int * theSeed)
{
return rand_r ((unsigned *) theSeed);
}
#else
inline int take_random (int *)
{
return rand();
}
#endif
//=======================================================================
//function : Fill
//purpose :
//=======================================================================
template <class TheObjType, class TheBndType>
Standard_Integer NCollection_UBTreeFiller<TheObjType,TheBndType>::Fill ()
{
Standard_Integer i, nbAdd = mySeqPtr.Length();
// Fisher-Yates randomization
if (myIsFullRandom)
for (i = nbAdd; i > 0; i--) {
unsigned int ind = take_random(&mySeed);
if (i > RAND_MAX) {
const unsigned int ind1 = take_random(&mySeed);
ind += (ind1 << 15);
}
ind = ind % i;
const ObjBnd& aObjBnd = mySeqPtr(ind);
myTree.Add (aObjBnd.myObj, aObjBnd.myBnd);
mySeqPtr(ind) = mySeqPtr(i-1);
}
else
for (i = nbAdd; i > 0; i--) {
unsigned int ind = take_random(&mySeed);
ind = i - (ind % i) - 1;
const ObjBnd& aObjBnd = mySeqPtr(ind);
myTree.Add (aObjBnd.myObj, aObjBnd.myBnd);
mySeqPtr(ind) = mySeqPtr(i-1);
}
mySeqPtr.Clear();
return nbAdd;
}
//=======================================================================
//function : CheckTree
//purpose :
//=======================================================================
template <class TheObjType, class TheBndType>
Standard_Integer NCollection_UBTreeFiller<TheObjType,TheBndType>::CheckTree
(Standard_OStream& theStream)
{
Standard_Integer aNumber(0);
const Standard_Real aLen = checkNode (myTree.Root(), 0, aNumber);
const Standard_Real num = (double) aNumber;
const Standard_Real aLen1 = sqrt (aLen / num);
const Standard_Real aLen0 = log(num) / log(2.);
char buf[128];
sprintf (buf, "Checking UBTree:%8d leaves, balance =%7.2f",
aNumber, aLen1 / aLen0);
theStream << buf << endl;
return aNumber;
}
//=======================================================================
//function : checkNode
//purpose :
//=======================================================================
template <class TheObjType, class TheBndType>
Standard_Real NCollection_UBTreeFiller<TheObjType,TheBndType>::checkNode
(const TYPENAME NCollection_UBTree<TheObjType, TheBndType>::TreeNode& theNode,
const Standard_Integer theLength,
Standard_Integer& theNumber)
{
Standard_Real aLength;
if (!theNode.IsLeaf())
aLength = (checkNode (theNode.Child(0), theLength+1, theNumber) +
checkNode (theNode.Child(1), theLength+1, theNumber));
else {
theNumber++;
aLength = theLength * theLength;
}
return aLength;
}
#endif
|