From 48be319379dd178872b65688d44b57197f1a2178 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Fri, 7 Aug 2015 16:26:13 -0400 Subject: [PATCH] lib/standard: Remove lshift and rshift from Int and Byte Signed-off-by: Lucas Bajolet --- .../pep8analysis/src/ast/suffixed_instructions.nit | 4 +-- lib/bitmap/bitmap.nit | 14 ++++---- lib/perfect_hashing.nit | 6 ++-- lib/standard/kernel.nit | 36 +++++++------------- lib/standard/math.nit | 10 +++--- lib/standard/text/abstract_text.nit | 2 +- lib/standard/text/flat.nit | 2 +- src/compiler/abstract_compiler.nit | 12 ------- src/compiler/java_compiler.nit | 8 ++--- tests/bench_add_all.nit | 2 +- tests/bench_bintree_gen.nit | 2 +- tests/bench_netsim.nit | 2 +- tests/bench_nsieve_bool.nit | 2 +- tests/bench_send.nit | 2 +- tests/bench_send2.nit | 2 +- tests/shootout_binarytrees.nit | 2 +- tests/shootout_mandelbrot.nit | 6 ++-- tests/shootout_nsieve.nit | 2 +- tests/shootout_nsieve_bytes_alt.nit | 2 +- 19 files changed, 47 insertions(+), 71 deletions(-) diff --git a/contrib/pep8analysis/src/ast/suffixed_instructions.nit b/contrib/pep8analysis/src/ast/suffixed_instructions.nit index 6466430..ea9f4e0 100644 --- a/contrib/pep8analysis/src/ast/suffixed_instructions.nit +++ b/contrib/pep8analysis/src/ast/suffixed_instructions.nit @@ -32,7 +32,7 @@ end # redef class AUnaryNopInstruction super ADigitSuffixed - redef fun digit_max do return 1.lshift(2)-1 + redef fun digit_max do return (1 << 2)-1 end redef class ANotInstruction @@ -49,7 +49,7 @@ end redef class ARetInstruction super ADigitSuffixed - redef fun digit_max do return 1.lshift(3)-1 + redef fun digit_max do return (1 << 3)-1 end redef abstract class AArithmeticInstruction diff --git a/lib/bitmap/bitmap.nit b/lib/bitmap/bitmap.nit index 505abcf..47475e7 100644 --- a/lib/bitmap/bitmap.nit +++ b/lib/bitmap/bitmap.nit @@ -200,9 +200,9 @@ class Bitmap private fun set_value(array: Array[Int], start_index: Int, value: Int) do array[start_index] = value.bin_and(0x000000FF) - array[start_index + 1] = value.rshift(8).bin_and(0x000000FF) - array[start_index + 2] = value.rshift(16).bin_and(0x000000FF) - array[start_index + 3] = value.rshift(24).bin_and(0x000000FF) + array[start_index + 1] = (value >> 8).bin_and(0x000000FF) + array[start_index + 2] = (value >> 16).bin_and(0x000000FF) + array[start_index + 3] = (value >> 24).bin_and(0x000000FF) end # Saves the bitmap into a file @@ -223,8 +223,8 @@ class Bitmap var row = self.data[x] for y in [0..self.width[ do var pixel = row[y] - var red = pixel.rshift(16) - var green = pixel.bin_and(0x00FF00).rshift(8) + var red = pixel >> 16 + var green = pixel.bin_and(0x00FF00) >> 8 var blue = pixel.bin_and(0x000000FF) fw.write(red.ascii.to_s) fw.write(green.ascii.to_s) @@ -241,8 +241,8 @@ class Bitmap var row = self.data[x] for y in [0..self.width[ do var pixel = row[y] - var red = pixel.rshift(16) - var green = pixel.bin_and(0x00FF00).rshift(8) + var red = pixel >> 16 + var green = pixel.bin_and(0x00FF00) >> 8 var blue = pixel.bin_and(0x000000FF) var lum = (0.2126 * red.to_f + 0.7152 * green.to_f + 0.0722 * blue.to_f).to_i pixel = lum * 256 * 256 + lum * 256 + lum diff --git a/lib/perfect_hashing.nit b/lib/perfect_hashing.nit index 9221116..38808cb 100644 --- a/lib/perfect_hashing.nit +++ b/lib/perfect_hashing.nit @@ -66,7 +66,7 @@ class Perfecthashing var i = mask.highest_bit while i != 0 do if mask.getbit(i) == 1 then - newmask = mask.bin_xor(1.lshift(i)) + newmask = mask.bin_xor(1 << i) # If there is no collision, replace the old mask if phandp(ids, newmask) then @@ -112,10 +112,10 @@ class Perfecthashing do var mask = phand(ids) -1 var i = 0 - while n+ids.length > (1.lshift(mask.number_bits(1))) do + while n+ids.length > (1 << mask.number_bits(1)) do # When there are not enough 1-bits if mask.getbit(i) == 0 then - mask = mask.bin_xor(1.lshift(i)) + mask = mask ^ (1 << i) end i += 1 end diff --git a/lib/standard/kernel.nit b/lib/standard/kernel.nit index e12b3b0..686334d 100644 --- a/lib/standard/kernel.nit +++ b/lib/standard/kernel.nit @@ -641,21 +641,15 @@ universal Byte redef fun zero do return 0.to_b redef fun value_of(val) do return val.to_b - # `i` bits shift fo the left (aka <<) + # `i` bits shift fo the left # - # assert 5.to_b.lshift(1) == 10.to_b - fun lshift(i: Int): Byte is intern + # assert 5u8 << 1 == 10u8 + fun <<(i: Int): Byte `{ return self << i; `} - # alias of `lshift` - fun <<(i: Int): Byte do return lshift(i) - - # `i` bits shift fo the right (aka >>) + # `i` bits shift fo the right # - # assert 5.to_b.rshift(1) == 2.to_b - fun rshift(i: Int): Byte is intern - - # alias of `rshift` - fun >>(i: Int): Byte do return rshift(i) + # assert 5u8 >> 1 == 2u8 + fun >>(i: Int): Byte `{ return self >> i; `} redef fun to_i is intern redef fun to_f is intern @@ -741,21 +735,15 @@ universal Int redef fun zero do return 0 redef fun value_of(val) do return val.to_i - # `i` bits shift fo the left (aka <<) + # `i` bits shift fo the left # - # assert 5.lshift(1) == 10 - fun lshift(i: Int): Int is intern + # assert 5 << 1 == 10 + fun <<(i: Int): Int `{ return self << i; `} - # alias of `lshift` - fun <<(i: Int): Int do return lshift(i) - - # `i` bits shift fo the right (aka >>) + # `i` bits shift fo the right # - # assert 5.rshift(1) == 2 - fun rshift(i: Int): Int is intern - - # alias of `rshift` - fun >>(i: Int): Int do return rshift(i) + # assert 5 >> 1 == 2 + fun >>(i: Int): Int `{ return self >> i; `} redef fun to_i do return self redef fun to_f is intern diff --git a/lib/standard/math.nit b/lib/standard/math.nit index 337355a..3e02ebe 100644 --- a/lib/standard/math.nit +++ b/lib/standard/math.nit @@ -116,14 +116,14 @@ redef class Int if o == 0 then return self if self.bin_and(1) == 0 then if o.bin_and(1) == 1 then - return self.rshift(1).gcd(o) + return (self >> 1).gcd(o) else - return self.rshift(1).gcd(o.rshift(1)).lshift(1) + return (self >> 1).gcd(o >> 1) << 1 end end - if o.bin_and(1) == 0 then return self.gcd(o.rshift(1)) - if self > o then return (self - o).rshift(1).gcd(o) - return (o - self).rshift(1).gcd(self) + if o & 1 == 0 then return self.gcd(o >> 1) + if self > o then return ((self - o) >> 1).gcd(o) + return ((o - self) >> 1).gcd(self) end # Is `self` even ? diff --git a/lib/standard/text/abstract_text.nit b/lib/standard/text/abstract_text.nit index 22f25f2..d7b915c 100644 --- a/lib/standard/text/abstract_text.nit +++ b/lib/standard/text/abstract_text.nit @@ -1046,7 +1046,7 @@ abstract class Text for i in [0..length[ do var char = chars[i] - h = h.lshift(5) + h + char.ascii + h = (h << 5) + h + char.ascii end hash_cache = h diff --git a/lib/standard/text/flat.nit b/lib/standard/text/flat.nit index 26c1a90..87381e6 100644 --- a/lib/standard/text/flat.nit +++ b/lib/standard/text/flat.nit @@ -317,7 +317,7 @@ class FlatString var myitems = items while i <= last_byte do - h = h.lshift(5) + h + myitems[i].to_i + h = (h << 5) + h + myitems[i].to_i i += 1 end diff --git a/src/compiler/abstract_compiler.nit b/src/compiler/abstract_compiler.nit index b1245d2..78d2c31 100644 --- a/src/compiler/abstract_compiler.nit +++ b/src/compiler/abstract_compiler.nit @@ -2141,12 +2141,6 @@ redef class AMethPropdef else if pname == "%" then v.ret(v.new_expr("{arguments[0]} % {arguments[1]}", ret.as(not null))) return true - else if pname == "lshift" then - v.ret(v.new_expr("{arguments[0]} << {arguments[1]}", ret.as(not null))) - return true - else if pname == "rshift" then - v.ret(v.new_expr("{arguments[0]} >> {arguments[1]}", ret.as(not null))) - return true else if pname == "==" then v.ret(v.equal_test(arguments[0], arguments[1])) return true @@ -2240,12 +2234,6 @@ redef class AMethPropdef else if pname == "%" then v.ret(v.new_expr("{arguments[0]} % {arguments[1]}", ret.as(not null))) return true - else if pname == "lshift" then - v.ret(v.new_expr("{arguments[0]} << {arguments[1]}", ret.as(not null))) - return true - else if pname == "rshift" then - v.ret(v.new_expr("{arguments[0]} >> {arguments[1]}", ret.as(not null))) - return true else if pname == "==" then v.ret(v.equal_test(arguments[0], arguments[1])) return true diff --git a/src/compiler/java_compiler.nit b/src/compiler/java_compiler.nit index f1e3437..f40ce45 100644 --- a/src/compiler/java_compiler.nit +++ b/src/compiler/java_compiler.nit @@ -1588,10 +1588,10 @@ redef class AMethPropdef else if pname == "%" then v.ret(v.new_expr("{arguments[0]} % {arguments[1]}", ret.as(not null))) return true - else if pname == "lshift" then + else if pname == "<<" then v.ret(v.new_expr("{arguments[0]} << {arguments[1]}", ret.as(not null))) return true - else if pname == "rshift" then + else if pname == ">>" then v.ret(v.new_expr("{arguments[0]} >> {arguments[1]}", ret.as(not null))) return true else if pname == "==" then @@ -1692,10 +1692,10 @@ redef class AMethPropdef else if pname == "%" then v.ret(v.new_expr("(byte)({arguments[0]} % {arguments[1]})", ret.as(not null))) return true - else if pname == "lshift" then + else if pname == "<<" then v.ret(v.new_expr("(byte)({arguments[0]} << {arguments[1]})", ret.as(not null))) return true - else if pname == "rshift" then + else if pname == ">>" then v.ret(v.new_expr("(byte)({arguments[0]} >> {arguments[1]})", ret.as(not null))) return true else if pname == "==" then diff --git a/tests/bench_add_all.nit b/tests/bench_add_all.nit index 0c9bc5e..35373c7 100644 --- a/tests/bench_add_all.nit +++ b/tests/bench_add_all.nit @@ -17,7 +17,7 @@ var n = 10 if args.not_empty then n = args.first.to_i -var nn = 1.lshift(n) +var nn = 1 << n var a = new Array[Numeric] var b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] diff --git a/tests/bench_bintree_gen.nit b/tests/bench_bintree_gen.nit index 2fc5a8a..c226df9 100644 --- a/tests/bench_bintree_gen.nit +++ b/tests/bench_bintree_gen.nit @@ -72,7 +72,7 @@ var long_lived_tree = bottom_up_tree(0, max_depth) var depth = min_depth while depth <= max_depth do - var iterations = 1.lshift(max_depth - depth + min_depth) + var iterations = 1 << (max_depth - depth + min_depth) var check_res = 0 for i in [1..(iterations+1)[ do diff --git a/tests/bench_netsim.nit b/tests/bench_netsim.nit index 0e13e59..6531814 100644 --- a/tests/bench_netsim.nit +++ b/tests/bench_netsim.nit @@ -272,6 +272,6 @@ if not args.is_empty then nb = args.first.to_i end -s.run_for(1.lshift(nb)) +s.run_for(1 << nb) print(c1.count) diff --git a/tests/bench_nsieve_bool.nit b/tests/bench_nsieve_bool.nit index 634e3cb..a5e13ce 100644 --- a/tests/bench_nsieve_bool.nit +++ b/tests/bench_nsieve_bool.nit @@ -38,7 +38,7 @@ end fun test(n: Int) do - var m = 1000.lshift(n) + var m = 1000 << n print("Primes up to {m} {nsieve(m)}") end diff --git a/tests/bench_send.nit b/tests/bench_send.nit index d3c1b7c..0937117 100644 --- a/tests/bench_send.nit +++ b/tests/bench_send.nit @@ -104,7 +104,7 @@ var i = 0 var n = 10 if args.not_empty then n = args.first.to_i -while i < 1.lshift(n) do +while i < 1 << n do a.hop(b, c, d) i = i + 1 end diff --git a/tests/bench_send2.nit b/tests/bench_send2.nit index a4f53ad..6e70969 100644 --- a/tests/bench_send2.nit +++ b/tests/bench_send2.nit @@ -67,7 +67,7 @@ for i in [0..(nb/6)[ do a[i*6+4] = new E a[i*6+5] = new F end -for i in [0..1.lshift(n)[ do +for i in [0..1 << n[ do for j in [0..nb[ do a[j].foo end diff --git a/tests/shootout_binarytrees.nit b/tests/shootout_binarytrees.nit index a171b58..5eea74f 100644 --- a/tests/shootout_binarytrees.nit +++ b/tests/shootout_binarytrees.nit @@ -75,7 +75,7 @@ var long_lived_tree = bottom_up_tree(0, max_depth) var depth = min_depth while depth <= max_depth do - var iterations = 1.lshift(max_depth - depth + min_depth) + var iterations = 1 << (max_depth - depth + min_depth) var check_res = 0 for i in [1..(iterations+1)[ do diff --git a/tests/shootout_mandelbrot.nit b/tests/shootout_mandelbrot.nit index f51a3d8..7970971 100644 --- a/tests/shootout_mandelbrot.nit +++ b/tests/shootout_mandelbrot.nit @@ -54,9 +54,9 @@ for y in [0..h[ do end if zr*zr+zi*zi > limit*limit then - byte_acc = (byte_acc.lshift(1)) + byte_acc = byte_acc << 1 else - byte_acc = (byte_acc.lshift(1)) + 1u8 + byte_acc = (byte_acc << 1) + 1u8 end bit_num = bit_num + 1 @@ -66,7 +66,7 @@ for y in [0..h[ do byte_acc = 0u8 bit_num = 0 else if x == w - 1 then - byte_acc = byte_acc.lshift(8-w%8) + byte_acc = byte_acc << (8-w%8) stdout.write_byte(byte_acc) byte_acc = 0u8 bit_num = 0 diff --git a/tests/shootout_nsieve.nit b/tests/shootout_nsieve.nit index 109b70f..1e7eda6 100644 --- a/tests/shootout_nsieve.nit +++ b/tests/shootout_nsieve.nit @@ -59,7 +59,7 @@ end fun test(n: Int) do - var m = 10000.lshift(n) + var m = 10000 << n print("Primes up to {m} {nsieve(m)}") end diff --git a/tests/shootout_nsieve_bytes_alt.nit b/tests/shootout_nsieve_bytes_alt.nit index ab5c703..b5ad0cb 100644 --- a/tests/shootout_nsieve_bytes_alt.nit +++ b/tests/shootout_nsieve_bytes_alt.nit @@ -55,7 +55,7 @@ end fun test(n: Int) do - var m = 10000.lshift(n) + var m = 10000 << n print("Primes up to {m} {nsieve(m)}") end -- 1.7.9.5