NEPLg2 Standard Library - list
Web Playground
Web Playground

list: 単方向たんほうこう連結れんけつリストを扱うライブラリ

目的もくてき:

実装じっそう:

注意ちゅうい:

計算量けいさんりょう:

追加の使い方:

TEST
#entry main
#target std
#import "alloc/collections/list" as *
#import "alloc/string" as *
#import "core/option" as *
fn main <()*>i32> ():
    let base0 <List<i32>>:
        list_nil<i32>
        |> list_push_front<i32> 3
        |> list_push_front<i32> 2
        |> list_push_front<i32> 1
    let ok0 if eq list_len<i32> base0 3 1 0;
    let base1 <List<i32>>:
        list_nil<i32>
        |> list_push_front<i32> 3
        |> list_push_front<i32> 2
        |> list_push_front<i32> 1
    let ok1 match list_head<i32> base1:
        Option::Some x:
            if eq x 1 1 0
        Option::None:
            0
    let base2 <List<i32>>:
        list_nil<i32>
        |> list_push_front<i32> 3
        |> list_push_front<i32> 2
        |> list_push_front<i32> 1
    let ok2 match list_get<i32> base2 1:
        Option::Some x:
            if eq x 2 1 0
        Option::None:
            0
    let base3 <List<i32>>:
        list_nil<i32>
        |> list_push_front<i32> 3
        |> list_push_front<i32> 2
        |> list_push_front<i32> 1
    let ok3 match list_tail<i32> base3:
        Option::Some t:
            if eq list_len<i32> t 2 1 0
        Option::None:
            0
    let s01 <i32> add ok0 ok1;
    let s23 <i32> add ok2 ok3;
    add s01 s23

使い方:

TEST
#entry main
#target std
#import "alloc/collections/list" as *
#import "alloc/string" as *
#import "core/option" as *
fn main <()*>i32> ():
    let src0 <List<str>>:
        list_nil<str>
        |> list_push_front<str> "c"
        |> list_push_front<str> "b"
        |> list_push_front<str> "a"
    let ra list_reverse<str> src0;
    let ok0 match list_head<str> ra:
        Option::Some x:
            if str_eq x "c" 1 0
        Option::None:
            0
    let src1 <List<str>>:
        list_nil<str>
        |> list_push_front<str> "c"
        |> list_push_front<str> "b"
        |> list_push_front<str> "a"
    let rb list_reverse<str> src1;
    let ok1 match list_get<str> rb 2:
        Option::Some x:
            if str_eq x "a" 1 0
        Option::None:
            0
    let src2 <List<str>>:
        list_nil<str>
        |> list_push_front<str> "c"
        |> list_push_front<str> "b"
        |> list_push_front<str> "a"
    let rc list_reverse<str> src2;
    let ok2 if is_none<str> list_get<str> rc 3 1 0;
    let s01 <i32> add ok0 ok1;
    add s01 ok2

List: 単方向連結リストの先頭ポインタを隠蔽する構造体

目的:

実装(アルゴリズム):

注意(重要):

計算量:

list_nil: 空リストを表す値を返す

目的:

実装(アルゴリズム):

注意(重要):

計算量:

使い方:

TEST
#entry main
#target std
#import "alloc/collections/list" as *
fn main <()*>i32> ():
    if list_is_empty<i32> list_nil<i32> 0 1

list_cons: 先頭に要素を追加して新しいリストを作る

目的:

実装(アルゴリズム):

注意(重要):

計算量:

使い方:

TEST
#entry main
#target std
#import "alloc/collections/list" as *
fn main <()*>i32> ():
    let l list_cons<i32> 5 list_nil<i32>;
    if list_is_empty<i32> l 1 0

list_push_front: 既存リストの先頭に要素を追加する(pipe向け)

目的:

実装(アルゴリズム):

注意(重要):

計算量:

list_head: 先頭要素を取得する

目的:

実装(アルゴリズム):

注意(重要):

計算量:

使い方:

TEST
#entry main
#target std
#import "alloc/collections/list" as *
#import "core/option" as *
fn main <()*>i32> ():
    let l list_cons<i32> 8 list_nil<i32>;
    match list_head<i32> l:
        Option::Some x:
            if eq x 8 0 1
        Option::None:
            1

list_tail: 末尾以外の部分(次のノード)を取得する

目的:

実装(アルゴリズム):

注意(重要):

計算量:

使い方:

TEST
#entry main
#target std
#import "alloc/collections/list" as *
#import "core/option" as *
fn main <()*>i32> ():
    let l2 <List<i32>>:
        list_nil<i32>
        |> list_push_front<i32> 2
        |> list_push_front<i32> 1
    match list_tail<i32> l2:
        Option::Some t:
            if eq list_len<i32> t 1 0 1
        Option::None:
            1

list_is_empty: 空リストか判定する

目的:

実装(アルゴリズム):

注意(重要):

計算量:

使い方:

TEST
#entry main
#target std
#import "alloc/collections/list" as *
fn main <()*>i32> ():
    if list_is_empty<i32> list_nil<i32> 0 1

list_len: 長さを数える

目的:

実装(アルゴリズム):

注意(重要):

計算量:

使い方:

TEST
#entry main
#target std
#import "alloc/collections/list" as *
fn main <()*>i32> ():
    let l3 <List<i32>>:
        list_nil<i32>
        |> list_push_front<i32> 3
        |> list_push_front<i32> 2
        |> list_push_front<i32> 1
    if eq list_len<i32> l3 3 0 1

list_get: 指定位置の要素を取得する

目的:

実装(アルゴリズム):

注意(重要):

計算量:

使い方:

TEST
#entry main
#target std
#import "alloc/collections/list" as *
#import "core/option" as *
fn main <()*>i32> ():
    let l3 <List<i32>>:
        list_nil<i32>
        |> list_push_front<i32> 3
        |> list_push_front<i32> 2
        |> list_push_front<i32> 1
    match list_get<i32> l3 1:
        Option::Some x:
            if eq x 2 0 1
        Option::None:
            1

list_free: リスト全体を解放する

目的:

実装(アルゴリズム):

注意(重要):

計算量:

使い方:

TEST
#entry main
#target std
#import "alloc/collections/list" as *
fn main <()*>i32> ():
    let l0 list_nil<i32>;
    let l1 list_cons<i32> 1 l0;
    list_free<i32> l1;
    0

list_reverse: リストを逆順にする

目的:

実装(アルゴリズム):

注意(重要):

計算量:

使い方:

TEST
#entry main
#target std
#import "alloc/collections/list" as *
#import "core/option" as *
fn main <()*>i32> ():
    let l3 <List<i32>>:
        list_nil<i32>
        |> list_push_front<i32> 3
        |> list_push_front<i32> 2
        |> list_push_front<i32> 1
    let r list_reverse<i32> l3;
    match list_head<i32> r:
        Option::Some x:
            if eq x 3 0 1
        Option::None:
            1