Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

more/gen/binary_heap.h

Go to the documentation of this file.
00001 //  Copyright (C) 2001  Petter Urkedal (petter.urkedal@matfys.lth.se)
00002 //
00003 //  This file is free software; you can redistribute it and/or modify
00004 //  it under the terms of the GNU General Public License as published by
00005 //  the Free Software Foundation; either version 2 of the License, or
00006 //  (at your option) any later version.
00007 //
00008 //  This file is distributed in the hope that it will be useful,
00009 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //  GNU General Public License for more details.
00012 //
00013 //  You should have received a copy of the GNU General Public License
00014 //  along with this program; if not, write to the Free Software
00015 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016 //
00017 //  As a special exception, you may use this file as part of a free
00018 //  software library without restriction.  Specifically, if other files
00019 //  instantiate templates or use macros or inline functions from this
00020 //  file, or you compile this file and link it with other files to
00021 //  produce an executable, this file does not by itself cause the
00022 //  resulting executable to be covered by the GNU General Public
00023 //  License.  This exception does not however invalidate any other
00024 //  reasons why the executable file might be covered by the GNU General
00025 //  Public License.
00026 //
00027 //  $Id: binary_heap.h,v 1.1 2002/05/30 18:01:37 petter_urkedal Exp $
00028 
00029 
00030 #ifndef MORE_GEN_BINARY_HEAP_H
00031 #define MORE_GEN_BINARY_HEAP_H
00032 
00033 #include <algorithm>
00034 #include <functional>
00035 
00036 namespace more {
00037 namespace gen {
00038 
00039   template< typename T, typename Prec = std::less<T> >
00040     struct binary_heap {
00041         typedef std::size_t size_type;
00042         typedef std::ptrdiff_t difference;
00043 
00044         binary_heap() : n(0), n_max(16), p(new T[n_max] - 1) {}
00045         ~binary_heap() { delete[] (p + 1); }
00046 
00047         void insert(T const& x);
00048         T const& front() const { return p[1]; }
00049         void pop_front();
00050         bool empty() const { return n == 0; }
00051       private:
00052         void expand();
00053         size_type n;
00054         size_type n_max;
00055         T* p;
00056         Prec prec;
00057     };
00058 
00059   template<typename T, typename Prec>
00060     inline void
00061     binary_heap<T, Prec>::insert(T const& x) {
00062         size_type i = ++n;
00063         if (n >= n_max)
00064             expand();
00065         while (i > 1 && prec(x, p[i/2])) {
00066             p[i] = p[i/2];
00067             i /= 2;
00068         }
00069         p[i] = x;
00070     }
00071 
00072   template<typename T, typename Prec>
00073     inline void
00074     binary_heap<T, Prec>::expand() {
00075         T* old = p;
00076         n_max *= 2;
00077         p = new T[n_max] - 1;
00078         std::copy(old + 1, old + n + 1, p + 1);
00079         delete[] (old + 1);
00080     }
00081 
00082   template<typename T, typename Prec>
00083     inline void
00084     binary_heap<T, Prec>::pop_front() {
00085         size_type i = 1;
00086         for (;;) {
00087             size_type j = i*2;
00088             if (j >= n) {
00089                 p[i] = p[n];
00090                 break;
00091             }
00092             if (prec(p[j + 1], p[j]))
00093                 ++j;
00094             if (!prec(p[j], p[n])) {
00095                 p[i] = p[n];
00096                 break;
00097             }
00098             p[i] = p[j];
00099             i = j;
00100         }
00101         --n;
00102     }
00103 }}
00104 
00105 #endif

Generated on Sat Sep 7 19:11:10 2002 for more with Doxygen 1.2.13.1. Doxygen 1.2.13.1 is written and copyright 1997-2002 by Dimitri van Heesch.