Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- load ./q3.dll
- source symul_lib.tcl
- set obslugaBitow {
- proc bity x { # postac binarna liczby
- usun0 [binary scan [binary format I $x] B* x; set x]
- }
- proc usun0 x { # usuwa zera poczatkowe z repr bin liczby
- set x [string trimleft $x 0]
- if {$x==""} {set x 0}
- set x
- }
- proc porownanieC {cv cu} { # porownuje 2 kolory, zwraca indeks oraz 2 bity...
- set dlcu [string len $cu]
- set dlcv [string len $cv]
- if {$dlcu<$dlcv} {
- set cu "[string repeat 0 [expr {$dlcv-$dlcu}]]$cu"
- }
- if {$dlcu>$dlcv} {
- set cv "[string repeat 0 [expr {$dlcu-$dlcv}]]$cv"
- }
- set dl [string len $cu]
- iterate i $dl {
- set i1 [expr {$dl-$i-1}]
- # KONIECZNIE trzeba numerowac bity od prawej gdyz
- # dopisuje sie 0 z lewej i wtedy indeksy by sie zmienialy!
- set bu [string index $cu $i1]
- set bv [string index $cv $i1]
- if {$bu != $bv} {return "$i $bv $bu"}
- }
- return {-1 ? ?}
- }
- proc wyrownaj {L x} { # dodaje 0 z lewej do L-bitow
- set dl [string len $x]
- if {$dl>$L} {error "wyrownaj"}
- return "[string repeat "0" [expr {$L-$dl}]]$x"
- }
- proc bin2dec x { # do 32-bitow
- binary scan [binary form B* [wyrownaj 32 $x]] I y
- set y
- }
- proc iterate {zm liIter kod} { # wygodna petla
- upvar $zm i
- for {set i 0} {$i<$liIter} {incr i} {
- set e [catch {uplevel $kod} x]
- if {$e!=0} {return -code $e $x}
- }
- }
- }
- set liczbaWierz 10
- iterate i $liczbaWierz {
- let i1 $i-1; if {$i1==-1} {let i1 $liczbaWierz-1}
- let i2 $i+1; if {$i2==$liczbaWierz} {let i2 0}
- set sasiedzi($i) "$i1 $i2"
- }
- fiber create $liczbaWierz {
- set Cv [bity $id_los]
- set L 50
- set Cv [wyrownaj $L $Cv]
- set roznica ?
- set a ?
- set b ?
- set c ?
- set pCv ?
- wyslij 1 $Cv
- fiber yield
- while {$L > 2} {
- set pCv $kom(0)
- set roznica [porownanieC $Cv $pCv]
- # numer bitu roznicy - iv
- set a [lindex $roznica 0]
- # bit z mojej liczby
- set b [lindex $roznica 1]
- # bit z otrzymanej liczby
- set c [lindex $roznica 2]
- # nowa dlugosc wyrownania
- set L [expr {int(ceil(log($L)/log(2)) + 1)}]
- # a - najmniejszy bit roznicy, jezeli istnieje, to przycinam
- if {$a > -1} {
- # przycinam kolor od $L do najmniejszego bitu roznicy?
- set Cv [string range $Cv [expr [string length $Cv] - $L] [expr [string length $Cv] - $a]]
- set Cv [wyrownaj $L $Cv]
- }
- wyslij 1 $Cv
- fiber yield
- }
- }
- fiber_iterate $obslugaBitow
- Inicjalizacja;
- proc wizualizacja {} {
- fiber_iterate {_puts "$id : $Cv \t pCv: $pCv \t dlugosc: $L \t a: $a b: $b c: $c"}
- }
- fiber yield; runda; wizualizacja
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement