Code

Super duper mega (fun!) commit: replaced encoding=utf-8 with fileencoding=utf-8 in...
[inkscape.git] / src / 2geom / choose.h
1 /**
2  * \file choose.h
3  * \brief  \todo brief description
4  *
5  * Copyright 2006 Nathan Hurst <njh@mail.csse.monash.edu.au>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it either under the terms of the GNU Lesser General Public
9  * License version 2.1 as published by the Free Software Foundation
10  * (the "LGPL") or, at your option, under the terms of the Mozilla
11  * Public License Version 1.1 (the "MPL"). If you do not alter this
12  * notice, a recipient may use your version of this file under either
13  * the MPL or the LGPL.
14  *
15  * You should have received a copy of the LGPL along with this library
16  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  * You should have received a copy of the MPL along with this library
19  * in the file COPYING-MPL-1.1
20  *
21  * The contents of this file are subject to the Mozilla Public License
22  * Version 1.1 (the "License"); you may not use this file except in
23  * compliance with the License. You may obtain a copy of the License at
24  * http://www.mozilla.org/MPL/
25  *
26  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28  * the specific language governing rights and limitations.
29  *
30  */
32 #ifndef _CHOOSE_H
33 #define _CHOOSE_H
34 #include <vector>
36 // XXX: Can we keep only the left terms easily? 
37 // this would more than halve the array
38 // row index becomes n2 = n/2, row2 = n2*(n2+1)/2, row = row2*2+(n&1)?n2:0
39 // we could also leave off the ones
41 template <typename T>
42 T choose(unsigned n, unsigned k) {
43     static std::vector<T> pascals_triangle;
44     static unsigned rows_done = 0;
45     // indexing is (0,0,), (1,0), (1,1), (2, 0)...
46     // to get (i, j) i*(i+1)/2 + j
47     if(/*k < 0 ||*/ k > n) return 0;
48     if(rows_done <= n) {// we haven't got there yet
49         if(rows_done == 0) {
50             pascals_triangle.push_back(1);
51             rows_done = 1;
52         }
53         while(rows_done <= n) {
54             unsigned p = pascals_triangle.size() - rows_done;
55             pascals_triangle.push_back(1);
56             for(unsigned i = 0; i < rows_done-1; i++) {
57                 pascals_triangle.push_back(pascals_triangle[p] 
58                                            + pascals_triangle[p+1]);
59                 p++;
60             }
61             pascals_triangle.push_back(1);
62             rows_done ++;
63         }
64     }
65     unsigned row = (n*(n+1))/2;
66     return pascals_triangle[row+k];
67 }
69 // Is it faster to store them or compute them on demand?
70 /*template <typename T>
71 T choose(unsigned n, unsigned k) {
72         T r = 1;
73         for(unsigned i = 1; i <= k; i++)
74                 r = (r*(n-k+i))/i;
75         return r;
76         }*/
78 #endif
80 /*
81   Local Variables:
82   mode:c++
83   c-file-style:"stroustrup"
84   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
85   indent-tabs-mode:nil
86   fill-column:99
87   End:
88 */
89 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :