首页 > 其他分享 >CSE2425 Hash map

CSE2425 Hash map

时间:2024-12-08 18:53:42浏览次数:5  
标签:function map Hash pointer CSE2425 should hash data

CSE2425, C programming lab, course 2020-2021

Final assignment: Hash map

1 Introduction

In this final assignment you will implement a hash map1 . A hash map is a datastructure that associates a key with a value (a chunk of data). Most hash mapsare implemented as an array of so-called buckets. A hash function translatesa given key (e.g., a name) to an index in the array, where the correspondingbucket is stored.Below we will specify the data structures that you have to provide, and thefunctions that you have to implement. This assignment includes two bonusfunctions that can raise your score from pass (C) to good (B) to excellent (A).

2 Testing The first part of the assignment consist of implementing a test set for the hash map. We have created a number of incorrect hash map implementations. Theyourownimplementation by copy&pasting it into the my tests of the Hashmap assignmentin Weblab.3 Hash map structure Define a type HashMap, which represents the hash map data structure.Note: Use typedef such that a HashMap structure can be used without usingHashMap *hm;4 Creating a hash map

  1. Implement a function create_hashmap that returns a pointer to the newlconstructed HashMap structure and has parameter❼ key_space, a size_t2 that represents the number of buckets in the hasmap.1http://en.wikipedia.org/wiki/Hashmap2http://en.wikipedia.org/wiki/Size_t1CSE2425, C programming lab, course 2020-2021This function should allocate enough memoryto fit key_space buckets, and theallocated memory should be zeroed (i.e., NULLed).
  1. A hash function maps a string (i.e. an array of chars ending with a nullcharacter) to an index, so it returns a unsigned int. The parameter of a hashfunction is simply a❼ key, a null-terminated string of characters.As the hash map can only hold up to key_space buckets, using the hash function–for example to lookup a mapping– requires some care; apply modulo key_spaceto the result such that the value will be in the available bucket range.

A default hash function named hash should be implemented. This functionshould sum all ASCII values of the characters of the key.For example:char *key = "AC";unsigned int h = hash(key);=> h = 1325 Inserting data Implement a function insert_data that has parameters❼ hm, a pointer to a hash map;

❼ key, a null-terminated string of characters;❼ data, a void pointer to the source data;❼ resolve_collision, a ResolveCollisionCallback (see below).The function should store the data pointer and a copy of the key in the bucketthat can be found by applying the hash function on the key. In case of acollision, i.e. when there already is data with the same key in the hash map, theresolve_collision function should be called with the the previously storeddata and data as arguments and the returned void pointer should be stored inthe bucket insteadResolveCollisionCallback, a pointer to a function that returns a void pointerand has two parameters:❼ old_data, a void pointer to the previously stored data;

❼ new_data, a void pointer to the data that is being newly inserted.The function should determine what data is stored in the has map in case of akey collision by returning the void pointer to the data that is to be stored.2CSE2425, C programming lab, course 2020-20216 Retrieving data Implement a function get_data that has parameters

❼ hm, a pointer to a hash map;❼ key, a null-terminated string of characters.

The function should return the data pointer 代写CSE2425 Hash map (a void pointer) in the hash mathat is associated with the key. If the key is not present in the hash map, NULLshould be returned.

7 Iterator Implement a function iterate that has parameters❼ hm, a pointer to a hash map;

❼ callback, a pointer to a function that returns nothing (i.e. void) and hastwo parameters:key, a null-terminated string of characters;data, a void pointer to the dataThis function should iterate over the entire hash map. For each data elementit finds, the callback function should be called with the two members of theelement.8 Removing data Implement a function remove_data that has parameters

❼ hm, a pointer to a hash map;

❼ key, a null-terminated string of characters.

❼ destroy_data, a DestroyDataCallback (see below).This function should remove the element in the hash map that is associated withthe given key. If the destroy_data parameter is non-NULL it should be calledwith the data pointer of the element as argument. If the key is not present, thehash map should remain untouched. As the remove_data function cannot fail,its return typeis void.DestroyDataCallback, a pointer to to a function that returns nothing (i.e.void) and has one parameter:❼ data, a void pointer.The function should clean up the data (e.g. free allocated memory).

3CSE2425, C programming lab, course 2020-20219 Deleting a hash map Implement a function delete_hashmap that has parameters❼ hm, a pointer to the hash map thatis to be deleted;❼ destroy_data, a DestroyDataCallback (see 8).The function should deallocate all memory that was allocated by the hash map.If the destroy_data parameter is non-NULL it should be called for every dataelement that is stored in the hash map with the data pointer of the element as

argument.10 Bonus: New hash function Implement a function set_hash_function that has parameters❼ hm, a pointer to a hash map;

❼ hash_function, a pointer to a hash function that returns a unsigned int

and a single parameter:key, a null-terminated string of characters.This function should set hash_function as the new hash function of the hashmap hm. Changing the hashfunction means that a particular key may now behashed to different bucket than it was with the previous hash function. Thehash map must beupdated (rehashed) to reflectthis so that all data in thehash map can still be retrieved with their corresponding keys.11 Bonus: Counting Words Implement a function count_words that has parameters❼ stream, a pointer to a FILE.This function should count the number of times each word in the stream occursusing the hash map you implemented. A word is defined as a sequence of one ormore alphanumeric characters (case sensitive). You may use fscanf3 to read aparticular set of characters from a stream but other solutions are also accepted.The data stored in the hash map should be properly allocated and deallocated,do not simply store an integer that is cast to a pointer type. The return typeof the function is void.3http://en.cppreference.com/w/c/io/fscanf, C programming lab, course 2020-2021Given the input:foo bar_, foo!bar "baz". program should writethe following to the standard output:bar: 2baz: 1foo: 3The order in which the output is printed is not important.12 Submission The assignment should be implemented on Weblab.

❼ All test code should be located in the Testing assignment.

❼ All hash map code should be located in the Hashmap assignment.

❼ Put all the word count source code inside the Wordcount assignment;

❼ If you have implemented the first bonus exercise, add the following macroto your Hashamp submission#define NEW_HASH❼ Do not include a main function. (We will use our own test driver, just like

the example test provided.)Submissions violating the above requirements will be automatically rejected bythe Weblab system

标签:function,map,Hash,pointer,CSE2425,should,hash,data
From: https://www.cnblogs.com/BUS001/p/18593381

相关文章

  • 《掌握Nmap:全面解析网络扫描与安全检测的终极指南》
    nmap#简介(帮助)用法:nmap[扫描类型][选项]{目标指定内容}简介(帮助)用法:nmap[扫描类型][选项]{目标指定内容}一、目标指定:可以传入主机名、IP地址、网络等。例如:scanme.nmap.org、microsoft.com/24、192.168.0.1;10.0.0-255.1-254-iL<输入文件名>:从主......
  • 强大的三个工具类、CountDownLatch 闭锁 、CyclicBarrier 、Semaphore
    文章目录①、CountDownLatch(闭锁)做减法②、CyclicBarrier做加法③、Semaphore信号量一、CountDownLatch(闭锁)做减法1、CountDownLatch主要有两个方法,当一个或多个线程调用await方法时,这些线程会阻塞2、其它线程调用countDown方法会将计数器减1(调用countDown方法的线......
  • PTA DS 7-4 航空公司VIP客户查询 (unordered_map) (C++)(全网最新)
    7-4航空公司VIP客户查询分数25全屏浏览切换布局作者 DS课程组单位 浙江大学不少航空公司都会提供优惠的会员服务,当某顾客飞行里程累积达到一定数量后,可以使用里程积分直接兑换奖励机票或奖励升舱等服务。现给定某航空公司全体会员的飞行记录,要求实现根据身份证号码快......
  • Python模块之random、hashlib、json、time等内置模块语法学习
    Python内置模块语法学习random、hashlib、json、time、datetime、os等内置模块语法学习模块简单理解为就是一个.py后缀的一个文件分为三种:内置模块:python自带,可调用第三方模块:别人设计的,可调用自定义模块:自己编写的,可调用模块之间苦于相互调用,是Python最高级别的组织......
  • C++——哈希表(Hash Table),附加于 Python 中字典区别于联系
    哈希表(HashTable)是一种非常高效的数据结构,用于存储键值对(key-value)。允许我们以非常快的速度进行插入、删除和查找操作,因为这些操作的时间复杂度平均为O(1)。哈希表通过使用哈希函数将键映射到表中的位置,从而实现快速访问。一、【哈希表的基本概念】1、哈希函数:这是一个将......
  • sqlmap基本使用
    1、sqlmap-u"url?id=1"开始注入2、sqlmap-uurl?id=1--current-db爆当前库3、sqlmap-uurl?id=1-D数据库名--tables爆表名4、sqlmap-uurl?id=1-D数据库名-T表名--columns爆字段5、sqlmap-uurl?id=1-D数据库名-T表名-C字段名1,字段名2,字段名3...--d......
  • ORB-SLAM2源码学习:MapPoint.cc:MapPoint::UpdateNormalAndDepth()计算平均观测方向以
    前言这个函数是属于地图点属性的一部分。1.函数声明voidMapPoint::UpdateNormalAndDepth(){....}2.函数定义1.获取观测到该地图点的所有关键帧的信息 map<KeyFrame*,size_t>observations;KeyFrame*pRefKF;cv::MatPos;{unique_lock<m......
  • HashMap Knn和KDtree KNN
    chatgpt3的回答使用HashMap进行KNN(K最近邻算法)和使用KD树进行KNN的主要区别在于数据存储和查询效率。HashMap可以快速存储和访问数据,但在处理高维数据时可能会出现高维诅咒的问题,因此不适合进行空间搜索。KD树通过将数据划分为超矩形区域来组织数据,可以更有效地执行邻近查询,特别......
  • ArcMap如何创建distance_to_railways图片?
    1导入数据        如下图所示,从OSM官网上下载的2015年路网数据,选择gis_osm_railways_free_1.shp数据。通过在ArcGIS中文件夹连接,导入目标数据文件,导入后效果如图所示。2坐标系转化    在实验过程中,优先使用投影坐标系。因此,需要将地理坐标系转化为投......
  • Java MyBatis返回两个字段作为Map的key和value
    使用MyBatis时,可能会遇到这种情况:只查询两个字段,需要返回一个Map,其中第一个字段作为key,第二个字段作为value。这种查询在某些场景非常好用,比如查询字典,查询出的key和value就是字典的value和label,利用HashMap的get方法时间复杂度为O(1)的特点,可以实现字典的快......