Advertisement
Guest User

Untitled

a guest
Apr 15th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 2.67 KB | None | 0 0
  1. load ./q3.dll
  2. source symul_lib.tcl
  3.  
  4.  
  5. set obslugaBitow {
  6.   proc bity x { # postac binarna liczby
  7.     usun0 [binary scan [binary format I $x] B* x; set x]
  8.   }
  9.   proc usun0 x { # usuwa zera poczatkowe z repr bin liczby
  10.     set x [string trimleft $x 0]
  11.     if {$x==""} {set x 0}
  12.     set x
  13.   }
  14.   proc porownanieC {cv cu} { # porownuje 2 kolory, zwraca indeks oraz 2 bity...
  15.     set dlcu [string len $cu]
  16.     set dlcv [string len $cv]
  17.     if {$dlcu<$dlcv} {
  18.       set cu "[string repeat 0 [expr {$dlcv-$dlcu}]]$cu"
  19.     }
  20.     if {$dlcu>$dlcv} {
  21.       set cv "[string repeat 0 [expr {$dlcu-$dlcv}]]$cv"
  22.     }
  23.     set dl [string len $cu]
  24.     iterate i $dl {
  25.       set i1 [expr {$dl-$i-1}]
  26.         # KONIECZNIE trzeba numerowac bity od prawej gdyz
  27.         # dopisuje sie 0 z lewej i wtedy indeksy by sie zmienialy!
  28.       set bu [string index $cu $i1]
  29.       set bv [string index $cv $i1]
  30.       if {$bu != $bv} {return "$i $bv $bu"}
  31.     }
  32.     return {-1 ? ?}
  33.   }
  34.   proc wyrownaj {L x} { # dodaje 0 z lewej do L-bitow
  35.     set dl [string len $x]
  36.     if {$dl>$L} {error "wyrownaj"}
  37.     return "[string repeat "0" [expr {$L-$dl}]]$x"
  38.   }
  39.   proc bin2dec x { # do 32-bitow
  40.     binary scan [binary form B* [wyrownaj 32 $x]] I y
  41.     set y
  42.   }
  43.   proc iterate {zm liIter kod} { # wygodna petla
  44.     upvar $zm i
  45.     for {set i 0} {$i<$liIter} {incr i} {
  46.       set e [catch {uplevel $kod} x]
  47.       if {$e!=0} {return -code $e $x}
  48.     }
  49.   }
  50. }
  51.  
  52.  
  53.  
  54. set liczbaWierz 10
  55. iterate i $liczbaWierz {
  56.   let i1 $i-1; if {$i1==-1} {let i1 $liczbaWierz-1}
  57.   let i2 $i+1; if {$i2==$liczbaWierz} {let i2 0}
  58.   set sasiedzi($i) "$i1 $i2"
  59. }
  60.  
  61.  
  62. fiber create $liczbaWierz {
  63.  
  64.   set Cv [bity $id_los]
  65.     set L 50
  66.     set Cv [wyrownaj $L $Cv]
  67.  
  68.   set roznica ?
  69.   set a ?
  70.     set b ?
  71.   set c ?
  72.   set pCv ?
  73.  
  74.   wyslij 1 $Cv
  75.  
  76.  
  77.     fiber yield
  78.    
  79.     while {$L > 2} {
  80.  
  81.         set pCv $kom(0)
  82.  
  83.         set roznica [porownanieC $Cv $pCv]
  84.    
  85.         # numer bitu roznicy - iv
  86.     set a [lindex $roznica 0]
  87.     # bit z mojej liczby
  88.     set b [lindex $roznica 1]
  89.     # bit z otrzymanej liczby
  90.     set c [lindex $roznica 2]
  91.  
  92.     # nowa dlugosc wyrownania
  93.         set L [expr {int(ceil(log($L)/log(2)) + 1)}]
  94.  
  95.     # a - najmniejszy bit roznicy, jezeli istnieje, to przycinam
  96.     if {$a > -1} {
  97.       # przycinam kolor od $L do najmniejszego bitu roznicy?
  98.       set Cv [string range $Cv [expr [string length $Cv] - $L] [expr [string length $Cv] - $a]]
  99.       set Cv [wyrownaj $L $Cv]
  100.     }
  101.  
  102.     wyslij 1 $Cv
  103.  
  104.         fiber yield
  105.     }
  106. }
  107.  
  108. fiber_iterate $obslugaBitow
  109.  
  110. Inicjalizacja;
  111.  
  112. proc wizualizacja {} {
  113.   fiber_iterate {_puts "$id : $Cv \t pCv: $pCv \t dlugosc: $L \t a: $a b: $b c: $c"}
  114. }
  115.  
  116. fiber yield; runda; wizualizacja
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement