alloc.cpp
Go to the documentation of this file.
00001 // ========================================================================================
00002 //  ApproxMVBB
00003 //  Copyright (C) 2014 by Gabriel Nützi <nuetzig (at) imes (d0t) mavt (d0t) ethz (døt) ch>
00004 //
00005 //  This Source Code Form is subject to the terms of the Mozilla Public
00006 //  License, v. 2.0. If a copy of the MPL was not distributed with this
00007 //  file, You can obtain one at http://mozilla.org/MPL/2.0/.
00008 // ========================================================================================
00009 
00010 
00011 #include <ApproxMVBB/Diameter/Utils/alloc.hpp>
00012 
00013 namespace ApproxMVBB{
00014 namespace Diameter{
00015 
00016 void *_AllocateListOfPoints( const int n, const int dim )
00017 {
00018   void *b = NULL;
00019   double **list, **dd;
00020   double *d;
00021   int i;
00022 
00023   if ( n <= 0 || dim <= 0 ) return(NULL);
00024 
00025   b = (void*)malloc( n         * sizeof( double* )
00026                      + n * dim * sizeof( double ) );
00027   if ( b == (void *)NULL )  return( (void *)NULL );
00028 
00029   dd  = list = (double **)b;
00030   dd += n;
00031 
00032   d   = (double*)dd;
00033   for (i=0; i<n; i++, d+=dim ) list[i] = d;
00034 
00035   return( b );
00036 }
00037 
00038 void *_AllocateListOfSegments( const int n )
00039 {
00040   void *b = NULL;
00041   TypeSegment *d;
00042   int i;
00043 
00044 
00045   if ( n <= 0  ) return(NULL);
00046 
00047   b = (void*)malloc( n * sizeof( TypeSegment ) );
00048 
00049   if ( b == (void *)NULL )  return( (void *)NULL );
00050 
00051   d = (TypeSegment *)b;
00052   for ( i=0; i<n; i++ ) {
00053     d[i].extremity1 = (double*)NULL;
00054     d[i].extremity2 = (double*)NULL;
00055     d[i].squareDiameter = 0.0;
00056     d[i].reduction_mode = 0;
00057   }
00058 
00059   return( b );
00060 }
00061 
00062 #define _NALLOC_ 20
00063 
00064 int _AddSegmentToList( TypeSegment *s, TypeListOfSegments *list )
00065 {
00066   TypeSegment *d;
00067 
00068   if ( list->nalloc <= 0 )
00069     list->n = list->nalloc = 0;
00070 
00071   if ( list->n >= list->nalloc ) {
00072 
00073     d = (TypeSegment *)_AllocateListOfSegments( list->nalloc + _NALLOC_ );
00074     if ( d == NULL ) {
00075       return( 0 );
00076     }
00077 
00078     if ( list->nalloc > 0 ) {
00079       memcpy( d, list->seg, list->nalloc*sizeof(TypeSegment) );
00080       free( list->seg );
00081     }
00082 
00083     list->nalloc += _NALLOC_;
00084     list->seg = d;
00085 
00086   }
00087 
00088   list->seg[list->n] = *s;
00089   list->n++;
00090 
00091   return( 1 );
00092 }
00093 
00094 }
00095 }
00096 


asr_approx_mvbb
Author(s): Gassner Nikolai
autogenerated on Sat Jun 8 2019 20:21:49