Aptos Move by Example
  • 🚀Getting Started
  • Set-Up
  • Why is Move Secure
    • Move prover
  • Move vs Solidity
    • Resources
    • Parallel Processing
    • Reentrancy attacks
    • Memory management
    • Smart contract verification
    • Compiled language
  • Basic Concepts
    • Move.toml
    • Primary data-types
    • Strings
    • Comments
    • Functions
    • Function Visibilities
    • Control flow and expressions
    • Loops
    • Error
    • Struct and its Abilities
    • Scripts
    • Operations
  • Intermediate Concepts
    • Local variables
    • Constants
    • Signer
    • Vector
    • Address
    • Uses and Aliases
    • Maps
    • Hash functions
    • References
    • Unit test
    • Generics
    • Type Arguments
    • Type Inference
  • Advanced Concepts
    • Global Storage Structure
    • Global Storage Operations
    • Phantom Type Parameters
    • Timestamps
    • Ownership
    • Move coding conventions
    • View functions
    • Aptos account
    • Aptos Coin
    • Aptos Token(Nft)
    • Object
    • Token V2
  • Applications
    • First App
    • ToDoList
    • Voting System
    • Basic Tokens
    • Storage using Generics
    • Company
    • Collection
    • Football Card
    • Staking Module
    • MultiSender Wallet
    • English Auction
    • Dutch Auction
    • Attendance Sheet
    • Polling Contract
    • Lottery Contract
  • Decentralized Finance
    • Simple Swap Protocol Contract
    • Code of Swapping Protocol
  • Hacks
    • Coming soon
  • Hands on tutorials
    • Indexer tutorials
Powered by GitBook
On this page
Edit on GitHub
  1. Intermediate Concepts

Maps

0x1::simple_map

This module provides a solution for sorted maps, that is it has the properties that

  • Keys point to Values

  • Each Key must be unique

  • A Key can be found within O(Log N) time

  • The data is stored as sorted by Key

  • Adds and removals take O(N) time

Defining an empty map:

let NAME : SimpleMap<TYPE1,TYPE2> = simple_map::create();

Operations on maps:

Lets understand various operations on maps using the module given below.

module my_addrx::Mapping
{
    use std::simple_map::{SimpleMap,Self};
    use std::string::{String,utf8};

    public fun mapping_in_move()
    {
        let mp:SimpleMap<u64,String> = simple_map::create(); //creating an empty map where Key->integer and Value->string.
        
        
        //adding the key and corresponding value in the map
        simple_map::add(&mut mp,1 ,utf8(b"John")); 
        simple_map::add(&mut mp,2 ,utf8(b"Ben"));
        simple_map::add(&mut mp,3 ,utf8(b"Tony"));
        simple_map::add(&mut mp,4 ,utf8(b"Gwen"));
      
        //calculating the length of the map
        let l=simple_map::length(&mut mp);
        assert!(l==4,1);

        //checking if a given key exists or not in the vector
        let x=simple_map::contains_key(&mut mp,&2);
        assert!(x==true,1);

        //removing key value pair in the map
        simple_map::remove(&mut mp,&2);
        assert!(simple_map::contains_key(&mut mp,&2)==false,1);

        //borrowing immutable reference to the value of a given key        
        let v=simple_map::borrow(&mut mp,&3);
        assert!(v==&utf8(b"Tony"),1);

        //borrowing mutable reference to the value of a given key        
        let v=simple_map::borrow_mut(&mut mp,&3);
        *v=utf8(b"Changed Name");
        
        assert!(simple_map::borrow(&mut mp,&3)==&utf8(b"Changed Name"),1);


    }

    #[test]
    fun testing()
    {
        mapping_in_move();
    }
}t
PreviousUses and AliasesNextHash functions

Last updated 1 year ago

For more information on maps follow

this link.