Git Product home page Git Product logo

Comments (2)

JustDoIt0910 avatar JustDoIt0910 commented on July 22, 2024 1

Yes, it's clear. I understand, thank you very much!

from sc.

tezc avatar tezc commented on July 22, 2024

Hi @JustDoIt0910

Internally, map uses an array (struct sc_map_item_str *mem above). struct sc_map_item_xx is like:

struct sc_map_item_##name {
	K key; 
	V value;
	uint32_t hash;
}; 

This array contains some key value pairs, some of them are used some of them are not.
If the key of an array item is zero (key == 0), it means that item/slot is empty. So, we can place the new item there.

What if user wants to store key=0, (say key = 0, value = 15). How are we going to distinguish between "empty indicator" key=0 and this valid item?

Hashmap does a trick, if user wants to put key=0, hashmap stores it as the first item:

sc/map/sc_map.c

Line 232 in f0bb6d9

m->mem[-1].value = value; \

So, how do you know if that special slot is empty or not? Hashmap uses used boolean for that. If user puts key=0 to the map, it will be stored as the first item and used value will be true. So, when user calls get(key=0), we'll know if there is a real value in that special slot.

For pointer types (like str, void*), special key is NULL btw.

Let me know if this is clear. Cheers!

from sc.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.