Wednesday | 04 DEC 2024
[ previous ]
[ next ]

How to Use Hashmaps

Title:
Date: 2023-08-28
Tags:  pick, basic

Table of Contents

  1. Installation
  2. Using Hashmaps

The MAP utilities are a set of hashmap functions written in Pick BASIC for multivalue database environments.

Multivalue systems can mimic hashmaps by using associated lists and the LOCATE statement however this is a linear search and I wanted something that was a bit faster.

You can also use regular hash files as a hashmap but that will involve writing to the disk.

Installation

There are 4 routines that make up the MAP programs and they depend on the multivalue platform having support for string arithmetic.

https://raw.githubusercontent.com/Krowemoh/TCL-Utilities/main/MAP.SET
https://raw.githubusercontent.com/Krowemoh/TCL-Utilities/main/MAP.GET
https://raw.githubusercontent.com/Krowemoh/TCL-Utilities/main/MAP.HAS
https://raw.githubusercontent.com/Krowemoh/TCL-Utilities/main/MAP.DELETE

You can download the above files, compile them and then catalog them to install the MAP functions.

You can also use my package manager, NPM, to install them.

NPM INSTALL BP MAP.SET
NPM INSTALL BP MAP.GET
NPM INSTALL BP MAP.HAS
NPM INSTALL BP MAP.DELETE

Using Hashmaps

The first step is to initialize the MAP. The size is tunable so you can optimize for the number of values you expect to store.

   EQU MAP.SIZE TO 5000
   DIM MAP(MAP.SIZE)
   MAT MAP = ''

You can then add something to the map via MAP.SET:

   CALL MAP.SET(MAT MAP,MAP.SIZE,'key','value')

Retrieve a value with MAP.GET:

   CALL MAP.GET(MAT MAP,MAP.SIZE,'key',VAL)

Check if a key exists with MAP.HAS:

   CALL MAP.HAS(MAT MAP,MAP.SIZE,'key',KEY.EXISTS)

Delete a key with MAP.DELETE:

   CALL MAP.DELETE(MAT MAP,MAP.SIZE,'key')