Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TreeMap impl based on AVL-tree #154

Merged
merged 57 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from 53 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
2f2626d
Rename map => unordered_map
sergey-melnychuk May 16, 2020
83071d8
Add prototype heap impl
sergey-melnychuk May 16, 2020
2cc2086
Add blueprint for HeapMap impl
sergey-melnychuk May 16, 2020
6167713
Update module imports, expose Heap and HeapMap
sergey-melnychuk May 16, 2020
bf469ea
Extract duplicate test code into separate module
sergey-melnychuk May 16, 2020
afd71dc
Extract `prefix` function to address copy-paste code
sergey-melnychuk May 16, 2020
ca483a0
Refactor Heap to deal with indices, add basic unit-tests
sergey-melnychuk May 16, 2020
6962a5b
Cleanup HeapMap, rely on Heap to keep track of indices
sergey-melnychuk May 16, 2020
a86e6a9
Minor cleanup: use prefix util to build id + b'x'
sergey-melnychuk May 17, 2020
eeb2d86
Add append_slice, rename prefix => append, minor cleanup
sergey-melnychuk May 17, 2020
09da6b7
Extract common error messages and (de)serialize util
sergey-melnychuk May 17, 2020
9517d13
Add Heap.{lookup, clear} impl
sergey-melnychuk May 17, 2020
6bb7c75
Add basic methods impl for HeapMap
sergey-melnychuk May 17, 2020
4dbcdd1
Add basic test cases for Heap.{sink, rise, sort}
sergey-melnychuk May 17, 2020
21ef87a
Add impl for HeapMap.{insert, remove, keys, entries}
sergey-melnychuk May 17, 2020
123bba1
Add in-place heap-sort impl, keep track of indices
sergey-melnychuk May 17, 2020
380d69e
Switch to max heap, restore natural ordering in iterators
sergey-melnychuk May 18, 2020
dfa58b3
Add test_{insert, remove, keys, entries} for HeapMap
sergey-melnychuk May 18, 2020
9c04fb3
Add check on insert to avoid adding duplicate values to heap
sergey-melnychuk May 19, 2020
7f36b21
Address review comments
sergey-melnychuk May 19, 2020
c38d3bf
Add AVL-based TreeMap implementation skeleton
sergey-melnychuk May 19, 2020
481eb1a
Add TreeMap.{min, max, floor, ceil, insert} impl and tests
sergey-melnychuk May 21, 2020
f884e08
Reduce tree size in tests to avoid 'GasLimitExceeded' error
sergey-melnychuk May 21, 2020
6e957bc
Remove HeapMap, it is useless in its current state
sergey-melnychuk May 21, 2020
63bfc07
Add TreeMap.remove implementation with restoring tree balance
sergey-melnychuk May 21, 2020
541c5a0
Add check if heap is empty to remove_max, add test case
sergey-melnychuk May 21, 2020
bd5e3b6
Add test case for removing a key from an empty TreeMap
sergey-melnychuk May 21, 2020
cf3d787
Add impl & tests for TreeMap.iter{,_rev}{,_from}
sergey-melnychuk May 22, 2020
7bfa5db
Add extra comments, break too long lines, add few more assertions
sergey-melnychuk May 22, 2020
b9465ba
Cleanup & simplify append{,_slice} utilities
sergey-melnychuk May 22, 2020
c3244d1
Avoid double-lookup in do_remove & add 'T' to tree_prefix
sergey-melnychuk May 22, 2020
4d12ddc
Add TreeMap.range iterator impl & tests
sergey-melnychuk May 22, 2020
939b0a3
Add extra comments to TreeMap.{min_at, max_at, range}
sergey-melnychuk May 23, 2020
76010b8
Add swap_with_last to do_remove to ensure reuse of node ids
sergey-melnychuk May 23, 2020
f855212
Add property-based test TreeMap vs std::collections::BTreeMap
sergey-melnychuk May 23, 2020
23fa9d9
Aling TreeMap API with Map: insert/remove by reference, not value
sergey-melnychuk May 23, 2020
4b05301
Add test env setup option with effectively unlimited gas
sergey-melnychuk May 26, 2020
bbcb07e
Cover range queries with property-based tests against BTreeMap
sergey-melnychuk May 26, 2020
bb94ec0
Fix dependency Map => UnorderedMap in example (fixes build)
sergey-melnychuk May 26, 2020
7db0ca6
Fix dependency Map => UnorderedMap in example (follow up)
sergey-melnychuk May 27, 2020
a9c6339
Address review comments: remove Heap.{lookup, sort}, add into_iter
sergey-melnychuk May 27, 2020
96749a2
Address review comments: add missing .remove(&target), cleanup
sergey-melnychuk May 27, 2020
1b7a85d
Address review comments: add property-based test for tree height
sergey-melnychuk May 27, 2020
3579915
Address review comments: round 1
sergey-melnychuk May 27, 2020
610adaa
Merge branch 'master' into sorted_map
sergey-melnychuk May 27, 2020
45035d5
Address review comments: round 2
sergey-melnychuk May 27, 2020
3666db8
Address review comments: round 3: 1 map instead of 4
sergey-melnychuk May 28, 2020
b1a098b
Add comments for tricky parts to reduce WTFs-per-minute
sergey-melnychuk May 28, 2020
bb1fd0a
Merge branch 'master' into sorted_map
sergey-melnychuk May 28, 2020
80abf6d
Restore after merge from master
sergey-melnychuk May 28, 2020
841f9c6
Merge branch 'master' into sorted_map
sergey-melnychuk May 29, 2020
789087d
Add some comments, update existing ones
sergey-melnychuk May 29, 2020
88bdc94
Address review comments: height, higher/lower, ceil/floor
sergey-melnychuk May 29, 2020
2595759
Address review comments: keep AVL-tree invariants after remove
sergey-melnychuk May 30, 2020
6f039bc
Address review comments: K: Clone, not Copy; V: neither Clone/Copy
sergey-melnychuk May 30, 2020
d1d205f
Cleanup heap, allow duplicate keys, add prop-tests
sergey-melnychuk Jun 1, 2020
8f3bf25
Remove heap
sergey-melnychuk Jun 1, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions examples/fungible-token/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* multiple accounts.
*/
use borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::collections::Map;
use near_sdk::collections::UnorderedMap;
use near_sdk::json_types::U128;
use near_sdk::{env, near_bindgen, AccountId, Balance};

Expand All @@ -27,13 +27,13 @@ pub struct Account {
/// Escrow Account ID hash to the allowance amount.
/// Allowance is the amount of tokens the Escrow Account ID can spent on behalf of the account
/// owner.
pub allowances: Map<Vec<u8>, Balance>,
pub allowances: UnorderedMap<Vec<u8>, Balance>,
}

impl Account {
/// Initializes a new Account with 0 balance and no allowances for a given `account_hash`.
pub fn new(account_hash: Vec<u8>) -> Self {
Self { balance: 0, allowances: Map::new(account_hash) }
Self { balance: 0, allowances: UnorderedMap::new(account_hash) }
}

/// Sets allowance for account `escrow_account_id` to `allowance`.
Expand All @@ -58,7 +58,7 @@ impl Account {
#[derive(BorshDeserialize, BorshSerialize)]
pub struct FungibleToken {
/// sha256(AccountID) -> Account details.
pub accounts: Map<Vec<u8>, Account>,
pub accounts: UnorderedMap<Vec<u8>, Account>,

/// Total supply of the all token.
pub total_supply: Balance,
Expand All @@ -78,7 +78,7 @@ impl FungibleToken {
assert!(env::is_valid_account_id(owner_id.as_bytes()), "Owner's account ID is invalid");
let total_supply = total_supply.into();
assert!(!env::state_exists(), "Already initialized");
let mut ft = Self { accounts: Map::new(b"a".to_vec()), total_supply };
let mut ft = Self { accounts: UnorderedMap::new(b"a".to_vec()), total_supply };
let mut account = ft.get_account(&owner_id);
account.balance = total_supply;
ft.set_account(&owner_id, &account);
Expand Down
6 changes: 3 additions & 3 deletions examples/lockable-fungible-token/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::{AccountId, Balance, env, near_bindgen};
use near_sdk::collections::Map;
use near_sdk::collections::UnorderedMap;
use std::collections::HashMap;
use std::str::FromStr;

Expand Down Expand Up @@ -51,7 +51,7 @@ impl Account {
#[derive(BorshDeserialize, BorshSerialize)]
pub struct FunToken {
/// AccountID -> Account details.
pub accounts: Map<AccountId, Account>,
pub accounts: UnorderedMap<AccountId, Account>,

/// Total supply of the all token.
pub total_supply: Balance,
Expand All @@ -68,7 +68,7 @@ impl FunToken {
#[init]
pub fn new(owner_id: AccountId, total_supply: String) -> Self {
let total_supply = u128::from_str(&total_supply).expect("Failed to parse total supply");
let mut ft = Self { accounts: Map::new(b"a".to_vec()), total_supply };
let mut ft = Self { accounts: UnorderedMap::new(b"a".to_vec()), total_supply };
let mut account = ft.get_account(&owner_id);
account.balance = total_supply;
ft.accounts.insert(&owner_id, &account);
Expand Down
6 changes: 3 additions & 3 deletions examples/status-message-collections/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::collections::{Map, Set};
use near_sdk::collections::{UnorderedMap, Set};
use near_sdk::{env, near_bindgen};

#[global_allocator]
Expand All @@ -8,13 +8,13 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct StatusMessage {
pub records: Map<String, String>,
pub records: UnorderedMap<String, String>,
pub unique_values: Set<String>,
}

impl Default for StatusMessage {
fn default() -> Self {
Self { records: Map::new(b"r".to_vec()), unique_values: Set::new(b"s".to_vec()) }
Self { records: UnorderedMap::new(b"r".to_vec()), unique_values: Set::new(b"s".to_vec()) }
}
}

Expand Down
1 change: 1 addition & 0 deletions near-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ rand = "0.7.2"
trybuild = "1.0"
rustversion = "1.0"
rand_xorshift = "0.2.0"
quickcheck = "0.9.2"
Loading