Skip to content

Commit

Permalink
Merge pull request #89 from niuez/v0.8
Browse files Browse the repository at this point in the history
v0.8.0
  • Loading branch information
niuez authored Nov 9, 2021
2 parents d190a7a + 9afcd0e commit 5cf47d4
Show file tree
Hide file tree
Showing 48 changed files with 1,812 additions and 126 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "niu"
version = "0.6.1"
version = "0.8.0"
authors = ["niuez <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
nom = "6.0.0"
nom = "7.0.0"
log = "0.4.0"
env_logger = "0.8.4"
clap = "2.33.3"
Expand Down
18 changes: 18 additions & 0 deletions examples/clone.niu
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import "std/opes.niu"
import "std/u64.niu"

struct Hoge {} {}

impl Clone for Hoge {
fn clone(self: &Self) -> Self { Hoge {} }
}
impl Copy for Hoge {}

fn clone_t<T>(t: &T) -> T where T: Copy {
*t
}

fn main() -> void {
clone_t(&Hoge{});
clone_t(&1);
}
27 changes: 27 additions & 0 deletions examples/fib.niu
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import "math/matrix.niu"
import "math/modint.niu"
#include <iostream>

impl Field for Modint<M9982> {
fn zero() -> Self { Modint::init(0) }
fn one() -> Self { Modint::init(1) }
}

fn scan<T>(t: &mut T) -> void $${std::cin >> $arg(t);}$$
fn println<T>(t: &T) -> void $${std::cout << $arg(t) << std::endl;}$$

fn main() -> void {
let mut n = 0;
scan(&mut n);

let mut mat = Matrix<Modint<M9982>>::zero(2, 2);
let mut ini = Matrix::zero(2, 1);
mat[0][1] = Modint::init(1);
mat[1][0] = Modint::init(1);
mat[1][1] = Modint::init(1);
ini[0][0] = Modint::init(0);
ini[1][0] = Modint::init(1);

let ans = &mat.pow(n) * &ini;
println(&ans[0][0].a);
}
21 changes: 21 additions & 0 deletions examples/foreach.niu
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import "std/vec.niu"
import "std/u64.niu"
#include <iostream>

fn println(a: u64) -> void $${std::cout << $arg(a) << std::endl;}$$

fn main() -> void {
let mut v = Vec::new();
for i = 0; i < 5; i = i + 1 {
v.push(i);
}
for e in &v {
println(*e);
}
for e in &mut v {
*e = *e + 10;
}
for e in v {
println(e);
}
}
6 changes: 6 additions & 0 deletions examples/if.niu
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import "std/u64.niu"

fn main() -> void {
let a = 2;
let b = 5;
Expand All @@ -7,4 +9,8 @@ fn main() -> void {
else {
b / a
};

if a == b {
let c = a + b;
}
}
31 changes: 31 additions & 0 deletions examples/map.niu
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import "std/map.niu"
import "std/u64.niu"
#include <iostream>

fn println<T>(t: &T) -> void $${std::cout << $arg(t) << std::endl;}$$

fn main() -> void {
let mut mp = Map::empty();
mp.insert(1, 1i64);
mp.insert(5, 5i64);
mp.insert(4, 4i64);
mp.insert(2, 2i64);
mp.insert(3, 3i64);

for x in &mp {
println(&x.0);
println(&x.1);
}

for i = 1; i <= 5; i = i + 1 {
println(&mp[&i]);
}

mp.remove(&3);

for x in mp {
let (k, v) = x;
println(&k);
println(&v);
}
}
26 changes: 26 additions & 0 deletions examples/move_check.niu
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import "std/u64.niu"

struct Hoge {
s: u64,
t: u64,
} {
fn print(self: &Self) -> void {}
fn move_hoge(self: Self) -> void {}
}

fn main() -> void {
let mut hoge = Hoge { s: 9, t: 1 };
let b = hoge.s;
let c = hoge.t;
hoge.s = 91;
hoge.t = 19;
hoge.print();
let huga = hoge;
huga.move_hoge();

let mut v = 100;
for i = 0; i < 5; i = i + 1 {
let w = v;
v = 120;
}
}
21 changes: 21 additions & 0 deletions examples/set.niu
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import "std/set.niu"
import "std/u64.niu"
#include <iostream>

fn println<T>(t: &T) -> void $${std::cout << $arg(t) << std::endl;}$$

fn main() -> void {
let mut st = Set::empty();
st.insert(5);
st.insert(2);
st.insert(3);
st.insert(1);
st.insert(4);
for x in &st {
println(x);
}
st.remove(&3);
for x in st {
println(&x);
}
}
28 changes: 28 additions & 0 deletions examples/tuple.niu
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import "std/tuple.niu"
import "std/u64.niu"

struct Hoge {
val: u64
} {}

fn throw_tuple(xyz: (u64, i64, bool)) -> (u64, i64, bool) {
xyz
}

fn main() -> void {
let (x, y, z) = throw_tuple((91u64, 19i64, false, ));

let (a, b, (c, d)) = (1, 2, (3, 4));

let t = (1, 2);
let t0 = t.0;
let t1 = t.1;

let mut tt = (Hoge { val: 8 }, false);
let tt0 = tt.0;
tt.0 = tt0;

let ttt = tt;

let copy_t = t;
}
12 changes: 7 additions & 5 deletions lib/data_structure/segment_tree.niu
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ struct SegmentTree<T> where T: Monoid {
node: Vec<T>,
n: u64,
} {
fn init(arr: &Vec<T>) -> Self {
fn init(arr: Vec<T>) -> Self {
let mut n = 1;
for i = 1; n < arr.len(); n = n * 2 {}
let mut node = Vec::init(2 * n, T#Monoid::ide());
for i = 0; i < arr.len(); i = i + 1 {
node[i + n] = arr[i];
let mut j = 0;
for x in arr {
node[j + n] = x;
j = j + 1;
}
for i = n - 1; i >= 1; i = i - 1 {
node[i] = node[i * 2].ope(&node[i * 2 + 1]);
Expand All @@ -43,11 +45,11 @@ struct SegmentTree<T> where T: Monoid {
if (i & 1) == 1 {
lx = lx.ope(&self.node[i]);
i = i + 1;
} else {}
}
if (j & 1) == 1 {
j = j - 1;
rx = self.node[j].ope(&rx);
} else {}
}
i = i >> 1;
}
lx.ope(&rx)
Expand Down
14 changes: 12 additions & 2 deletions lib/data_structure/segment_tree_test.niu
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ struct Am {
}
}

impl Clone for Am {
fn clone(self: &Self) -> Self { Am { v: self.v } }
}
impl Copy for Am {}

impl Monoid for Am {
fn ide() -> Am { Am::init(0i64) }
fn ope(self: &Am, right: &Am) -> Am {
Expand All @@ -32,6 +37,11 @@ impl Monoid for Fm {
}
}

impl Clone for Fm {
fn clone(self: &Self) -> Self { Fm { a: self.a, b: self.b } }
}
impl Copy for Fm {}


testfn(library-checker-problems:datastructure/point_add_range_sum) segmenttree_point_add_range_sum $${
std::cin.tie(nullptr);
Expand All @@ -44,7 +54,7 @@ testfn(library-checker-problems:datastructure/point_add_range_sum) segmenttree_p
a[i] = Am::init(v);
}
using Seg = SegmentTree<Am>;
auto seg = Seg::init(a);
auto seg = Seg::init(std::move(a));
while(Q--) {
int t, a, b;
std::cin >> t >> a >> b;
Expand All @@ -68,7 +78,7 @@ testfn(library-checker-problems:datastructure/point_set_range_composite) segment
a[i] = Fm::init(v, w);
}
using Seg = SegmentTree<Fm>;
auto seg = Seg::init(a);
auto seg = Seg::init(std::move(a));
while(Q--) {
int t, a, b, c;
std::cin >> t >> a >> b >> c;
Expand Down
2 changes: 0 additions & 2 deletions lib/data_structure/union_find.niu
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ struct UnionFind {
self.par[yr] = xr;
}
}
else {
}
}
}

Expand Down
22 changes: 14 additions & 8 deletions lib/math/matrix.niu
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import "std/u64.niu"
trait Field where
Self: Add<Self>(Output=Self),
Self: Sub<Self>(Output=Self),
Self: Mul<Self>(Output=Self) {
Self: Mul<Self>(Output=Self),
Self: Copy {
fn zero() -> Self;
fn one() -> Self;
}
Expand Down Expand Up @@ -37,14 +38,13 @@ struct Matrix<F> where F: Field
}
}
fn pow(self: &Self, p: u64) -> Self {
let mut now = *self;
let mut now = self.clone();
let mut ans = Matrix::one(self.h);
for(let mut exp = p; exp > 0; exp = exp >> 1) {
if exp & 1 == 1 {
ans = ans * now;
ans = &ans * &now;
}
else {}
now = now * now;
now = &now * &now;
}
ans
}
Expand Down Expand Up @@ -92,10 +92,10 @@ impl<F> Sub<Matrix<F>> for Matrix<F> where
}
}

impl<F> Mul<Matrix<F>> for Matrix<F> where
impl<F> Mul<&Matrix<F>> for &Matrix<F> where
F: Field {
type Output = Self;
fn mul(mut self: Self, right: Self) -> Self {
type Output = Matrix<F>;
fn mul(self: &Matrix<F>, right: &Matrix<F>) -> Matrix<F> {
let mut out = Matrix<F>::zero(self.h, right.w);
for(let mut i = 0; i < self.h; i = i + 1) {
for(let mut k = 0; k < right.w; k = k + 1) {
Expand All @@ -107,3 +107,9 @@ impl<F> Mul<Matrix<F>> for Matrix<F> where
out
}
}

impl<F> Clone for Matrix<F> where F: Field {
fn clone(self: &Self) -> Self {
Matrix { a: self.a.clone(), h: self.h, w: self.w }
}
}
5 changes: 4 additions & 1 deletion lib/math/modint.niu
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ struct Modint<M> where M: Mod {
if exp & 1 == 1 {
ans = ans * now;
}
else {}
now = now * now;
}
ans
Expand Down Expand Up @@ -85,3 +84,7 @@ impl<M> Neg for Modint<M> where M: Mod {
}
}
}
impl<M> Clone for Modint<M> where M: Mod {
fn clone(self: &Self) -> Self { Modint { a: self.a } }
}
impl<M> Copy for Modint<M> where M: Mod {}
2 changes: 2 additions & 0 deletions lib/std/bool.niu
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ impl Not for bool {
type Output = bool;
fn not(a: Self) -> bool $${!a}$$
}
impl Clone for bool { fn clone(a: &Self) -> Self $${a}$$ }
impl Copy for bool {}
2 changes: 2 additions & 0 deletions lib/std/f64.niu
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ impl Eq for f64 {
impl Ord for f64 {
fn le(a: &f64, b: &f64) -> bool $${a < b}$$
}
impl Clone for f64 { fn clone(a: &Self) -> Self $${a}$$ }
impl Copy for f64 {}
2 changes: 2 additions & 0 deletions lib/std/i64.niu
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ impl Eq for i64 {
impl Ord for i64 {
fn le(a: &i64, b: &i64) -> bool $${a < b}$$
}
impl Clone for i64 { fn clone(a: &Self) -> Self $${a}$$ }
impl Copy for i64 {}
Loading

0 comments on commit 5cf47d4

Please sign in to comment.