choose.h revision d02f89424319afe28fe2cc6696c28108448de3ac
2N/A/*
2N/A * choose.h
2N/A *
2N/A * Copyright 2006 Nathan Hurst <njh@mail.csse.monash.edu.au>
2N/A *
2N/A * This library is free software; you can redistribute it and/or
2N/A * modify it either under the terms of the GNU Lesser General Public
2N/A * License version 2.1 as published by the Free Software Foundation
2N/A * (the "LGPL") or, at your option, under the terms of the Mozilla
2N/A * Public License Version 1.1 (the "MPL"). If you do not alter this
2N/A * notice, a recipient may use your version of this file under either
2N/A * the MPL or the LGPL.
2N/A *
2N/A * You should have received a copy of the LGPL along with this library
2N/A * in the file COPYING-LGPL-2.1; if not, write to the Free Software
2N/A * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2N/A * You should have received a copy of the MPL along with this library
2N/A * in the file COPYING-MPL-1.1
2N/A *
2N/A * The contents of this file are subject to the Mozilla Public License
2N/A * Version 1.1 (the "License"); you may not use this file except in
2N/A * compliance with the License. You may obtain a copy of the License at
2N/A * http://www.mozilla.org/MPL/
2N/A *
2N/A * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
2N/A * OF ANY KIND, either express or implied. See the LGPL or the MPL for
2N/A * the specific language governing rights and limitations.
2N/A *
2N/A */
2N/A
2N/A#ifndef _CHOOSE_H
2N/A#define _CHOOSE_H
2N/A
2N/A// XXX: Can we keep only the left terms easily?
2N/A// this would more than halve the array
2N/A// row index becomes n2 = n/2, row2 = n2*(n2+1)/2, row = row2*2+(n&1)?n2:0
2N/A// we could also leave off the ones
2N/A
2N/Atemplate <typename T>
2N/AT choose(unsigned n, unsigned k) {
2N/A static std::vector<T> pascals_triangle;
2N/A static unsigned rows_done = 0;
2N/A // indexing is (0,0,), (1,0), (1,1), (2, 0)...
2N/A // to get (i, j) i*(i+1)/2 + j
2N/A if(k < 0 || k > n) return 0;
2N/A if(rows_done <= n) {// we haven't got there yet
2N/A if(rows_done == 0) {
2N/A pascals_triangle.push_back(1);
2N/A rows_done = 1;
2N/A }
2N/A while(rows_done <= n) {
2N/A unsigned p = pascals_triangle.size() - rows_done;
2N/A pascals_triangle.push_back(1);
2N/A for(unsigned i = 0; i < rows_done-1; i++) {
2N/A pascals_triangle.push_back(pascals_triangle[p]
2N/A + pascals_triangle[p+1]);
2N/A p++;
2N/A }
2N/A pascals_triangle.push_back(1);
2N/A rows_done ++;
2N/A }
2N/A }
2N/A unsigned row = (n*(n+1))/2;
2N/A return pascals_triangle[row+k];
2N/A}
2N/A
2N/A// Is it faster to store them or compute them on demand?
2N/A/*template <typename T>
2N/AT choose(unsigned n, unsigned k) {
2N/A T r = 1;
2N/A for(unsigned i = 1; i <= k; i++)
2N/A r = (r*(n-k+i))/i;
2N/A return r;
2N/A }*/
2N/A
2N/A#endif
2N/A
2N/A/*
2N/A Local Variables:
2N/A mode:c++
2N/A c-file-style:"stroustrup"
2N/A c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2N/A indent-tabs-mode:nil
2N/A fill-column:99
2N/A End:
2N/A*/
2N/A// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
2N/A