mirror of
https://scm.univ-tours.fr/22107988t/rappaurio-sae501_502.git
synced 2025-08-29 21:05:57 +02:00
262 lines
60 KiB
HTML
262 lines
60 KiB
HTML
<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"> <style>
|
||
.KEYW {color: #933;}
|
||
.COMM {color: #bbb; font-style: italic;}
|
||
.NUMB {color: #393;}
|
||
.STRN {color: #393;}
|
||
.REGX {color: #339;}
|
||
.line {border-right: 1px dotted #666; color: #666; font-style: normal;}
|
||
</style></head><body><pre><span class='line'> 1</span> <span class="TOKN"></span><span class="WHIT">
|
||
<span class='line'> 2</span> </span><span class="COMM">/**
|
||
<span class='line'> 3</span> * @name CeL Hamming function
|
||
<span class='line'> 4</span> * @fileoverview
|
||
<span class='line'> 5</span> * 本檔案包含了 Hamming Code, 漢明碼的 functions。
|
||
<span class='line'> 6</span> * @since 2010/3/21 14:26:08
|
||
<span class='line'> 7</span> * @example
|
||
<span class='line'> 8</span> * CeL.use('math.Hamming');
|
||
<span class='line'> 9</span> * var hc = CeL.Hamming.encode('1100');
|
||
<span class='line'> 10</span> */</span><span class="WHIT">
|
||
<span class='line'> 11</span>
|
||
<span class='line'> 12</span>
|
||
<span class='line'> 13</span> </span><span class="COMM">/*
|
||
<span class='line'> 14</span> TODO:
|
||
<span class='line'> 15</span> 最佳化
|
||
<span class='line'> 16</span> calculate generator matrix
|
||
<span class='line'> 17</span> SEC-DED ("single error correction, double error detection") 版本(加在最後)
|
||
<span class='line'> 18</span> http://www.ee.unb.ca/cgi-bin/tervo/hamming.pl
|
||
<span class='line'> 19</span> 最小漢明距離3
|
||
<span class='line'> 20</span> http://phpbb.godfat.org/viewtopic.php?t=66&sid=6e34dd040aa98c64c75bfe099008c82a
|
||
<span class='line'> 21</span> BCH碼
|
||
<span class='line'> 22</span> */</span><span class="WHIT">
|
||
<span class='line'> 23</span>
|
||
<span class='line'> 24</span> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">typeof</span><span class="WHIT"> </span><span class="NAME">CeL</span><span class="WHIT"> </span><span class="PUNC">===</span><span class="WHIT"> </span><span class="STRN">'function'</span><span class="PUNC">)</span><span class="PUNC">{</span><span class="WHIT">
|
||
<span class='line'> 25</span>
|
||
<span class='line'> 26</span> </span><span class="COMM">/**
|
||
<span class='line'> 27</span> * 本 module 之 name(id),<span style="text-decoration:line-through;">不設定時會從呼叫時之 path 取得</span>。
|
||
<span class='line'> 28</span> * @type String
|
||
<span class='line'> 29</span> * @constant
|
||
<span class='line'> 30</span> * @inner
|
||
<span class='line'> 31</span> * @ignore
|
||
<span class='line'> 32</span> */</span><span class="WHIT">
|
||
<span class='line'> 33</span> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">module_name</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="STRN">'math.Hamming'</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'> 34</span>
|
||
<span class='line'> 35</span> </span><span class="COMM">//===================================================</span><span class="WHIT">
|
||
<span class='line'> 36</span> </span><span class="COMM">/**
|
||
<span class='line'> 37</span> * 若欲 include 整個 module 時,需囊括之 code。
|
||
<span class='line'> 38</span> * @type Function
|
||
<span class='line'> 39</span> * @param {Function} library_namespace namespace of library
|
||
<span class='line'> 40</span> * @param load_arguments 呼叫時之 argument(s)
|
||
<span class='line'> 41</span> * @return
|
||
<span class='line'> 42</span> * @name CeL.math.Hamming
|
||
<span class='line'> 43</span> * @constant
|
||
<span class='line'> 44</span> * @inner
|
||
<span class='line'> 45</span> * @ignore
|
||
<span class='line'> 46</span> */</span><span class="WHIT">
|
||
<span class='line'> 47</span> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">code_for_including</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">library_namespace</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">load_arguments</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
|
||
<span class='line'> 48</span>
|
||
<span class='line'> 49</span>
|
||
<span class='line'> 50</span>
|
||
<span class='line'> 51</span> </span><span class="COMM">//============================================================================</span><span class="WHIT">
|
||
<span class='line'> 52</span> </span><span class="COMM">// definition of module Hamming</span><span class="WHIT">
|
||
<span class='line'> 53</span>
|
||
<span class='line'> 54</span> </span><span class="KEYW">var</span><span class="WHIT">
|
||
<span class='line'> 55</span> </span><span class="COMM">/**
|
||
<span class='line'> 56</span> * Hamming code
|
||
<span class='line'> 57</span> * @class Hamming Code 的 constructor
|
||
<span class='line'> 58</span> * @constructor
|
||
<span class='line'> 59</span> */</span><span class="WHIT">
|
||
<span class='line'> 60</span> </span><span class="NAME">CeL.math.Hamming</span><span class="WHIT">
|
||
<span class='line'> 61</span> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
|
||
<span class='line'> 62</span> </span><span class="WHIT"> </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">this.encode.apply</span><span class="PUNC">(</span><span class="KEYW">this</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">arguments</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'> 63</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'> 64</span>
|
||
<span class='line'> 65</span> </span><span class="COMM">//class public interface ---------------------------</span><span class="WHIT">
|
||
<span class='line'> 66</span>
|
||
<span class='line'> 67</span> </span><span class="NAME">CeL.math.Hamming</span><span class="WHIT">
|
||
<span class='line'> 68</span> </span><span class="PUNC">.</span><span class="WHIT">
|
||
<span class='line'> 69</span> </span><span class="COMM">/**
|
||
<span class='line'> 70</span> * 是否左右顛倒。
|
||
<span class='line'> 71</span> * default: data[1,2,..] 左至右, reverse: data[..,2,1] 右至左
|
||
<span class='line'> 72</span> * @memberOf CeL.math.Hamming
|
||
<span class='line'> 73</span> */</span><span class="WHIT">
|
||
<span class='line'> 74</span> </span><span class="NAME">reverse</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">false</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'> 75</span>
|
||
<span class='line'> 76</span>
|
||
<span class='line'> 77</span> </span><span class="NAME">CeL.math.Hamming</span><span class="WHIT">
|
||
<span class='line'> 78</span> </span><span class="PUNC">.</span><span class="WHIT">
|
||
<span class='line'> 79</span> </span><span class="COMM">/**
|
||
<span class='line'> 80</span> * encode data to Hamming Code.
|
||
<span class='line'> 81</span> * @param data data stream
|
||
<span class='line'> 82</span> * @param no_reverse forced NO reverse
|
||
<span class='line'> 83</span> * @return {String} encoded Hamming Code
|
||
<span class='line'> 84</span> * @memberOf CeL.math.Hamming
|
||
<span class='line'> 85</span> */</span><span class="WHIT">
|
||
<span class='line'> 86</span> </span><span class="NAME">encode</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">data</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">no_reverse</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
|
||
<span class='line'> 87</span> </span><span class="WHIT"> </span><span class="NAME">data</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">library_namespace.is_Array</span><span class="PUNC">(</span><span class="NAME">data</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">?</span><span class="WHIT"> </span><span class="NAME">data.join</span><span class="PUNC">(</span><span class="STRN">''</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="STRN">''</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">data</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'> 88</span> </span><span class="WHIT"> </span><span class="PUNC">.</span><span class="NAME">replace</span><span class="PUNC">(</span><span class="REGX">/[ ,]/g</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="STRN">''</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'> 89</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">!</span><span class="REGX">/^[01]{1,20}$/</span><span class="PUNC">.</span><span class="NAME">test</span><span class="PUNC">(</span><span class="NAME">data</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'> 90</span> </span><span class="WHIT"> </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="STRN">''</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'> 91</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">this.reverse</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="PUNC">!</span><span class="NAME">no_reverse</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'> 92</span> </span><span class="WHIT"> </span><span class="NAME">data</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">data.split</span><span class="PUNC">(</span><span class="STRN">''</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">reverse</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">join</span><span class="PUNC">(</span><span class="STRN">''</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'> 93</span> </span><span class="WHIT"> </span><span class="COMM">// library_namespace.debug('encode [' + d + ']',1,module_name + '.encode');</span><span class="WHIT">
|
||
<span class='line'> 94</span> </span><span class="WHIT"> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">g</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="WHIT"> </span><span class="STRN">''</span><span class="WHIT"> </span><span class="PUNC">]</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">i_g</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">bin</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">i_d</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">l_d</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">data.length</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">ch</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'> 95</span> </span><span class="WHIT"> </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">;</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i_g</span><span class="PUNC">++</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
|
||
<span class='line'> 96</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">i_g</span><span class="WHIT"> </span><span class="PUNC">===</span><span class="WHIT"> </span><span class="NAME">bin</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'> 97</span> </span><span class="WHIT"> </span><span class="COMM">// library_namespace.debug('initial [' + g.length + '] as 0',1,module_name + '.encode'),</span><span class="WHIT">
|
||
<span class='line'> 98</span> </span><span class="WHIT"> </span><span class="NAME">g.push</span><span class="PUNC">(</span><span class="NUMB">0</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">bin</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">2</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'> 99</span> </span><span class="WHIT"> </span><span class="KEYW">else</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
|
||
<span class='line'>100</span> </span><span class="WHIT"> </span><span class="COMM">// library_namespace.debug('initial [' + g.length + '] as ' + d.charAt(i_d) + ' (' + i_d + '/' + l_d + ')',1,module_name + '.encode');</span><span class="WHIT">
|
||
<span class='line'>101</span> </span><span class="WHIT"> </span><span class="NAME">g.push</span><span class="PUNC">(</span><span class="NAME">ch</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">data.charAt</span><span class="PUNC">(</span><span class="NAME">i_d</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>102</span> </span><span class="WHIT"> </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC"><</span><span class="WHIT"> </span><span class="NAME">bin</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">2</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>103</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">i_g</span><span class="WHIT"> </span><span class="PUNC">&</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>104</span> </span><span class="WHIT"> </span><span class="NAME">g</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">^</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">ch</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>105</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">++</span><span class="NAME">i_d</span><span class="WHIT"> </span><span class="PUNC">===</span><span class="WHIT"> </span><span class="NAME">l_d</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>106</span> </span><span class="WHIT"> </span><span class="KEYW">break</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>107</span> </span><span class="WHIT"> </span><span class="PUNC">}</span><span class="WHIT">
|
||
<span class='line'>108</span> </span><span class="WHIT"> </span><span class="PUNC">}</span><span class="WHIT">
|
||
<span class='line'>109</span>
|
||
<span class='line'>110</span>
|
||
<span class='line'>111</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">library_namespace.is_debug</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
|
||
<span class='line'>112</span> </span><span class="WHIT"> </span><span class="COMM">// for debug print</span><span class="WHIT">
|
||
<span class='line'>113</span> </span><span class="WHIT"> </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">bin</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">i_d</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC"><</span><span class="WHIT"> </span><span class="NAME">g.length</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">++</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
|
||
<span class='line'>114</span> </span><span class="WHIT"> </span><span class="NAME">ch</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">g</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>115</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">===</span><span class="WHIT"> </span><span class="NAME">bin</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>116</span> </span><span class="WHIT"> </span><span class="NAME">ch</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="STRN">'<span style="color:#292;">'</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">ch</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">'</span>'</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">bin</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">2</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>117</span> </span><span class="WHIT"> </span><span class="COMM">// i_d 在這表示一個專門用於顯示的 array</span><span class="WHIT">
|
||
<span class='line'>118</span> </span><span class="WHIT"> </span><span class="NAME">i_d.push</span><span class="PUNC">(</span><span class="NAME">ch</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>119</span> </span><span class="WHIT"> </span><span class="PUNC">}</span><span class="WHIT">
|
||
<span class='line'>120</span> </span><span class="WHIT"> </span><span class="NAME">library_namespace.log</span><span class="PUNC">(</span><span class="STRN">'Hamming code of ['</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">data</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">']: ['</span><span class="WHIT">
|
||
<span class='line'>121</span> </span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">i_d.join</span><span class="PUNC">(</span><span class="STRN">''</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">']'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>122</span> </span><span class="WHIT"> </span><span class="PUNC">}</span><span class="WHIT">
|
||
<span class='line'>123</span>
|
||
<span class='line'>124</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">this.reverse</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="PUNC">!</span><span class="NAME">no_reverse</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>125</span> </span><span class="WHIT"> </span><span class="NAME">g</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">g.reverse</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>126</span> </span><span class="WHIT"> </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">g.join</span><span class="PUNC">(</span><span class="STRN">''</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>127</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>128</span>
|
||
<span class='line'>129</span>
|
||
<span class='line'>130</span> </span><span class="NAME">CeL.math.Hamming</span><span class="WHIT">
|
||
<span class='line'>131</span> </span><span class="PUNC">.</span><span class="WHIT">
|
||
<span class='line'>132</span> </span><span class="COMM">/**
|
||
<span class='line'>133</span> * 將 Hamming Code 分成 data & check bits
|
||
<span class='line'>134</span> * @param code Hamming Code to split
|
||
<span class='line'>135</span> * @return [資料位元 data bits, 檢查位元 check bits (parity bits)]
|
||
<span class='line'>136</span> * @memberOf CeL.math.Hamming
|
||
<span class='line'>137</span> */</span><span class="WHIT">
|
||
<span class='line'>138</span> </span><span class="NAME">split_code</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">code</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
|
||
<span class='line'>139</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">library_namespace.is_Array</span><span class="PUNC">(</span><span class="NAME">code</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>140</span> </span><span class="WHIT"> </span><span class="NAME">code</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">code.join</span><span class="PUNC">(</span><span class="STRN">''</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>141</span> </span><span class="WHIT"> </span><span class="NAME">code</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="STRN">' '</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">code</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">split</span><span class="PUNC">(</span><span class="STRN">''</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>142</span> </span><span class="WHIT"> </span><span class="NAME">library_namespace.debug</span><span class="PUNC">(</span><span class="STRN">'split ['</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">code</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">']'</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">module_name</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">'.split_code'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>143</span> </span><span class="WHIT"> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">l</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">code.length</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">cb</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>144</span> </span><span class="WHIT"> </span><span class="KEYW">while</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC"><</span><span class="WHIT"> </span><span class="NAME">l</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>145</span> </span><span class="WHIT"> </span><span class="NAME">cb.push</span><span class="PUNC">(</span><span class="NAME">code</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">code</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="STRN">''</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">2</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>146</span> </span><span class="WHIT"> </span><span class="NAME">library_namespace.debug</span><span class="PUNC">(</span><span class="STRN">'→ data ['</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">code.join</span><span class="PUNC">(</span><span class="STRN">''</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">replace</span><span class="PUNC">(</span><span class="REGX">/ +/g</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="STRN">''</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">'], check bits ['</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">cb</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">']'</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">module_name</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">'.split_code'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>147</span> </span><span class="WHIT"> </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="WHIT"> </span><span class="NAME">code.join</span><span class="PUNC">(</span><span class="STRN">''</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">replace</span><span class="PUNC">(</span><span class="REGX">/ +/g</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="STRN">''</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">cb</span><span class="WHIT"> </span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>148</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>149</span>
|
||
<span class='line'>150</span> </span><span class="NAME">CeL.math.Hamming</span><span class="WHIT">
|
||
<span class='line'>151</span> </span><span class="PUNC">.</span><span class="WHIT">
|
||
<span class='line'>152</span> </span><span class="COMM">/**
|
||
<span class='line'>153</span> * decode Hamming Code to data
|
||
<span class='line'>154</span> * @param code
|
||
<span class='line'>155</span> * @return
|
||
<span class='line'>156</span> * @memberOf CeL.math.Hamming
|
||
<span class='line'>157</span> */</span><span class="WHIT">
|
||
<span class='line'>158</span> </span><span class="NAME">decode</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">code</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
|
||
<span class='line'>159</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">!</span><span class="NAME">code</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="PUNC">!</span><span class="REGX">/^[01]{3,30}$/</span><span class="PUNC">.</span><span class="NAME">test</span><span class="PUNC">(</span><span class="NAME">code</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="STRN">''</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">code</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">replace</span><span class="PUNC">(</span><span class="REGX">/[ ,]/g</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="STRN">''</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>160</span> </span><span class="WHIT"> </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="STRN">''</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>161</span> </span><span class="WHIT"> </span><span class="NAME">code</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">code.split</span><span class="PUNC">(</span><span class="STRN">''</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>162</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">this.reverse</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>163</span> </span><span class="WHIT"> </span><span class="NAME">code</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">code.reverse</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>164</span>
|
||
<span class='line'>165</span> </span><span class="WHIT"> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">l</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">code.length</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">ch</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">bin</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">split_c</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.split_code</span><span class="PUNC">(</span><span class="NAME">code</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">test_c</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">cb</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>166</span>
|
||
<span class='line'>167</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">!</span><span class="NAME">split_c</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>168</span> </span><span class="WHIT"> </span><span class="KEYW">return</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>169</span> </span><span class="WHIT"> </span><span class="COMM">// check bits (parity bits)</span><span class="WHIT">
|
||
<span class='line'>170</span> </span><span class="WHIT"> </span><span class="NAME">cb</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">split_c</span><span class="PUNC">[</span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>171</span> </span><span class="WHIT"> </span><span class="NAME">test_c</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.encode</span><span class="PUNC">(</span><span class="NAME">split_c</span><span class="PUNC">[</span><span class="NUMB">0</span><span class="PUNC">]</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>172</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">!</span><span class="NAME">test_c</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="PUNC">!</span><span class="PUNC">(</span><span class="NAME">test_c</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.split_code</span><span class="PUNC">(</span><span class="NAME">test_c</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>173</span> </span><span class="WHIT"> </span><span class="KEYW">return</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>174</span>
|
||
<span class='line'>175</span> </span><span class="WHIT"> </span><span class="NAME">library_namespace.debug</span><span class="PUNC">(</span><span class="STRN">'received check bits: ['</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">cb.join</span><span class="PUNC">(</span><span class="STRN">''</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">'], calculated: ['</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">test_c</span><span class="PUNC">[</span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">.</span><span class="NAME">join</span><span class="PUNC">(</span><span class="STRN">''</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">']'</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">module_name</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">'.decode'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>176</span> </span><span class="WHIT"> </span><span class="NAME">test_c</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">parseInt</span><span class="PUNC">(</span><span class="NAME">test_c</span><span class="PUNC">[</span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">.</span><span class="NAME">reverse</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">join</span><span class="PUNC">(</span><span class="STRN">''</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NUMB">2</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>177</span> </span><span class="WHIT"> </span><span class="PUNC">^</span><span class="WHIT"> </span><span class="NAME">parseInt</span><span class="PUNC">(</span><span class="NAME">cb.reverse</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">join</span><span class="PUNC">(</span><span class="STRN">''</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NUMB">2</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>178</span> </span><span class="WHIT"> </span><span class="NAME">library_namespace.debug</span><span class="PUNC">(</span><span class="STRN">'error bit'</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">this.reverse</span><span class="WHIT"> </span><span class="PUNC">?</span><span class="WHIT"> </span><span class="STRN">' (do reversed XOR)'</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="STRN">''</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">': '</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">test_c</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">'('</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">test_c.toString</span><span class="PUNC">(</span><span class="NUMB">2</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">'), '</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">code.join</span><span class="PUNC">(</span><span class="STRN">''</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">module_name</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">'.decode'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>179</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">test_c</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>180</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">test_c</span><span class="WHIT"> </span><span class="PUNC"><</span><span class="WHIT"> </span><span class="NAME">code.length</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>181</span> </span><span class="WHIT"> </span><span class="NAME">code</span><span class="PUNC">[</span><span class="NAME">test_c</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">^</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">split_c</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.split_code</span><span class="PUNC">(</span><span class="NAME">code</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>182</span> </span><span class="WHIT"> </span><span class="KEYW">else</span><span class="WHIT">
|
||
<span class='line'>183</span> </span><span class="WHIT"> </span><span class="COMM">// 這算是能檢測出 2 bits 以上錯誤的特殊例子,機率通常也不大:已經超過 index 了。</span><span class="WHIT">
|
||
<span class='line'>184</span> </span><span class="WHIT"> </span><span class="COMM">// e.g., reversed 011010001100</span><span class="WHIT">
|
||
<span class='line'>185</span> </span><span class="WHIT"> </span><span class="NAME">library_namespace.debug</span><span class="PUNC">(</span><span class="STRN">'<em>Out of index!</em> More than 2 errors occurred.'</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">module_name</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">'.decode'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>186</span>
|
||
<span class='line'>187</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">library_namespace.is_debug</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>188</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">test_c</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
|
||
<span class='line'>189</span> </span><span class="WHIT"> </span><span class="NAME">cb</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">code.join</span><span class="PUNC">(</span><span class="STRN">'\0'</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">split</span><span class="PUNC">(</span><span class="STRN">'\0'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>190</span> </span><span class="WHIT"> </span><span class="NAME">cb</span><span class="PUNC">[</span><span class="NAME">test_c</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="STRN">'<span style="color:#f33;">'</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">cb</span><span class="PUNC">[</span><span class="NAME">test_c</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">'</span>'</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>191</span> </span><span class="WHIT"> </span><span class="NAME">library_namespace.debug</span><span class="PUNC">(</span><span class="STRN">'→ '</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">cb.join</span><span class="PUNC">(</span><span class="STRN">''</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">module_name</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">'.decode'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>192</span> </span><span class="WHIT"> </span><span class="PUNC">}</span><span class="WHIT"> </span><span class="KEYW">else</span><span class="WHIT">
|
||
<span class='line'>193</span> </span><span class="WHIT"> </span><span class="NAME">library_namespace.debug</span><span class="PUNC">(</span><span class="STRN">'The Hamming code is correct.'</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">module_name</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">'.decode'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>194</span>
|
||
<span class='line'>195</span> </span><span class="WHIT"> </span><span class="NAME">split_c</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">split_c</span><span class="PUNC">[</span><span class="NUMB">0</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>196</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">this.reverse</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>197</span> </span><span class="WHIT"> </span><span class="NAME">split_c</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">split_c.split</span><span class="PUNC">(</span><span class="STRN">''</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">reverse</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">join</span><span class="PUNC">(</span><span class="STRN">''</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>198</span> </span><span class="WHIT"> </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">split_c</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>199</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>200</span>
|
||
<span class='line'>201</span>
|
||
<span class='line'>202</span> </span><span class="NAME">CeL.math.Hamming</span><span class="WHIT">
|
||
<span class='line'>203</span> </span><span class="PUNC">.</span><span class="WHIT">
|
||
<span class='line'>204</span> </span><span class="COMM">/**
|
||
<span class='line'>205</span> * 顯示 Hamming Code 的計算方法
|
||
<span class='line'>206</span> * @param {Number} bit_length bit length. e.g., 8, 16.
|
||
<span class='line'>207</span> * @memberOf CeL.math.Hamming
|
||
<span class='line'>208</span> */</span><span class="WHIT">
|
||
<span class='line'>209</span> </span><span class="NAME">show</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">bit_length</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
|
||
<span class='line'>210</span> </span><span class="WHIT"> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">code</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">bit</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">parity</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">j</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">k</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">d</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">a</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">cc</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">dc</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>211</span> </span><span class="WHIT"> </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">;</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">++</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
|
||
<span class='line'>212</span> </span><span class="WHIT"> </span><span class="NAME">bit</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">i.toString</span><span class="PUNC">(</span><span class="NUMB">2</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>213</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">===</span><span class="WHIT"> </span><span class="NAME">a</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>214</span> </span><span class="WHIT"> </span><span class="NAME">code</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="STRN">'C'</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">Math.pow</span><span class="PUNC">(</span><span class="NUMB">2</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">cc</span><span class="PUNC">++</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">a</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">2</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>215</span> </span><span class="WHIT"> </span><span class="KEYW">else</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
|
||
<span class='line'>216</span> </span><span class="WHIT"> </span><span class="NAME">code</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="STRN">'D'</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">dc</span><span class="PUNC">++</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>217</span> </span><span class="WHIT"> </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">j</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">k</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">j</span><span class="WHIT"> </span><span class="PUNC"><</span><span class="WHIT"> </span><span class="NAME">cc</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">j</span><span class="PUNC">++</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">k</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">2</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>218</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">&</span><span class="WHIT"> </span><span class="NAME">k</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>219</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">parity</span><span class="PUNC">[</span><span class="NAME">j</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>220</span> </span><span class="WHIT"> </span><span class="NAME">parity</span><span class="PUNC">[</span><span class="NAME">j</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="STRN">'<span style="color:#aaa;">⊕</span>'</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">code</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>221</span> </span><span class="WHIT"> </span><span class="KEYW">else</span><span class="WHIT">
|
||
<span class='line'>222</span> </span><span class="WHIT"> </span><span class="NAME">parity</span><span class="PUNC">[</span><span class="NAME">j</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">code</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>223</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">dc</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="NAME">bit_length</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>224</span> </span><span class="WHIT"> </span><span class="KEYW">break</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>225</span> </span><span class="WHIT"> </span><span class="PUNC">}</span><span class="WHIT">
|
||
<span class='line'>226</span> </span><span class="WHIT"> </span><span class="PUNC">}</span><span class="WHIT">
|
||
<span class='line'>227</span> </span><span class="WHIT"> </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC"><</span><span class="WHIT"> </span><span class="NAME">code.length</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">++</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
|
||
<span class='line'>228</span> </span><span class="WHIT"> </span><span class="NAME">a</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">code</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>229</span> </span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">j</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">a.match</span><span class="PUNC">(</span><span class="REGX">/^C(\d+)$/</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT">
|
||
<span class='line'>230</span> </span><span class="WHIT"> </span><span class="NAME">a</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="STRN">' = '</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">parity</span><span class="PUNC">[</span><span class="NAME">Math.round</span><span class="PUNC">(</span><span class="NAME">Math.log</span><span class="PUNC">(</span><span class="NAME">j</span><span class="PUNC">[</span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="WHIT"> </span><span class="NAME">Math.LOG2E</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>231</span> </span><span class="WHIT"> </span><span class="NAME">library_namespace.debug</span><span class="PUNC">(</span><span class="NAME">bit</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">': '</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">a</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>232</span> </span><span class="WHIT"> </span><span class="PUNC">}</span><span class="WHIT">
|
||
<span class='line'>233</span>
|
||
<span class='line'>234</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>235</span>
|
||
<span class='line'>236</span>
|
||
<span class='line'>237</span> </span><span class="COMM">/**
|
||
<span class='line'>238</span> * 不 extend 的 member
|
||
<span class='line'>239</span> * @ignore
|
||
<span class='line'>240</span> */</span><span class="WHIT">
|
||
<span class='line'>241</span> </span><span class="NAME">CeL.math.Hamming</span><span class="WHIT">
|
||
<span class='line'>242</span> </span><span class="PUNC">.</span><span class="NAME">no_extend</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="STRN">'*'</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>243</span>
|
||
<span class='line'>244</span>
|
||
<span class='line'>245</span> </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="WHIT">
|
||
<span class='line'>246</span> </span><span class="WHIT"> </span><span class="NAME">CeL.math.Hamming</span><span class="WHIT">
|
||
<span class='line'>247</span> </span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>248</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>249</span>
|
||
<span class='line'>250</span> </span><span class="COMM">//============================================================================</span><span class="WHIT">
|
||
<span class='line'>251</span>
|
||
<span class='line'>252</span> </span><span class="NAME">CeL.setup_module</span><span class="PUNC">(</span><span class="NAME">module_name</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">code_for_including</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>253</span>
|
||
<span class='line'>254</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
|
||
<span class='line'>255</span> </span></pre></body></html> |