Repository navigation
Expand file tree
/
Copy pathUtils.cs
More file actions
159 lines (140 loc) · 4.23 KB
/
Copy pathUtils.cs
File metadata and controls
159 lines (140 loc) · 4.23 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Bigtree.Algorithm.CRFLite
{
public class QueueElement
{
public Node node;
public QueueElement next;
public double fx;
public double gx;
};
public class Heap
{
public int capacity;
public int elem_size; //size of elem_list
public int size; // size of elem_ptr_list
public List<QueueElement> elem_ptr_list;
public List<QueueElement> elem_list;
};
public class BaseUtils
{
public const double eps = 1e-7;
public const int MINUS_LOG_EPSILON = 13;
public const int DEFAULT_CRF_MAX_WORD_NUM = 100;
public const int MODEL_TYPE_NORM = 100;
public const int RETURN_INVALIDATED_FEATURE = -8;
public const int RETURN_HEAP_SIZE_TOO_BIG = -7;
public const int RETURN_INSERT_HEAP_FAILED = -6;
public const int RETURN_EMPTY_FEATURE = -5;
public const int RETURN_INVALIDATED_PARAMETER = -4;
public const int RETURN_WRONG_STATUS = -3;
public const int RETURN_TOO_LONG_WORD = -2;
public const int RETURN_UNKNOWN = -1;
public const int RETURN_SUCCESS = 0;
public static Heap heap_init(int max_size)
{
Heap H;
H = new Heap();
H.capacity = max_size;
H.size = 0;
H.elem_size = 0;
H.elem_ptr_list = new List<QueueElement>(max_size + 1);
H.elem_list = new List<QueueElement>(max_size + 1);
for (var z = 0; z < max_size; z++)
{
H.elem_list.Add(new QueueElement());
H.elem_ptr_list.Add(null);
}
H.elem_list[0].fx = double.MinValue;
H.elem_ptr_list.Add(H.elem_list[0]);
return H;
}
public static QueueElement allc_from_heap(Heap H)
{
if (H.elem_size >= H.capacity)
{
return null;
}
else
{
return H.elem_list[++H.elem_size];
}
}
public static int heap_insert(QueueElement qe, Heap H)
{
if (H.size >= H.capacity)
{
return BaseUtils.RETURN_HEAP_SIZE_TOO_BIG;
}
var i = ++H.size;
while (i != 1 && H.elem_ptr_list[i / 2].fx > qe.fx)
{
H.elem_ptr_list[i] = H.elem_ptr_list[i / 2]; //此时i还没有进行i/2操作
i /= 2;
}
H.elem_ptr_list[i] = qe;
return 0;
}
public static QueueElement heap_delete_min(Heap H)
{
var min_elem = H.elem_ptr_list[1]; //堆是从第1号元素开始的
var last_elem = H.elem_ptr_list[H.size--];
int i = 1, ci = 2;
while (ci <= H.size)
{
if (ci < H.size && H.elem_ptr_list[ci].fx > H.elem_ptr_list[ci + 1].fx)
{
ci++;
}
if (last_elem.fx <= H.elem_ptr_list[ci].fx)
{
break;
}
H.elem_ptr_list[i] = H.elem_ptr_list[ci];
i = ci;
ci *= 2;
}
H.elem_ptr_list[i] = last_elem;
return min_elem;
}
public static bool is_heap_empty(Heap H)
{
return H.size == 0;
}
public static void heap_reset(Heap H)
{
if (H != null)
{
H.size = 0;
H.elem_size = 0;
}
}
public static double logsumexp(double x, double y, bool flg)
{
if (flg)
{
return y; // init mode
}
double vmin;
double vmax;
if (x > y)
{
vmin = y;
vmax = x;
}
else
{
vmin = x;
vmax = y;
}
if (vmax > vmin + MINUS_LOG_EPSILON)
{
return vmax;
}
return vmax + Math.Log(Math.Exp(vmin - vmax) + 1.0);
}
}
}