Code

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