w_window1d.hh 3.4 KB
Newer Older
Nicolas Burrus's avatar
Nicolas Burrus committed
1
// Copyright (C) 2001, 2002, 2003  EPITA Research and Development Laboratory
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
//
// This file is part of the Olena Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License version 2 as published by the
// Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.
//
// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

#ifndef OLENA_CORE_W_WINDOW1D_HH
# define OLENA_CORE_W_WINDOW1D_HH

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
31 32
# include <oln/core/internal/w_window.hh>
# include <oln/core/accum.hh>
33 34 35 36 37
# include <oln/core/winiter.hh>
# include <oln/core/winneighb.hh>
# include <oln/core/dpoint1d.hh>
# include <oln/core/window1d.hh>
# include <algorithm>
38 39 40

namespace oln {

Nicolas Burrus's avatar
Nicolas Burrus committed
41 42
  template<class T, class Exact = mlc::final>
  class w_window1d : public internal::_w_window< 1, T, typename mlc::exact_vt<w_window1d<T, Exact>, Exact>::ret >
43
  {
Nicolas Burrus's avatar
Nicolas Burrus committed
44
    typedef internal::_w_window< 1, T, typename mlc::exact_vt<w_window1d<T, Exact>, Exact>::ret > super;
45
  public:
46
   
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    typedef w_window1d self;

    typedef winiter< self >   iter;
    typedef winneighb< self > neighb;

    w_window1d<T>& add(coord col, T weight)
    {
      return add(dpoint1d(col), weight);
    }

    w_window1d(): super(), _delta(0) {}
    w_window1d(unsigned size) : super(size), _delta(0) {}

    w_window1d<T>& add(const dpoint1d& dp, T weight)
    {
      if (weight == 0)		// Don't add 0 weighted entries
	return *this;

      super::add(dp, weight);
      _delta(abs(dp.col()));
      return *this;
    }

Nicolas Burrus's avatar
Nicolas Burrus committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    T& set(const dpoint1d& dp, T weight)
    {
      // if the dp exists, return a ref to the existing entry
      for (unsigned i = 0; i < card(); ++i)
	if (_dp[i] == dp)
	  {
	    _w[i] = weight;
	    return _w[i];
	  }

      // otherwise, create new entry
      add(dp, weight);
      return _w.back();
    }

    T& set(coord col)
    {
      return set(dpoint1d(col));
    }

90 91 92 93 94 95 96 97 98 99 100 101
    coord delta() const
    {
      return _delta;
    }

    self operator-() const
    {
      self win(*this);
      win.sym();
      return win;
    }

102 103 104
    static std::string name() { return std::string("w_window1d<") 
				       + T::name() + ","
				       + Exact::name() + ">"; }
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  private:
    max_accumulator<coord> _delta;
  };


  template<class T>
  w_window1d<T> mk_w_win_from_win(T weight, const window1d& win)
  {
    w_window1d<T> w_win(win.card());
    for (unsigned i = 0; i < win.card(); ++i)
      w_win.add(win.dp(i), weight);
    return w_win;
  }

} // end of oln

#endif // ! OLENA_CORE_W_WINDOW1D_HH