mathematical 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -4
- data/Rakefile +2 -2
- data/ext/mathematical/extconf.rb +2 -0
- data/ext/mathematical/mtex2MML/src/color_definitions.c +45 -0
- data/ext/mathematical/mtex2MML/src/deps/uthash/utarray.h +232 -0
- data/ext/mathematical/mtex2MML/src/deps/uthash/uthash.h +958 -0
- data/ext/mathematical/mtex2MML/src/deps/uthash/utlist.h +757 -0
- data/ext/mathematical/mtex2MML/src/deps/uthash/utstring.h +393 -0
- data/ext/mathematical/mtex2MML/src/lex.yy.c +4642 -3467
- data/ext/mathematical/mtex2MML/src/mtex2MML.h +31 -31
- data/ext/mathematical/mtex2MML/src/mtex2MML_ruby.c +2299 -0
- data/ext/mathematical/mtex2MML/src/parse_extras.c +223 -224
- data/ext/mathematical/mtex2MML/src/parse_extras.h +59 -58
- data/ext/mathematical/mtex2MML/src/string_extras.c +17 -194
- data/ext/mathematical/mtex2MML/src/string_extras.h +10 -28
- data/ext/mathematical/mtex2MML/src/y.tab.c +3622 -3063
- data/ext/mathematical/mtex2MML/src/y.tab.h +440 -336
- data/lib/mathematical/corrections.rb +0 -18
- data/lib/mathematical/version.rb +1 -1
- data/test/mathematical/corrections_test.rb +0 -40
- data/test/mathematical/fixtures/performance/big_file.text +8 -20
- data/test/mathematical/maliciousness_test.rb +12 -2
- data/test/mathematical/mathjax_test.rb +16 -0
- metadata +10 -4
- data/ext/mathematical/mtex2MML/src/stack.c +0 -80
- data/ext/mathematical/mtex2MML/src/stack.h +0 -101
@@ -2,21 +2,7 @@ module Mathematical
|
|
2
2
|
module Corrections
|
3
3
|
|
4
4
|
def apply_corrections(maths)
|
5
|
-
maths = adjust_alignment(maths)
|
6
|
-
maths = adjust_frac(maths)
|
7
5
|
maths = adjust_lt_gt(maths)
|
8
|
-
maths = adjust_limits(maths)
|
9
|
-
end
|
10
|
-
|
11
|
-
# `{align}` *should* be valid, according to AMS-Latex, but it seems itex@1.5.1 does not like it.
|
12
|
-
def adjust_alignment(maths)
|
13
|
-
maths = maths.gsub(/\\begin\{align\}/, "\\begin{aligned}")
|
14
|
-
maths.gsub(/\\end\{align\}/, "\\end{aligned}")
|
15
|
-
end
|
16
|
-
|
17
|
-
# seems like KaTeX/MathJax supports \frac\d\d, but itex does not
|
18
|
-
def adjust_frac(maths)
|
19
|
-
maths.gsub(/\\frac(\d)(\d)/, '\frac{\1}{\2}')
|
20
6
|
end
|
21
7
|
|
22
8
|
# from the itex website:
|
@@ -26,9 +12,5 @@ module Mathematical
|
|
26
12
|
def adjust_lt_gt(maths)
|
27
13
|
maths.gsub(/</, '\lt').gsub(/>/, '\gt')
|
28
14
|
end
|
29
|
-
|
30
|
-
def adjust_limits(maths)
|
31
|
-
maths.gsub(/\\int\\limits_/, '\int_')
|
32
|
-
end
|
33
15
|
end
|
34
16
|
end
|
data/lib/mathematical/version.rb
CHANGED
@@ -4,41 +4,6 @@ class Mathematical::CorrectionsTest < Test::Unit::TestCase
|
|
4
4
|
|
5
5
|
def setup
|
6
6
|
@render = Mathematical::Render.new
|
7
|
-
@slash_maths = '''
|
8
|
-
$$
|
9
|
-
\dot{x} & = \sigma(y-x) \\\\
|
10
|
-
\dot{y} & = \rho x - y - xz \\\\
|
11
|
-
\dot{z} & = -\beta z + xy
|
12
|
-
$$
|
13
|
-
'''
|
14
|
-
|
15
|
-
@aligned_maths = '''
|
16
|
-
$$
|
17
|
-
\begin{align}
|
18
|
-
\dot{x} & = \sigma(y-x) \\\\
|
19
|
-
\dot{y} & = \rho x - y - xz \\\\
|
20
|
-
\dot{z} & = -\beta z + xy
|
21
|
-
\end{align}
|
22
|
-
$$
|
23
|
-
'''
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_expect_slashes_failures
|
27
|
-
@render.expects(:apply_corrections).returns(@slash_maths)
|
28
|
-
|
29
|
-
printed = capture_stderr do
|
30
|
-
@render.render @slash_maths
|
31
|
-
end
|
32
|
-
assert_match /Failed to parse mtex/, printed
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_adjust_aligned
|
36
|
-
assert_no_match /align\}/, @render.apply_corrections(@aligned_maths)
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_adjust_fracs
|
40
|
-
simple_frac = '$\frac25$'
|
41
|
-
assert_match /frac\{2\}\{5\}/, @render.apply_corrections(simple_frac)
|
42
7
|
end
|
43
8
|
|
44
9
|
def test_adjust_lt_gt
|
@@ -48,9 +13,4 @@ $$
|
|
48
13
|
simple_gt = '$|q| > 1$'
|
49
14
|
assert_match /|q| \\gt 1/, @render.apply_corrections(simple_gt)
|
50
15
|
end
|
51
|
-
|
52
|
-
def test_adjust_limits
|
53
|
-
integral = '\int\limits_{-\pi}^{\pi}'
|
54
|
-
assert_equal '\int_{-\pi}^{\pi}', @render.apply_corrections(integral)
|
55
|
-
end
|
56
16
|
end
|
@@ -103,9 +103,8 @@ compactifying divisors of $\tilde{X}$. The principal result of \cite{HZ} was tha
|
|
103
103
|
We show $j_{\ast} C_n^c = T_n^c$ (Proposition~\ref{CnTn}), and hence the Hirzebruch-Zagier theorem follows easily from Theorem~\ref{FMHZ-main} above, see Theorem~\ref{HZTheorem}.
|
104
104
|
|
105
105
|
The main work in \cite{HZ} was to show that the generating function
|
106
|
-
|
107
|
-
|
108
|
-
\]
|
106
|
+
|
107
|
+
|
109
108
|
for the intersection numbers in $\tilde{X}$ of $T^c_n$ with a fixed $T_m$ is a modular form of weight $2$. The Hirzebruch-Zagier proof of the modularity of $F$ was a remarkable synthesis of algebraic geometry, combinatorics, and modular forms. They explicitly computed the intersection numbers $T_n^c \cdot T_m$ as the sum of two terms, $T_n^c \cdot T_m = (T_n \cdot T_m)_X + ({T}_n \cdot {T}_m)_{\infty}$, where $(T_n \cdot T_m)_X $ is the geometric intersection number of $T_n$ and $T_m$ in the interior of $X$ and $({T}_n \cdot {T}_m)_{\infty}$ which they called the ``contribution from infinity''. They then proved both generating functions $\sum_{n=0}^{\infty} (T_n \cdot T_m)_X q^n$ and $\sum_{n=0}^{\infty} (T_n \cdot T_m)_{\infty} q^n$ are the holomorphic parts of two non-holomorphic forms $F_X$ and $F_{\infty}$ with the {\it same} non-holomorphic part (with opposite signs). Hence combining these two forms gives $F(\tau)$.
|
110
109
|
|
111
110
|
We recover this feature of the original Hirzebruch-Zagier proof via \eqref{mappingconelift} with $C=C_m$. The first term on the right hand side of \eqref{mappingconelift} was studied in the thesis of the first author of this paper \cite{FCompo} and gives the interior intersections $(T_n \cdot T_m)_X$ encoded in $F_X$. So via Theorem~\ref{FMHZ-main} the second term on the right hand side of \eqref{mappingconelift} must match the boundary contribution $F_{\infty}$ in \cite{HZ}, that is, we obtain
|
@@ -677,13 +676,7 @@ glueing homeomorphism $f \in SL(2,\Z)$ on $H_1(T^2, \Z)$ for linking numbers fo
|
|
677
676
|
|
678
677
|
This immediately leads to an explicit formula for the numbers $Lk(\partial C_n, \partial C_m)$. Using Lemma~\ref{LemmaB} we obtain
|
679
678
|
|
680
|
-
|
681
|
-
Let $g = (f^{-1} - I)^{-1}$. Then
|
682
|
-
\[
|
683
|
-
Lk( (\partial C_n)_P, (\partial C_m)_P) = \sum_{ \substack{x\in \G_M \back \mathcal{L}_W \\ (x,x)=2n}} \sum_{\substack{x'\in \G_M \back \mathcal{L}_W \\ (x,x)=2m}} (\min_{\lambda \in \Lambda_W} {\hspace{-5pt}'}
|
684
|
-
|(\lambda,x)|) (\min_{\mu \in \Lambda_W} {\hspace{-5pt}'}
|
685
|
-
|(\mu,x')|) \langle g(Jx),Jx' \rangle.
|
686
|
-
\]
|
679
|
+
|
687
680
|
Here $Jx$ is properly oriented primitive vector in $\Lambda_W$ such that $(Jx,x)=0$.
|
688
681
|
\end{theorem}
|
689
682
|
|
@@ -1244,10 +1237,7 @@ and $\tilde{\psi'}_{0,1}(n)$ has bidegree $(0,1)$ (here we use the obvious base/
|
|
1244
1237
|
|
1245
1238
|
\begin{lemma}\label{LemmaA}
|
1246
1239
|
Under the hypothesis on $\eta$ in Proposition~\ref{finalintegral} we have
|
1247
|
-
|
1248
|
-
\int_{A_n} \eta = \sum_{ \substack{x\in \G_M \back \mathcal{L}_W \\ (x,x)=2n}} \min_{\la \in \Lambda_W} {\hspace{-5pt}'} |(\la,x)| \int_{a_x} \eta
|
1249
|
-
\]
|
1250
|
-
\end{lemma}
|
1240
|
+
|
1251
1241
|
|
1252
1242
|
\begin{proof}
|
1253
1243
|
We use Lemma~\ref{LemmaB}. Write $\eta = d \omega$ for some $1$-form $\omega$ which by the support condition on $\eta$ is closed in $F_n$. Since $c_{x+ku}$ and $c_x$ are parallel hence homologous circles in $F_x$, we see $\int_{a_{x+ku}} \eta = \int_{c_{x+ku}} \omega = \int_{c_x} \omega = \int_{a_x} \eta$.
|
@@ -1262,9 +1252,8 @@ Proposition~\ref{finalintegral} will now follow from
|
|
1262
1252
|
|
1263
1253
|
\begin{proposition}
|
1264
1254
|
Under the hypothesis on $\eta$ in Proposition~\ref{finalintegral}, we have for any positive length vector $x \in \calL_W$
|
1265
|
-
|
1266
|
-
|
1267
|
-
\]
|
1255
|
+
|
1256
|
+
|
1268
1257
|
\end{proposition}
|
1269
1258
|
|
1270
1259
|
|
@@ -1296,9 +1285,8 @@ Here $\omega_3$ is the $dw_3$ component of $\omega$ and we used that $\partial D
|
|
1296
1285
|
of $\omega$ over (homologous) horizontal translates of the cycle $c_{e_2}$. But the restriction of $\omega$ to $F_x$ is closed so $\int_{c_{e_2}} \omega(0,w_2,w_3)$ is independent of $w_2$ and the last integral becomes
|
1297
1286
|
$\left( \int_{T^2/ c_{e_2}} dw_2 \right)\left( \int_{c_{e_2}} \omega \right)e^{- \pi \mu^2}$.
|
1298
1287
|
But $\int_{c_{e_2}} \omega = \int_{A_{e_2}} \eta$. The proposition is then a consequence of
|
1299
|
-
|
1300
|
-
|
1301
|
-
\]
|
1288
|
+
|
1289
|
+
|
1302
1290
|
which follows from the fact that the map $W \to R$ given by $w \mapsto (w,e_2)$ induces an isomorphism $T^2/ \partial C_{e_2} \simeq R / (\min_{\la \in \Lambda_W}'|(\la,e_2)|)\Z$.
|
1303
1291
|
|
1304
1292
|
\end{proof}
|
@@ -5,8 +5,9 @@ class Mathematical::MaliciousnessTest < Test::Unit::TestCase
|
|
5
5
|
def test_it_does_not_error_on_unrecognized_commands
|
6
6
|
render = Mathematical::Render.new
|
7
7
|
output = nil
|
8
|
-
# In
|
9
|
-
assert_nothing_raised { output = render.render('
|
8
|
+
# In mtex2MML, we raise a ParseError, but Mathematical suppresses it and returns the string.
|
9
|
+
assert_nothing_raised { output = render.render('$not__thisisnotreal$') }
|
10
|
+
assert output == '$not__thisisnotreal$'
|
10
11
|
end
|
11
12
|
|
12
13
|
def test_it_does_not_blow_up_on_bad_arguments
|
@@ -76,4 +77,13 @@ class Mathematical::MaliciousnessTest < Test::Unit::TestCase
|
|
76
77
|
render = Mathematical::Render.new.render("No dollars")
|
77
78
|
end
|
78
79
|
end
|
80
|
+
|
81
|
+
def test_it_returns_unmodified_string_for_max_parsing
|
82
|
+
render = Mathematical::Render.new
|
83
|
+
output = nil
|
84
|
+
# Much like above, this fails in mtx2MML, but should do nothing here
|
85
|
+
text = '$\Huge \sqrt\sqrt\sqrt\sqrt\sqrt\sqrt\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}'
|
86
|
+
assert_nothing_raised { output = render.render(text) }
|
87
|
+
assert output == text
|
88
|
+
end
|
79
89
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class Mathematical::MathJaxTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
render = Mathematical::Render.new
|
6
|
+
|
7
|
+
MATHJAX_TEST_TEST_DIR = File.join('ext', 'mathematical', 'mtex2MML', 'test', 'fixtures', 'MathJax')
|
8
|
+
MATHJAX_TEST_TEX_DIR = File.join(MATHJAX_TEST_TEST_DIR, 'LaTeXToMathML-tex')
|
9
|
+
puts MATHJAX_TEST_TEX_DIR
|
10
|
+
Dir["#{MATHJAX_TEST_TEX_DIR}/**/*.tex"].each do |tex|
|
11
|
+
define_method "test_#{tex}" do
|
12
|
+
tex_contents = File.read(tex)
|
13
|
+
assert_nothing_raised { render.render(tex_contents) }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mathematical
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Garen Torikian
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake-compiler
|
@@ -303,12 +303,16 @@ files:
|
|
303
303
|
- ext/mathematical/lasem/tools/generate-entity-array.c
|
304
304
|
- ext/mathematical/mathematical.c
|
305
305
|
- ext/mathematical/mtex2MML/ext/extconf.rb
|
306
|
+
- ext/mathematical/mtex2MML/src/color_definitions.c
|
307
|
+
- ext/mathematical/mtex2MML/src/deps/uthash/utarray.h
|
308
|
+
- ext/mathematical/mtex2MML/src/deps/uthash/uthash.h
|
309
|
+
- ext/mathematical/mtex2MML/src/deps/uthash/utlist.h
|
310
|
+
- ext/mathematical/mtex2MML/src/deps/uthash/utstring.h
|
306
311
|
- ext/mathematical/mtex2MML/src/lex.yy.c
|
307
312
|
- ext/mathematical/mtex2MML/src/mtex2MML.h
|
313
|
+
- ext/mathematical/mtex2MML/src/mtex2MML_ruby.c
|
308
314
|
- ext/mathematical/mtex2MML/src/parse_extras.c
|
309
315
|
- ext/mathematical/mtex2MML/src/parse_extras.h
|
310
|
-
- ext/mathematical/mtex2MML/src/stack.c
|
311
|
-
- ext/mathematical/mtex2MML/src/stack.h
|
312
316
|
- ext/mathematical/mtex2MML/src/string_extras.c
|
313
317
|
- ext/mathematical/mtex2MML/src/string_extras.h
|
314
318
|
- ext/mathematical/mtex2MML/src/y.tab.c
|
@@ -350,6 +354,7 @@ files:
|
|
350
354
|
- test/mathematical/fixtures/png/pmatrix.png
|
351
355
|
- test/mathematical/fixtures_test.rb
|
352
356
|
- test/mathematical/maliciousness_test.rb
|
357
|
+
- test/mathematical/mathjax_test.rb
|
353
358
|
- test/mathematical/mathml_test.rb
|
354
359
|
- test/mathematical/performance_test.rb
|
355
360
|
- test/mathematical/png_test.rb
|
@@ -412,6 +417,7 @@ test_files:
|
|
412
417
|
- test/mathematical/fixtures/png/pmatrix.png
|
413
418
|
- test/mathematical/fixtures_test.rb
|
414
419
|
- test/mathematical/maliciousness_test.rb
|
420
|
+
- test/mathematical/mathjax_test.rb
|
415
421
|
- test/mathematical/mathml_test.rb
|
416
422
|
- test/mathematical/performance_test.rb
|
417
423
|
- test/mathematical/png_test.rb
|
@@ -1,80 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
* File: stack.c
|
3
|
-
* Author: Robert I. Pitts <rip@cs.bu.edu>
|
4
|
-
* Last Modified: March 7, 2000
|
5
|
-
* Topic: Stack - Array Implementation
|
6
|
-
* ----------------------------------------------------------------
|
7
|
-
*
|
8
|
-
* This is an array implementation of a character stack.
|
9
|
-
*/
|
10
|
-
|
11
|
-
#include <stdio.h>
|
12
|
-
#include <stdlib.h>
|
13
|
-
#include "stack.h"
|
14
|
-
|
15
|
-
/************************ Function Definitions **********************/
|
16
|
-
|
17
|
-
void StackInit(stackT *stackP, int maxSize)
|
18
|
-
{
|
19
|
-
stackElementT *newContents;
|
20
|
-
|
21
|
-
/* Allocate a new array to hold the contents. */
|
22
|
-
|
23
|
-
newContents = (stackElementT *)malloc(sizeof(stackElementT) * maxSize);
|
24
|
-
|
25
|
-
if (newContents == NULL) {
|
26
|
-
fprintf(stderr, "Insufficient memory to initialize stack.\n");
|
27
|
-
exit(1); /* Exit, returning error code. */
|
28
|
-
}
|
29
|
-
|
30
|
-
stackP->contents = newContents;
|
31
|
-
stackP->maxSize = maxSize;
|
32
|
-
stackP->top = -1; /* I.e., empty */
|
33
|
-
}
|
34
|
-
|
35
|
-
void StackDestroy(stackT *stackP)
|
36
|
-
{
|
37
|
-
/* Get rid of array. */
|
38
|
-
free(stackP->contents);
|
39
|
-
|
40
|
-
stackP->contents = NULL;
|
41
|
-
stackP->maxSize = 0;
|
42
|
-
stackP->top = -1; /* I.e., empty */
|
43
|
-
}
|
44
|
-
|
45
|
-
void StackPush(stackT *stackP, stackElementT element)
|
46
|
-
{
|
47
|
-
if (StackIsFull(stackP)) {
|
48
|
-
fprintf(stderr, "Can't push element on stack: stack is full.\n");
|
49
|
-
exit(1); /* Exit, returning error code. */
|
50
|
-
}
|
51
|
-
|
52
|
-
/* Put information in array; update top. */
|
53
|
-
|
54
|
-
stackP->contents[++stackP->top] = element;
|
55
|
-
}
|
56
|
-
|
57
|
-
stackElementT StackPop(stackT *stackP)
|
58
|
-
{
|
59
|
-
if (StackIsEmpty(stackP)) {
|
60
|
-
fprintf(stderr, "Can't pop element from stack: stack is empty.\n");
|
61
|
-
exit(1); /* Exit, returning error code. */
|
62
|
-
}
|
63
|
-
|
64
|
-
return stackP->contents[stackP->top--];
|
65
|
-
}
|
66
|
-
|
67
|
-
int StackIsEmpty(stackT *stackP)
|
68
|
-
{
|
69
|
-
return stackP->top < 0;
|
70
|
-
}
|
71
|
-
|
72
|
-
int StackIsFull(stackT *stackP)
|
73
|
-
{
|
74
|
-
return stackP->top >= stackP->maxSize - 1;
|
75
|
-
}
|
76
|
-
|
77
|
-
stackElementT StackTop(stackT *stackP)
|
78
|
-
{
|
79
|
-
return stackP->contents[stackP->top];
|
80
|
-
}
|
@@ -1,101 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
* File: stack.h
|
3
|
-
* Author: Robert I. Pitts <rip@cs.bu.edu>
|
4
|
-
* Last Modified: March 7, 2000
|
5
|
-
* Topic: Stack - Array Implementation
|
6
|
-
* ----------------------------------------------------------------
|
7
|
-
*
|
8
|
-
* This is the interface for a stack of characters.
|
9
|
-
*/
|
10
|
-
|
11
|
-
#ifndef _STACK_H
|
12
|
-
#define _STACK_H
|
13
|
-
|
14
|
-
typedef struct {
|
15
|
-
char *line;
|
16
|
-
int line_pos;
|
17
|
-
} stackElementT;
|
18
|
-
|
19
|
-
/*
|
20
|
-
* Type: stackElementT
|
21
|
-
* -------------------
|
22
|
-
* This is the type of the objects entered in the stack.
|
23
|
-
* Edit it to change the type of things to be placed in
|
24
|
-
* the stack.
|
25
|
-
*/
|
26
|
-
|
27
|
-
// typedef char* stackElementT;
|
28
|
-
|
29
|
-
/*
|
30
|
-
* Type: stackT
|
31
|
-
* --------------
|
32
|
-
* This is the type for a stack, i.e., it is a type that
|
33
|
-
* holds the information necessary to keep track of a stack.
|
34
|
-
* It has a pointer `contents' to a dynamically-allocated
|
35
|
-
* array (used to hold the contents of the stack), an integer
|
36
|
-
* `maxSize' that holds the size of this array (i.e., the
|
37
|
-
* maximum number of things that can be held in the stack),
|
38
|
-
* and another integer `top,' which stores the array index of
|
39
|
-
* the element at the top of the stack.
|
40
|
-
*/
|
41
|
-
|
42
|
-
typedef struct {
|
43
|
-
stackElementT *contents;
|
44
|
-
int maxSize;
|
45
|
-
int top;
|
46
|
-
} stackT;
|
47
|
-
|
48
|
-
/*
|
49
|
-
* Function: StackInit
|
50
|
-
* Usage: StackInit(&stack, maxSize);
|
51
|
-
* -------------------------
|
52
|
-
* A new stack variable is initialized. The initialized
|
53
|
-
* stack is made empty. MaxSize is used to determine the
|
54
|
-
* maximum number of character that can be held in the
|
55
|
-
* stack.
|
56
|
-
*/
|
57
|
-
|
58
|
-
void StackInit(stackT *stackP, int maxSize);
|
59
|
-
|
60
|
-
/* Function: StackDestroy
|
61
|
-
* Usage: StackDestroy(&stack);
|
62
|
-
* -----------------------
|
63
|
-
* This function frees all memory associated with the stack.
|
64
|
-
* The `stack' variable may not be used again unless
|
65
|
-
* StackInit(&stack, maxSize) is first called on the stack.
|
66
|
-
*/
|
67
|
-
|
68
|
-
void StackDestroy(stackT *stackP);
|
69
|
-
|
70
|
-
/*
|
71
|
-
* Functions: StackPush, StackPop
|
72
|
-
* Usage: StackPush(&stack, element); element = StackPop(&stack);
|
73
|
-
* --------------------------------------------
|
74
|
-
* These are the fundamental stack operations that add an element to
|
75
|
-
* the top of the stack and remove an element from the top of the stack.
|
76
|
-
* A call to StackPop on an empty stack or to StackPush on a full stack
|
77
|
-
* is an error. Make use of StackIsEmpty()/StackIsFull() (see below)
|
78
|
-
* to avoid these errors.
|
79
|
-
*/
|
80
|
-
|
81
|
-
void StackPush(stackT *stackP, stackElementT element);
|
82
|
-
stackElementT StackPop(stackT *stackP);
|
83
|
-
|
84
|
-
/*
|
85
|
-
* Functions: StackIsEmpty, StackIsFull
|
86
|
-
* Usage: if (StackIsEmpty(&stack)) ...
|
87
|
-
* -----------------------------------
|
88
|
-
* These return a true value if the stack is empty
|
89
|
-
* or full (respectively).
|
90
|
-
*/
|
91
|
-
|
92
|
-
int StackIsEmpty(stackT *stackP);
|
93
|
-
int StackIsFull(stackT *stackP);
|
94
|
-
|
95
|
-
// Retrieve the top-most element from the stack
|
96
|
-
stackElementT StackTop(stackT *stackP);
|
97
|
-
|
98
|
-
// Retrieve the bottom-most element from the stack
|
99
|
-
stackElementT StackBottom(stackT *stackP);
|
100
|
-
|
101
|
-
#endif /* not defined _STACK_H */
|