DGtal 1.3.0
Loading...
Searching...
No Matches
Functions
testBits.cpp File Reference
#include <cstdio>
#include <cmath>
#include <iostream>
#include "DGtal/base/Common.h"
#include "DGtal/base/Bits.h"

Go to the source code of this file.

Functions

unsigned int index (DGtal::uint32_t n, unsigned int b)
 
int main ()
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
Jacques-Olivier Lachaud (jacqu.nosp@m.es-o.nosp@m.livie.nosp@m.r.la.nosp@m.chaud.nosp@m.@uni.nosp@m.v-sav.nosp@m.oie..nosp@m.fr ) Laboratory of Mathematics (CNRS, UMR 5807), University of Savoie, France
Date
2012/07/02

This file is part of the DGtal library

Definition in file testBits.cpp.

Function Documentation

◆ index()

unsigned int index ( DGtal::uint32_t  n,
unsigned int  b 
)

Definition at line 44 of file testBits.cpp.

45{
46 int i = 0;
47 for ( ; b != 0; --b )
48 {
49 if ( n & 1 ) ++i;
50 n >>= 1;
51 }
52 return ( n & 1 ) ? i + 1 : 0;
53}

◆ main()

int main ( void  )
Examples
arithmetic/approximation.cpp, arithmetic/convergents-biginteger.cpp, arithmetic/convergents.cpp, arithmetic/extended-euclid.cpp, arithmetic/fraction.cpp, arithmetic/lower-integer-convex-hull.cpp, arithmetic/pattern.cpp, dec/exampleDECSurface.cpp, dec/exampleDiscreteExteriorCalculusChladni.cpp, dec/exampleDiscreteExteriorCalculusSolve.cpp, dec/exampleDiscreteExteriorCalculusUsage.cpp, dec/exampleHeatLaplace.cpp, dec/examplePropagation.cpp, doc-examples/demo-kernel-1.cpp, doc-examples/kernelDomain.cpp, doc-examples/range.cpp, examples/tutorial-examples/polyhedralizer.cpp, geometry/curves/convex-and-concave-parts.cpp, geometry/curves/estimation/exampleCurvature.cpp, geometry/curves/estimation/exampleLMST2D.cpp, geometry/curves/estimation/exampleLMST3D.cpp, geometry/curves/estimation/exampleLMST3DBy2D.cpp, geometry/curves/exampleAlphaThickSegment.cpp, geometry/curves/exampleAlphaThickSegmentNoisy.cpp, geometry/curves/exampleAlphaThickSegmentTgtCover.cpp, geometry/curves/exampleArithDSS3d.cpp, geometry/curves/exampleArithmeticalDSL.cpp, geometry/curves/exampleArithmeticalDSS.cpp, geometry/curves/exampleArithmeticalDSSComputer.cpp, geometry/curves/exampleDSLSubsegment.cpp, geometry/curves/exampleDigitalConvexity.cpp, geometry/curves/exampleFrechetShortcut.cpp, geometry/curves/exampleGridCurve2d.cpp, geometry/curves/exampleGridCurve3d-2.cpp, geometry/curves/exampleGridCurve3d.cpp, geometry/curves/exampleNaive3DDSSComputer.cpp, geometry/curves/exampleRationalConvexity.cpp, geometry/curves/greedy-dss-decomposition.cpp, geometry/curves/greedyAlphaThickDecomposition.cpp, geometry/meshes/curvature-comparator-ii-cnc-3d.cpp, geometry/meshes/curvature-measures-icnc-3d.cpp, geometry/meshes/curvature-measures-icnc-XY-3d.cpp, geometry/meshes/curvature-measures-nc-3d.cpp, geometry/meshes/curvature-measures-nc-XY-3d.cpp, geometry/meshes/digpoly-curvature-measures-cnc-3d.cpp, geometry/meshes/digpoly-curvature-measures-cnc-XY-3d.cpp, geometry/meshes/obj-curvature-measures-icnc-3d.cpp, geometry/meshes/obj-curvature-measures-icnc-XY-3d.cpp, geometry/meshes/vol-curvature-measures-icnc-3d.cpp, geometry/meshes/vol-curvature-measures-icnc-XY-3d.cpp, geometry/surfaces/dvcm-2d-curvature.cpp, geometry/surfaces/dvcm-3d.cpp, geometry/surfaces/greedy-plane-segmentation-ex2.cpp, geometry/surfaces/greedy-plane-segmentation.cpp, geometry/tools/checkLatticeBallQuickHull.cpp, geometry/tools/determinant/exampleInHalfPlane.cpp, geometry/tools/exampleAlphaShape.cpp, geometry/tools/exampleConvexHull2D.cpp, geometry/tools/exampleLatticeBallDelaunay2D.cpp, geometry/tools/exampleLatticeBallDelaunay3D.cpp, geometry/tools/exampleLatticeBallQuickHull3D.cpp, geometry/tools/examplePreimage.cpp, geometry/tools/exampleQuickHull3D.cpp, geometry/tools/exampleRationalBallDelaunay3D.cpp, geometry/tools/exampleRationalBallQuickHull3D.cpp, geometry/volumes/checkFullConvexityTheorems.cpp, geometry/volumes/digitalPolyhedronBuilder3D.cpp, geometry/volumes/distance/distancetransform2D.cpp, geometry/volumes/distance/distancetransform3D.cpp, geometry/volumes/distance/exampleFMM2D.cpp, geometry/volumes/distance/exampleFMM3D.cpp, geometry/volumes/distance/toricdomainvolumetric.cpp, geometry/volumes/distance/voronoimap2D.cpp, geometry/volumes/dvcm-2d.cpp, geometry/volumes/exampleBoundedLatticePolytopeCount2D.cpp, geometry/volumes/exampleBoundedLatticePolytopeCount3D.cpp, geometry/volumes/exampleBoundedLatticePolytopeCount4D.cpp, geometry/volumes/fullConvexityAnalysis3D.cpp, geometry/volumes/fullConvexityCollapsiblePoints2D.cpp, geometry/volumes/fullConvexityLUT2D.cpp, geometry/volumes/fullConvexityShortestPaths3D.cpp, geometry/volumes/fullConvexitySphereGeodesics.cpp, geometry/volumes/fullConvexityThinning3D.cpp, geometry/volumes/standardDigitalPolyhedronBuilder3D.cpp, graph/graphTraversal.cpp, graph/volDistanceTraversal.cpp, images/exampleConstImageAdapter.cpp, images/exampleImageFactoryFromHDF5.cpp, images/exampleRigidtransformation2d.cpp, images/exampleRigidtransformation3d.cpp, images/exampleTiledImage.cpp, images/extract2DImagesFrom3D.cpp, images/extract2DImagesFrom3DandVisu.cpp, images/extract2DSlicesImagesFrom3D.cpp, images/imageBasicSubsampling.cpp, images/raw2HDF5.cpp, io/boards/dgtalBoard2D-1-points.cpp, io/boards/dgtalBoard2D-2-sets.cpp, io/boards/dgtalBoard2D-3-custom-classes.cpp, io/boards/dgtalBoard2D-3-custom-points.cpp, io/boards/dgtalBoard2D-4-colormaps.cpp, io/boards/dgtalBoard3D-1-points.cpp, io/boards/dgtalBoard3D-2-ks.cpp, io/boards/dgtalBoard3D-6-clipping.cpp, io/boards/dgtalBoard3DTo2D-1-points.cpp, io/boards/dgtalBoard3DTo2D-2-sets.cpp, io/boards/dgtalBoard3DTo2D-2bis-sets.cpp, io/boards/dgtalBoard3DTo2D-3-objects.cpp, io/boards/dgtalBoard3DTo2D-4-modes.cpp, io/boards/dgtalBoard3DTo2D-5-custom.cpp, io/boards/dgtalBoard3DTo2D-6.cpp, io/boards/dgtalBoard3DTo2D-KSCell.cpp, io/boards/exampleBezierCurve.cpp, io/boards/logoDGtal.cpp, io/digitalSetFromPointList.cpp, io/digitalSetFromVol.cpp, io/display3DToOFF.cpp, io/meshFromOFF.cpp, io/viewDualSurface.cpp, io/viewers/demo-kernel-2.cpp, io/viewers/viewer3D-1-points.cpp, io/viewers/viewer3D-10-interaction.cpp, io/viewers/viewer3D-11-extension.cpp, io/viewers/viewer3D-2-sets.cpp, io/viewers/viewer3D-3-objects.cpp, io/viewers/viewer3D-4-modes.cpp, io/viewers/viewer3D-4bis-illustrationMode.cpp, io/viewers/viewer3D-5-custom.cpp, io/viewers/viewer3D-6-clipping.cpp, io/viewers/viewer3D-7-planes.cpp, io/viewers/viewer3D-7-stdplane.cpp, io/viewers/viewer3D-7bis-planes.cpp, io/viewers/viewer3D-7bis-stdplane.cpp, io/viewers/viewer3D-8-2DSliceImages.cpp, io/viewers/viewer3D-8bis-2Dimages.cpp, io/viewers/viewer3D-9-3Dimages.cpp, math/polynomial-derivative.cpp, math/polynomial-read.cpp, math/polynomial2-derivative.cpp, shapes/exampleEuclideanShapesDecorator.cpp, shapes/exampleMeshVoxelizer.cpp, shapes/exampleSurfaceMesh.cpp, shapes/mesh3DConstructionAndVisualisation.cpp, shapes/viewMarchingCubes.cpp, shapes/viewPolygonalMarchingCubes.cpp, topology/3dBorderExtraction.cpp, topology/3dBorderExtractionImg.cpp, topology/3dKSSurfaceExtraction.cpp, topology/area-estimation-with-digital-surface.cpp, topology/area-estimation-with-indexed-digital-surface.cpp, topology/ctopo-1-3d.cpp, topology/ctopo-1.cpp, topology/ctopo-1s-3d.cpp, topology/ctopo-2-3d.cpp, topology/ctopo-2.cpp, topology/ctopo-fillContours.cpp, topology/cubical-complex-collapse.cpp, topology/cubical-complex-illustrations.cpp, topology/digitalSetToCubicalComplexes2D.cpp, topology/digitalSurfaceSlice.cpp, topology/frontierAndBoundary.cpp, topology/generateSimplicityTables2D.cpp, topology/generateSimplicityTables3D.cpp, topology/homotopicThinning3D.cpp, topology/khalimskySpaceScanner.cpp, topology/trackImplicitPolynomialSurfaceToOFF.cpp, topology/volBreadthFirstTraversal.cpp, topology/volMarchingCubes.cpp, topology/volScanBoundary.cpp, topology/volToOFF.cpp, topology/volTrackBoundary.cpp, tutorial-examples/2DSliceImageFromVol.cpp, tutorial-examples/AreaSurfaceEstimation-final.cpp, tutorial-examples/FMMErosion.cpp, tutorial-examples/freemanChainFromImage.cpp, tutorial-examples/polyhedralizer.cpp, and tutorial-examples/volDTGranulo.cpp.

Definition at line 56 of file testBits.cpp.

57{
58 unsigned int nb = 0;
59 unsigned int nbok = 0;
60 ++nb; nbok += Bits::nbSetBits( (DGtal::uint8_t)-1) == 8 ? 1 : 0;
61 ++nb; nbok += Bits::nbSetBits( (DGtal::uint16_t)-1) == 16 ? 1 : 0;
62 ++nb; nbok += Bits::nbSetBits( (DGtal::uint32_t)-1) == 32 ? 1 : 0;
63 ++nb; nbok += Bits::nbSetBits( (DGtal::uint64_t)-1) == 64 ? 1 : 0;
64 ++nb; nbok += Bits::nbSetBits( (DGtal::uint8_t)1) == 1 ? 1 : 0;
65 ++nb; nbok += Bits::nbSetBits( (DGtal::uint16_t)1) == 1 ? 1 : 0;
66 ++nb; nbok += Bits::nbSetBits( (DGtal::uint32_t)1) == 1 ? 1 : 0;
67 ++nb; nbok += Bits::nbSetBits( (DGtal::uint64_t)1) == 1 ? 1 : 0;
68 ++nb; nbok += Bits::nbSetBits( (DGtal::uint8_t)-2) == 7 ? 1 : 0;
69 ++nb; nbok += Bits::nbSetBits( (DGtal::uint16_t)-2) == 15 ? 1 : 0;
70 ++nb; nbok += Bits::nbSetBits( (DGtal::uint32_t)-2) == 31 ? 1 : 0;
71 ++nb; nbok += Bits::nbSetBits( (DGtal::uint64_t)-2) == 63 ? 1 : 0;
72
73 for ( unsigned int i = 0; i < 100; ++i )
74 {
75 DGtal::uint16_t n = (DGtal::uint16_t) ( rand() % 65536 );
76 for ( unsigned int b = 0; b < 16; ++b )
77 {
78 ++nb; nbok += Bits::indexInSetBits( n, b ) == index( n, b ) ? 1 : 0;
79 }
80 }
81
82 std::cerr << "(" << nbok << "/" << nb << ")" << " tests." << std::endl;
83
84 trace.beginBlock ( "Testing speed of loop version of indexInSetBits" );
85 srand( 0 );
86 unsigned int val = 0;
87 for ( unsigned int i = 0; i < 100000; ++i )
88 {
90 for ( unsigned int b = 0; b < 32; ++b )
91 val += index( n, b );
92 }
93 trace.info() << "- checksum = " << val << std::endl;
95
96 trace.beginBlock ( "Testing speed of look-up table version of indexInSetBits" );
97 srand( 0 );
98 unsigned int val2 = 0;
99 for ( unsigned int i = 0; i < 100000; ++i )
100 {
101 DGtal::uint32_t n = (DGtal::uint32_t) rand();
102 for ( unsigned int b = 0; b < 32; ++b )
103 val2 += Bits::indexInSetBits( n, b );
104 }
105 trace.info() << "- checksum = " << val2 << std::endl;
106 trace.endBlock();
107 ++nb; nbok += val == val2 ? 1 : 0;
108 return ( nb == nbok ) ? 0 : 1;
109}
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
boost::uint32_t uint32_t
unsigned 32-bit integer.
Definition: BasicTypes.h:63
boost::uint16_t uint16_t
unsigned 16-bit integer.
Definition: BasicTypes.h:61
boost::uint8_t uint8_t
unsigned 8-bit integer.
Definition: BasicTypes.h:59
Trace trace
Definition: Common.h:154
boost::uint64_t uint64_t
unsigned 64-bit integer.
Definition: BasicTypes.h:65
static unsigned int indexInSetBits(DGtal::uint8_t n, unsigned int b)
Definition: Bits.h:207
static unsigned int nbSetBits(T val)
Definition: Bits.h:130
srand(0)

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), DGtal::Bits::indexInSetBits(), DGtal::Trace::info(), DGtal::Bits::nbSetBits(), srand(), and DGtal::trace.