ZealOS/docs/Demo/MultiCore/Palindrome.html
2021-07-03 00:07:57 -04:00

129 lines
9.6 KiB
HTML
Executable file

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
<meta name="generator" content="ZealOS V0.05">
<style type="text/css">
body {background-color:#000000;}
.cF0{color:#ffffff;background-color:#000000;}
.cF1{color:#3465a4;background-color:#000000;}
.cF2{color:#4e9a06;background-color:#000000;}
.cF3{color:#06989a;background-color:#000000;}
.cF4{color:#a24444;background-color:#000000;}
.cF5{color:#75507b;background-color:#000000;}
.cF6{color:#ce982f;background-color:#000000;}
.cF7{color:#bcc0b9;background-color:#000000;}
.cF8{color:#555753;background-color:#000000;}
.cF9{color:#729fcf;background-color:#000000;}
.cFA{color:#82bc49;background-color:#000000;}
.cFB{color:#34e2e2;background-color:#000000;}
.cFC{color:#ac3535;background-color:#000000;}
.cFD{color:#ad7fa8;background-color:#000000;}
.cFE{color:#fce94f;background-color:#000000;}
.cFF{color:#000000;background-color:#000000;}
</style>
</head>
<body>
<pre style="font-family:courier;font-size:10pt">
<a name="l1"></a><span class=cF0>#</span><span class=cF1>define</span><span class=cF0> BORDER_CHARS </span><span class=cFE>1</span><span class=cF0>
<a name="l2"></a>
<a name="l3"></a></span><span class=cF1>U8</span><span class=cF0> *buf;
<a name="l4"></a></span><span class=cF9>I64</span><span class=cF0> size, mp_not_done_flags;
<a name="l5"></a></span><span class=cF9>CDoc</span><span class=cF0> *doc;
<a name="l6"></a>
<a name="l7"></a></span><span class=cF1>U0</span><span class=cF0> PDNormalize()
<a name="l8"></a>{
<a name="l9"></a> </span><span class=cF1>U8</span><span class=cF0> *r = </span><span class=cF5>MAlloc</span><span class=cF0>(size+</span><span class=cFE>2</span><span class=cF0>*BORDER_CHARS), *src = buf, *dst = r;
<a name="l10"></a> </span><span class=cF9>I64</span><span class=cF0> ch;
<a name="l11"></a>
<a name="l12"></a> *dst++ = </span><span class=cF6>'['</span><span class=cF0>; </span><span class=cF2>//Border</span><span class=cF0>
<a name="l13"></a> </span><span class=cF1>while</span><span class=cF0> (size--)
<a name="l14"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l15"></a> ch = *src++;
<a name="l16"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF6>'a'</span><span class=cF0> &lt;= ch &lt;= </span><span class=cF6>'z'</span><span class=cF0>) </span><span class=cF2>//lower is most common so do first</span><span class=cF0>
<a name="l17"></a> *dst++ = ch;
<a name="l18"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF6>'A'</span><span class=cF0> &lt;= ch &lt;= </span><span class=cF6>'Z'</span><span class=cF0>)
<a name="l19"></a> *dst++ = ch + </span><span class=cF6>'a'</span><span class=cF0> - </span><span class=cF6>'A'</span><span class=cF0>; </span><span class=cF2>//Convert to lower</span><span class=cF0>
<a name="l20"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l21"></a> *dst++ = </span><span class=cF6>']'</span><span class=cF0>; </span><span class=cF2>//Border</span><span class=cF0>
<a name="l22"></a> </span><span class=cF5>Free</span><span class=cF0>(buf);
<a name="l23"></a> buf = r;
<a name="l24"></a> size = dst - r - </span><span class=cFE>2</span><span class=cF0> * BORDER_CHARS;
<a name="l25"></a>}
<a name="l26"></a>
<a name="l27"></a></span><span class=cF1>U0</span><span class=cF0> PDAnswer(</span><span class=cF1>U8</span><span class=cF0> *a, </span><span class=cF9>I64</span><span class=cF0> len)
<a name="l28"></a>{
<a name="l29"></a> </span><span class=cF5>DocLock</span><span class=cF0>(doc);
<a name="l30"></a> a -= (len - </span><span class=cFE>1</span><span class=cF0>) / </span><span class=cFE>2</span><span class=cF0>;
<a name="l31"></a> </span><span class=cF5>DocPrint</span><span class=cF0>(doc, </span><span class=cF6>&quot;CPU%02X Len:%2d &quot;</span><span class=cF0>, </span><span class=cF5>Gs</span><span class=cF0>-&gt;num, len);
<a name="l32"></a> </span><span class=cF1>while</span><span class=cF0> (len--)
<a name="l33"></a> </span><span class=cF5>DocPrint</span><span class=cF0>(doc, </span><span class=cF6>&quot;%C&quot;</span><span class=cF0>, *a++); </span><span class=cF2>//%C is toupper char</span><span class=cF0>
<a name="l34"></a> </span><span class=cF5>DocPrint</span><span class=cF0>(doc, </span><span class=cF6>&quot;\n&quot;</span><span class=cF0>);
<a name="l35"></a> </span><span class=cF5>DocUnlock</span><span class=cF0>(doc);
<a name="l36"></a>}
<a name="l37"></a>
<a name="l38"></a></span><span class=cF1>U0</span><span class=cF0> MPPalindrome(</span><span class=cF9>I64</span><span class=cF0> dummy=</span><span class=cFE>0</span><span class=cF0>)
<a name="l39"></a>{
<a name="l40"></a> </span><span class=cF1>no_warn</span><span class=cF0> dummy;
<a name="l41"></a> </span><span class=cF1>U8</span><span class=cF0> *src = buf + BORDER_CHARS + size * </span><span class=cF5>Gs</span><span class=cF0>-&gt;num / </span><span class=cFB>mp_count</span><span class=cF0>, *f, *b;
<a name="l42"></a> </span><span class=cF9>I64</span><span class=cF0> len, best = </span><span class=cFE>0</span><span class=cF0>, my_size = (size + </span><span class=cFB>mp_count</span><span class=cF0> - </span><span class=cFE>1</span><span class=cF0>) / </span><span class=cFB>mp_count</span><span class=cF0>;
<a name="l43"></a> </span><span class=cF1>while</span><span class=cF0> (my_size--)
<a name="l44"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l45"></a>
<a name="l46"></a> </span><span class=cF2>//Odd</span><span class=cF0>
<a name="l47"></a> f = src + </span><span class=cFE>1</span><span class=cF0>;
<a name="l48"></a> b = src - </span><span class=cFE>1</span><span class=cF0>;
<a name="l49"></a> </span><span class=cF1>while</span><span class=cF0> (*f == *b)
<a name="l50"></a> {
<a name="l51"></a> f++;
<a name="l52"></a> b--;
<a name="l53"></a> }
<a name="l54"></a> len = f - b + </span><span class=cFE>1</span><span class=cF0> - </span><span class=cFE>2</span><span class=cF0>;
<a name="l55"></a> </span><span class=cF1>if</span><span class=cF0> (len &gt; best)
<a name="l56"></a> {
<a name="l57"></a> best = len;
<a name="l58"></a> PDAnswer(src, len);
<a name="l59"></a> }
<a name="l60"></a>
<a name="l61"></a> </span><span class=cF2>//Even</span><span class=cF0>
<a name="l62"></a> f = src + </span><span class=cFE>1</span><span class=cF0>;
<a name="l63"></a> b = src;
<a name="l64"></a> </span><span class=cF1>while</span><span class=cF0> (*f == *b)
<a name="l65"></a> {
<a name="l66"></a> f++;
<a name="l67"></a> b--;
<a name="l68"></a> }
<a name="l69"></a> len = f - b + </span><span class=cFE>1</span><span class=cF0> - </span><span class=cFE>2</span><span class=cF0>;
<a name="l70"></a> </span><span class=cF1>if</span><span class=cF0> (len &gt; best)
<a name="l71"></a> {
<a name="l72"></a> best = len;
<a name="l73"></a> PDAnswer(src, len);
<a name="l74"></a> }
<a name="l75"></a>
<a name="l76"></a> src++;
<a name="l77"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l78"></a> </span><span class=cF5>LBtr</span><span class=cF0>(&amp;mp_not_done_flags, </span><span class=cF5>Gs</span><span class=cF0>-&gt;num);
<a name="l79"></a>}
<a name="l80"></a>
<a name="l81"></a></span><span class=cF1>U0</span><span class=cF0> Palindrome(</span><span class=cF1>U8</span><span class=cF0> *filename)
<a name="l82"></a>{
<a name="l83"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l84"></a> </span><span class=cF1>F64</span><span class=cF0> t0 = </span><span class=cF5>tS</span><span class=cF0>;
<a name="l85"></a>
<a name="l86"></a> buf = </span><span class=cF5>FileRead</span><span class=cF0>(filename, &amp;size);
<a name="l87"></a> PDNormalize;
<a name="l88"></a>
<a name="l89"></a> doc = </span><span class=cF5>DocPut</span><span class=cF0>;
<a name="l90"></a> mp_not_done_flags = </span><span class=cFE>1</span><span class=cF0> &lt;&lt; </span><span class=cFB>mp_count</span><span class=cF0> - </span><span class=cFE>1</span><span class=cF0>;
<a name="l91"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i &lt; </span><span class=cFB>mp_count</span><span class=cF0>; i++)
<a name="l92"></a> </span><span class=cF5>JobQueue</span><span class=cF0>(&amp;MPPalindrome, </span><span class=cF3>NULL</span><span class=cF0>, i);
<a name="l93"></a> </span><span class=cF1>while</span><span class=cF0> (mp_not_done_flags)
<a name="l94"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l95"></a>
<a name="l96"></a> </span><span class=cF5>Free</span><span class=cF0>(buf);
<a name="l97"></a> </span><span class=cF6>&quot;Time:%9.5f\n&quot;</span><span class=cF0>, </span><span class=cF5>tS</span><span class=cF0> - t0;
<a name="l98"></a>}
<a name="l99"></a>
<a name="l100"></a>Palindrome(</span><span class=cF3>BIBLE_FILENAME</span><span class=cF0>);
</span></pre></body>
</html>