Welcome to mirror list, hosted at ThFree Co, Russian Federation.

nl-heap.h « include « rvtl « hhmm « synlm « contrib - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 31be6ed759037a2751c0a2a13bd2fdbf7d9ca7c5 (plain)
1
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
///////////////////////////////////////////////////////////////////////////////
//                                                                           //
// This file is part of ModelBlocks. Copyright 2009, ModelBlocks developers. //
//                                                                           //
//    ModelBlocks is free software: you can redistribute it and/or modify    //
//    it under the terms of the GNU General Public License as published by   //
//    the Free Software Foundation, either version 3 of the License, or      //
//    (at your option) any later version.                                    //
//                                                                           //
//    ModelBlocks 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 ModelBlocks.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                           //
//    ModelBlocks developers designate this particular file as subject to    //
//    the "Moses" exception as provided by ModelBlocks developers in         //
//    the LICENSE file that accompanies this code.                           //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

#ifndef _NL_MINHEAP_
#define _NL_MINHEAP_

#include "nl-safeids.h"

////////////////////////////////////////////////////////////////////////////////

template <class R, bool outrank(const R&, const R&)>
class Heap {
 private:
  Array<R> at;
  int      iNextToFill;
  //SafeArray1D<Id<int>,R> at;
  // Private specification methods...
  int      heapify ( unsigned int ) ;
 public:
  // Constructor / destructor methods...
  Heap<R,outrank> ( )                   : at(10),  iNextToFill(0) { }
  Heap<R,outrank> ( int i )             : at(i),   iNextToFill(0) { }
  Heap<R,outrank> ( int i, const R& r ) : at(i,r), iNextToFill(0) { }
  // Specification methods...
  void     init       ( int i )                { iNextToFill=0; at.init(i); }
  void     clear      ( )                      { iNextToFill=0; }
  unsigned int fixIncRank ( unsigned int i );
  unsigned int fixDecRank ( unsigned int i );
  R&       set        ( unsigned int i )       { return at.set(i-1); }
  void     enqueue    ( const R& r )           { set(iNextToFill+1)=r; fixIncRank(iNextToFill+1); iNextToFill++; }
  R        dequeueTop ( )                      { R r=get(1); iNextToFill--; set(1)=get(iNextToFill+1); set(iNextToFill+1)=R(); fixDecRank(1); return r; }
  ////R&       set        ( const Id<int>& i )       { return at.set(i); }
  R&       setTop     ( )                      { return at.set(1-1); }
  // Extraction methods...
  int      getSize    ( ) const                { return iNextToFill; }
  const R& getTop     ( ) const                { return at.get(1-1); }
  const R& get        ( unsigned int i ) const { return at.get(i-1); }
  ////const R& get        ( const Id<int>& i ) const { return at.get(i); }
  // Input / output methods...
  friend ostream& operator<< ( ostream& os, const Heap<R,outrank>& h ) { for(int i=0;i<h.iNextToFill;i++) os<<h.at.get(i)<<"\n"; return os; }
};

////////////////////////////////////////////////////////////////////////////////

template <class R, bool outrank(const R&, const R&)>
int Heap<R,outrank>::heapify ( unsigned int ind ) {
  // Find best of parent, left child, right child...
  unsigned int indBest = ind;
  indBest = (ind*2 <= (unsigned int)iNextToFill &&
	     outrank(get(ind*2),get(indBest)))
            ? ind*2 : indBest;
  indBest = (ind*2+1 <= (unsigned int)iNextToFill &&
	     outrank(get(ind*2+1),get(indBest)))
	    ? ind*2+1 : indBest;

  // If parent isn't best, restore heap property...
  if ( indBest != ind ) {
    // Swap heap elements...
    R rTemp      = get(ind);
    set(ind)     = get(indBest);
    set(indBest) = rTemp;
    // Recurse...
    return heapify(indBest);
  }
  else return ind;
}

template <class R, bool outrank(const R&, const R&)>
unsigned int Heap<R,outrank>::fixIncRank ( unsigned int ind ) {     //const R& rec ) {
  // If child outranks parent, restore heap property...
  if ( outrank(get(ind),get((ind==1)?1:ind/2)) ) {
    // Swap heap elements...
    R rTemp               = get((ind==1)?1:ind/2);
    set((ind==1)?1:ind/2) = get(ind);
    set(ind)              = rTemp;
    // Recurse on parent...
    return fixIncRank(ind/2);
  }
  else return ind;
}

template <class R, bool outrank(const R&, const R&)>
unsigned int Heap<R,outrank>::fixDecRank ( unsigned int ind ) {     //const R& rec ) {
  return heapify(ind);
}


////////////////////////////////////////////////////////////////////////////////

template <class R>
class MinHeap {
 private:
  Array<R> at;
  //SafeArray1D<Id<int>,R> at;
  // Private specification methods...
  int      minHeapify ( unsigned int ) ;
 public:
  // Constructor / destructor methods...
  MinHeap<R> ( )                   : at(10)  { }
  MinHeap<R> ( int i )             : at(i)   { }
  MinHeap<R> ( int i, const R& r ) : at(i,r) { }
  // Specification methods...
  void     init       ( int i )                  { at.init(i); }
  void     clear      ( )                        { at.clear(); }
  int      fixDecr    ( int i );
  int      fixIncr    ( int i );
  R&       set        ( unsigned int i )         { return at.set(i); }
  ////R&       set        ( const Id<int>& i )       { return at.set(i); }
  R&       setMin     ( )                        { return at.set(1-1); }
  // Extraction methods...
  int      getSize    ( ) const                  { return at.getSize(); }
  const R& getMin     ( ) const                  { return at.get(1-1); }
  const R& get        ( unsigned int i ) const   { return at.get(i); }
  ////const R& get        ( const Id<int>& i ) const { return at.get(i); }
};

////////////////////////////////////////////////////////////////////////////////

template <class R>
int MinHeap<R>::minHeapify ( unsigned int ind ) {
  // Find min of parent, left child, right child...
  unsigned int indMin = ind ;
  indMin = (ind*2 <= (unsigned int)at.getSize() &&
            at.get(ind*2-1).getScore() < at.get(indMin-1).getScore())
              ? ind*2 : indMin ;
  indMin = (ind*2+1 <= (unsigned int)at.getSize() &&
            at.get(ind*2+1-1).getScore() < at.get(indMin-1).getScore())
              ? ind*2+1 :indMin;

  // If parent isn't min, restore heap property...
  if ( indMin != ind ) {
    // Swap heap elements...
    R rTemp          = at.get(ind-1);
    at.set(ind-1)    = at.get(indMin-1);
    at.set(indMin-1) = rTemp;
    // Recurse...
    return minHeapify(indMin);
  }
  else return ind;
}

template <class R>
int MinHeap<R>::fixDecr ( int i ) {     //const R& rec ) {
  // If parent isn't min, restore heap property...
  if ( at.get((i+1)/2).getScore() > at.get(i).getScore() ) {
    // Swap heap elements...
    R rTemp         = at.get((i+1)/2);
    at.set((i+1)/2) = at.get(i);
    at.set(i)       = rTemp;
    // Recurse on parent...
    return fixDecr((i+1)/2);
  }
  else return i;
}

template <class R>
int MinHeap<R>::fixIncr ( int i ) {     //const R& rec ) {
  return minHeapify(i+1)-1;
}

#endif //_NL_HEAP_