Skip to content

Commit

Permalink
Merge pull request #3 from CetusProtocol/skip_list_u128
Browse files Browse the repository at this point in the history
Skip list u128
  • Loading branch information
CetusProtocol authored Dec 31, 2023
2 parents 054cb5c + be04860 commit 6d2f6a4
Show file tree
Hide file tree
Showing 6 changed files with 765 additions and 16 deletions.
6 changes: 4 additions & 2 deletions sui/Move.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

[move]
version = 0
manifest_digest = "3187E6E23D81B69A26EFE7BA7A92EE008F2F3217F08B987449AAE473BCA93D78"
deps_digest = "F8BBB0CCB2491CA29A3DF03D6F92277A4F3574266507ACD77214D37ECA3F3082"

dependencies = [
{ name = "Sui" },
]

[[move.package]]
name = "MoveStdlib"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "mainnet-v1.6.3", subdir = "crates/sui-framework/packages/move-stdlib" }
source = { git = "https://github.com/MystenLabs/sui.git", rev = "mainnet-v1.15.1", subdir = "crates/sui-framework/packages/move-stdlib" }

[[move.package]]
name = "Sui"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "mainnet-v1.6.3", subdir = "crates/sui-framework/packages/sui-framework" }
source = { git = "https://github.com/MystenLabs/sui.git", rev = "mainnet-v1.15.1", subdir = "crates/sui-framework/packages/sui-framework" }

dependencies = [
{ name = "MoveStdlib" },
Expand Down
6 changes: 3 additions & 3 deletions sui/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# ------------------------
[package]
name = "MoveSTL"
version = "1.1.0"
published-at = "0xfacdeb69fc1354887ab4be6f8bd58b70a2c139ce85eb3cbc18b13013ff75ce73"
version = "1.2.0"
published-at = "0x7118b0382949f8321e995203c59eb60f0d48657d74234946d32b2bf7126ec80d"

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "mainnet-v1.6.3" }
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "mainnet-v1.15.1" }

[addresses]
move_stl = "0xbe21a06129308e0495431d12286127897aff07a8ade3970495a4404d97f9eaaa"
31 changes: 20 additions & 11 deletions sui/sources/linked_table.move
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ module move_stl::linked_table {
#[test_only]
use sui::tx_context;
#[test_only]
use sui::transfer::transfer;
use sui::transfer;

#[test]
fun test_table_new() {
Expand All @@ -195,6 +195,7 @@ module move_stl::linked_table {
}

#[test]
#[lint_allow(self_transfer)]
fun test_table_push_front() {
let ctx = &mut tx_context::dummy();
let table = new<u64, u256>(ctx);
Expand Down Expand Up @@ -227,10 +228,11 @@ module move_stl::linked_table {
assert!(is_none(&node_3.prev), 0);
assert!((is_some(&node_3.next) && (*option::borrow(&node_3.next) == 2)), 0);

transfer(table, tx_context::sender(ctx));
transfer::transfer(table, tx_context::sender(ctx));
}

#[test]
#[lint_allow(self_transfer)]
fun test_table_push_back() {
let ctx = &mut tx_context::dummy();
let table = new<u64, u256>(ctx);
Expand Down Expand Up @@ -263,10 +265,11 @@ module move_stl::linked_table {
assert!((is_some(&node_3.prev) && (*option::borrow(&node_3.prev) == 2)), 0);
assert!(is_none(&node_3.next), 0);

transfer(table, tx_context::sender(ctx));
transfer::transfer(table, tx_context::sender(ctx));
}

#[test]
#[lint_allow(self_transfer)]
fun test_table_remove() {
let ctx = &mut tx_context::dummy();
let table = new<u64, u256>(ctx);
Expand Down Expand Up @@ -317,10 +320,11 @@ module move_stl::linked_table {

assert!(length(&table) == 6, 0);

transfer(table, tx_context::sender(ctx));
transfer::transfer(table, tx_context::sender(ctx));
}

#[test]
#[lint_allow(self_transfer)]
fun test_table_insert_before() {
let ctx = &mut tx_context::dummy();
let table = new<u64, u256>(ctx);
Expand Down Expand Up @@ -369,10 +373,11 @@ module move_stl::linked_table {
assert!(*option::borrow(&table.head) == 1, 0);
assert!(*option::borrow(&table.tail) == 10, 0);

transfer(table, tx_context::sender(ctx));
transfer::transfer(table, tx_context::sender(ctx));
}

#[test]
#[lint_allow(self_transfer)]
fun test_table_insert_after() {
let ctx = &mut tx_context::dummy();
let table = new<u64, u256>(ctx);
Expand Down Expand Up @@ -424,10 +429,11 @@ module move_stl::linked_table {
assert!(*option::borrow(&table.head) == 2, 0);
assert!(*option::borrow(&table.tail) == 11, 0);

transfer(table, tx_context::sender(ctx));
transfer::transfer(table, tx_context::sender(ctx));
}

#[test]
#[lint_allow(self_transfer)]
fun test_table_push_back_bench() {
let ctx = &mut tx_context::dummy();
let table = new<u64, u256>(ctx);
Expand All @@ -436,10 +442,11 @@ module move_stl::linked_table {
push_back(&mut table, n, (n as u256));
n = n + 1;
};
transfer(table, tx_context::sender(ctx));
transfer::transfer(table, tx_context::sender(ctx));
}

#[test]
#[lint_allow(self_transfer)]
fun test_table_push_front_bench() {
let ctx = &mut tx_context::dummy();
let table = new<u64, u256>(ctx);
Expand All @@ -448,10 +455,11 @@ module move_stl::linked_table {
push_front(&mut table, n, (n as u256));
n = n + 1;
};
transfer(table, tx_context::sender(ctx));
transfer::transfer(table, tx_context::sender(ctx));
}

#[test]
#[lint_allow(self_transfer)]
fun test_table_insert_before_bench() {
let ctx = &mut tx_context::dummy();
let table = new<u64, u64>(ctx);
Expand All @@ -464,10 +472,11 @@ module move_stl::linked_table {
current_key = n;
n = n - 1;
};
transfer(table, tx_context::sender(ctx));
transfer::transfer(table, tx_context::sender(ctx));
}

#[test]
#[lint_allow(self_transfer)]
fun test_table_insert_after_bench() {
let ctx = &mut tx_context::dummy();
let table = new<u64, u64>(ctx);
Expand All @@ -480,6 +489,6 @@ module move_stl::linked_table {
current_key = n;
n = n + 1;
};
transfer(table, tx_context::sender(ctx));
transfer::transfer(table, tx_context::sender(ctx));
}
}
}
83 changes: 83 additions & 0 deletions sui/sources/option_u128.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
module move_stl::option_u128 {
#[test_only]
use std::option;

const EOptionU128IsNone: u64 = 0;

struct OptionU128 has copy, drop, store {
is_none: bool,
v: u128
}

public fun some(v: u128): OptionU128 {
OptionU128 {
is_none: false,
v
}
}

public fun none(): OptionU128 {
OptionU128 {
is_none: true,
v: 0
}
}

public fun borrow(opt: &OptionU128): u128 {
assert!(!opt.is_none, EOptionU128IsNone);
opt.v
}

public fun borrow_mut(opt: &mut OptionU128): &mut u128 {
assert!(!opt.is_none, EOptionU128IsNone);
&mut opt.v
}

public fun swap_or_fill(opt: &mut OptionU128, v: u128) {
opt.is_none = false;
opt.v = v;
}

public fun is_some(opt: &OptionU128): bool {
!opt.is_none
}

public fun is_none(opt: &OptionU128): bool {
opt.is_none
}

public fun contains(opt: &OptionU128, e_ref: u128): bool {
if (opt.is_none) {
return false
};
(opt.v == e_ref)
}

public fun is_some_and_eq(opt: &OptionU128, v: u128): bool {
((!opt.is_none) && (opt.v == v))
}

public fun is_some_and_lte(opt: &OptionU128, v: u128): bool {
(!opt.is_none) && (opt.v <= v)
}

#[test]
fun test_opt() {
let a = some(10000u128);
let n = 0;
while (n < 10000) {
_ = borrow(&a);
n = n + 1;
};
}

#[test]
fun test_option_contains() {
let a = option::some(100000);
let n = 0;
while (n < 10000) {
option::contains(&a, &100000);
n = n + 1;
}
}
}
15 changes: 15 additions & 0 deletions sui/sources/skip_list.move
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module move_stl::skip_list {
const ENodeAlreadyExist: u64 = 0;
const ENodeDoesNotExist: u64 = 1;
const ESkipListNotEmpty: u64 = 3;

#[allow(unused_const)]
const ESkipListIsEmpty: u64 = 4;

/// The skip list.
Expand Down Expand Up @@ -365,6 +367,7 @@ module move_stl::skip_list {
}

#[test_only]
#[lint_allow(self_transfer)]
fun check_skip_list<V: store>(list: &SkipList<V>) {
if (list.level == 0) {
assert!(length(list) == 0, 0);
Expand Down Expand Up @@ -434,6 +437,7 @@ module move_stl::skip_list {
}

#[test_only]
#[lint_allow(self_transfer)]
fun get_all_socres<V: store>(list: &SkipList<V>): vector<u64> {
let (opt_next_score,scores ) = (vector::borrow(&list.head, 0), vector::empty<u64>());
while (is_some(opt_next_score)) {
Expand All @@ -446,6 +450,7 @@ module move_stl::skip_list {
}

#[test]
#[lint_allow(self_transfer)]
fun test_new() {
let ctx = &mut tx_context::dummy();
let skip_list = new<u256>(16, 2, 12345, ctx);
Expand All @@ -454,6 +459,7 @@ module move_stl::skip_list {
}

#[test]
#[lint_allow(self_transfer)]
fun test_create_node() {
let ctx = &mut tx_context::dummy();
let skip_list = new<u256>(16, 2, 12345, ctx);
Expand All @@ -468,6 +474,7 @@ module move_stl::skip_list {
}

#[test_only]
#[lint_allow(self_transfer)]
fun add_node_for_test<V: store + copy + drop>(list: &mut SkipList<V>, size: u64, seed: u64, value: V) {
let random = random::new(seed);
let n = 0;
Expand All @@ -492,13 +499,15 @@ module move_stl::skip_list {
}

#[test]
#[lint_allow(self_transfer)]
fun test_insert() {
let ctx = &mut tx_context::dummy();
let list = new_list_for_test<u256>(16, 2, 3000, 1234, 0, ctx);
transfer::transfer(list, tx_context::sender(ctx));
}

#[test]
#[lint_allow(self_transfer)]
fun test_insert_bench() {
let ctx = &mut tx_context::dummy();
let list = new<u256>(16, 2, 100000, ctx);
Expand All @@ -513,13 +522,15 @@ module move_stl::skip_list {
transfer::transfer(list, tx_context::sender(ctx));
}

#[allow(unused_field)]
struct Item has drop, store {
n: u64,
score: u64,
finded: OptionU64
}

#[test]
#[lint_allow(self_transfer)]
fun test_find() {
let ctx = &mut tx_context::dummy();
let list = new_list_for_test<u256>(16, 2, 1000, 12345, 0, ctx);
Expand Down Expand Up @@ -567,6 +578,7 @@ module move_stl::skip_list {
}

#[test]
#[lint_allow(self_transfer)]
fun test_find_bench() {
let ctx = &mut tx_context::dummy();
let list = new_list_for_test<u256>(16, 2, 1000, 12345, 0, ctx);
Expand All @@ -585,6 +597,7 @@ module move_stl::skip_list {
}

#[test]
#[lint_allow(self_transfer)]
fun test_find_next_bench() {
let ctx = &mut tx_context::dummy();
let list = new_list_for_test<u256>(16, 2, 1000, 12345, 0, ctx);
Expand All @@ -599,6 +612,7 @@ module move_stl::skip_list {
}

#[test]
#[lint_allow(self_transfer)]
fun test_remove() {
let ctx = &mut tx_context::dummy();
let list = new_list_for_test<u256>(16, 2, 1000, 5678, 0, ctx);
Expand Down Expand Up @@ -633,6 +647,7 @@ module move_stl::skip_list {
}

#[test]
#[lint_allow(self_transfer)]
fun test_find_in_empty_list() {
let ctx = &mut tx_context::dummy();
let list = new<u256>(16, 2, 1234, ctx);
Expand Down
Loading

0 comments on commit 6d2f6a4

Please sign in to comment.