casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Combinatorics.h
Go to the documentation of this file.
00001 //# Smooth.h: smooth vectors and arrays 
00002 //# Copyright (C) 2010 by ESO (in the framework of the ALMA collaboration)
00003 //# Copyright (C) 1996,1997,1998,1999,2000,2001
00004 //# Associated Universities, Inc. Washington DC, USA.
00005 //#
00006 //# This library is free software; you can redistribute it and/or modify it
00007 //# under the terms of the GNU Library General Public License as published by
00008 //# the Free Software Foundation; either version 2 of the License, or (at your
00009 //# option) any later version.
00010 //#
00011 //# This library is distributed in the hope that it will be useful, but WITHOUT
00012 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00014 //# License for more details.
00015 //#
00016 //# You should have received a copy of the GNU Library General Public License
00017 //# along with this library; if not, write to the Free Software Foundation,
00018 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00019 //#
00020 //# Correspondence concerning AIPS++ should be addressed as follows:
00021 //#        Internet email: aips2-request@nrao.edu.
00022 //#        Postal address: AIPS++ Project Office
00023 //#                        National Radio Astronomy Observatory
00024 //#                        520 Edgemont Road
00025 //#                        Charlottesville, VA 22903-2475 USA
00026 //#
00027 //# $Id: Combinatorics.h 21116 2011-07-21 11:23:15Z gervandiepen $
00028 
00029 #ifndef SCIMATH_COMBINATORICS_H
00030 #define SCIMATH_COMBINATORICS_H
00031 
00032 
00033 //# Includes
00034 #include <casa/aips.h>
00035 #include <casa/namespace.h>
00036 #include <casa/Arrays/Vector.h>
00037 #include <casa/OS/Mutex.h>
00038 
00039 namespace casa { //# NAMESPACE CASA - BEGIN
00040 
00041 // <summary>
00042 // Combinatorics related functions.
00043 // </summary>
00044 
00045 // <use visibility=export>
00046 
00047 //# <author>Dave Mehringer</author>
00048 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00049 // </reviewed>
00050 
00051 // <prerequisite>
00052 //   <li> <linkto class="Vector">Vector</linkto>
00053 //   <li> <linkto class="Array">Array</linkto>
00054 // </prerequisite>
00055 
00056 // <etymology>
00057 // self-explanatory
00058 // </etymology>
00059 
00060 // <synopsis>
00061 // Various factorial and combinatorical functions.
00062 // </synopsis>
00063 
00064 // <motivation>
00065 // Binomial coefficients needed for Images/ImageProfileFitter
00066 // </motivation>
00067 
00068 class Combinatorics {
00069   
00070   public:
00071  
00072   // Get n!
00073   static uInt factorial(const uInt n)
00074   {
00075     //# This test is thread-safe.
00076     if (n >= _factorialCacheSize) fillCache(n);
00077     return _factorialCache[n];
00078   }
00079   
00080   // "n choose k" = n!/(k!(n-k)!)
00081   // Exception is thrown if k > n.
00082   static uInt choose(const uInt n, const uInt k);
00083 
00084   private:
00085   static void fillCache(const uInt n);
00086 
00087   static Vector<uInt> _factorialCache;
00088   static volatile uInt _factorialCacheSize; //# volatile for double checked lock
00089   static Mutex theirMutex;
00090 };
00091 } //# NAMESPACE CASA - END
00092 
00093 #endif
00094