Update array_hash_map

This commit is contained in:
krahets 2023-03-15 03:11:43 +08:00
parent 669388128f
commit d5afd93571
10 changed files with 98 additions and 98 deletions

View File

@ -20,11 +20,11 @@ public:
/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
private:
vector<Entry*> bucket;
vector<Entry*> buckets;
public:
ArrayHashMap() {
// 初始化一个长度为 100 的桶(数组)
bucket= vector<Entry*>(100);
buckets= vector<Entry*>(100);
}
/* 哈希函数 */
@ -36,7 +36,7 @@ public:
/* 查询操作 */
string get(int key) {
int index = hashFunc(key);
Entry* pair = bucket[index];
Entry* pair = buckets[index];
if (pair == nullptr)
return nullptr;
return pair->val;
@ -46,20 +46,20 @@ public:
void put(int key, string val) {
Entry* pair = new Entry(key, val);
int index = hashFunc(key);
bucket[index] = pair;
buckets[index] = pair;
}
/* 删除操作 */
void remove(int key) {
int index = hashFunc(key);
// 置为 nullptr ,代表删除
bucket[index] = nullptr;
buckets[index] = nullptr;
}
/* 获取所有键值对 */
vector<Entry*> entrySet() {
vector<Entry*> entrySet;
for (Entry* pair: bucket) {
for (Entry* pair: buckets) {
if (pair != nullptr) {
entrySet.push_back(pair);
}
@ -70,7 +70,7 @@ public:
/* 获取所有键 */
vector<int> keySet() {
vector<int> keySet;
for (Entry* pair: bucket) {
for (Entry* pair: buckets) {
if (pair != nullptr) {
keySet.push_back(pair->key);
}
@ -81,7 +81,7 @@ public:
/* 获取所有值 */
vector<string> valueSet() {
vector<string> valueSet;
for (Entry* pair: bucket) {
for (Entry* pair: buckets) {
if (pair != nullptr){
valueSet.push_back(pair->val);
}

View File

@ -23,14 +23,14 @@ class Entry
/* 基于数组简易实现的哈希表 */
class ArrayHashMap
{
private List<Entry?> bucket;
private List<Entry?> buckets;
public ArrayHashMap()
{
// 初始化一个长度为 100 的桶数组
bucket = new();
buckets = new();
for (int i = 0; i < 100; i++)
{
bucket.Add(null);
buckets.Add(null);
}
}
@ -45,7 +45,7 @@ class ArrayHashMap
public String? get(int key)
{
int index = hashFunc(key);
Entry? pair = bucket[index];
Entry? pair = buckets[index];
if (pair == null) return null;
return pair.val;
}
@ -55,7 +55,7 @@ class ArrayHashMap
{
Entry pair = new Entry(key, val);
int index = hashFunc(key);
bucket[index] = pair;
buckets[index] = pair;
}
/* 删除操作 */
@ -63,14 +63,14 @@ class ArrayHashMap
{
int index = hashFunc(key);
// 置为 null 代表删除
bucket[index] = null;
buckets[index] = null;
}
/* 获取所有键值对 */
public List<Entry> entrySet()
{
List<Entry> entrySet = new();
foreach (Entry? pair in bucket)
foreach (Entry? pair in buckets)
{
if (pair != null)
entrySet.Add(pair);
@ -82,7 +82,7 @@ class ArrayHashMap
public List<int> keySet()
{
List<int> keySet = new();
foreach (Entry? pair in bucket)
foreach (Entry? pair in buckets)
{
if (pair != null)
keySet.Add(pair.key);
@ -94,7 +94,7 @@ class ArrayHashMap
public List<String> valueSet()
{
List<String> valueSet = new();
foreach (Entry? pair in bucket)
foreach (Entry? pair in buckets)
{
if (pair != null)
valueSet.Add(pair.val);

View File

@ -14,14 +14,14 @@ type entry struct {
/* 基于数组简易实现的哈希表 */
type arrayHashMap struct {
bucket []*entry
buckets []*entry
}
/* 初始化哈希表 */
func newArrayHashMap() *arrayHashMap {
// 初始化一个长度为 100 的桶(数组)
bucket := make([]*entry, 100)
return &arrayHashMap{bucket: bucket}
buckets := make([]*entry, 100)
return &arrayHashMap{buckets: buckets}
}
/* 哈希函数 */
@ -33,7 +33,7 @@ func (a *arrayHashMap) hashFunc(key int) int {
/* 查询操作 */
func (a *arrayHashMap) get(key int) string {
index := a.hashFunc(key)
pair := a.bucket[index]
pair := a.buckets[index]
if pair == nil {
return "Not Found"
}
@ -44,20 +44,20 @@ func (a *arrayHashMap) get(key int) string {
func (a *arrayHashMap) put(key int, val string) {
pair := &entry{key: key, val: val}
index := a.hashFunc(key)
a.bucket[index] = pair
a.buckets[index] = pair
}
/* 删除操作 */
func (a *arrayHashMap) remove(key int) {
index := a.hashFunc(key)
// 置为 nil ,代表删除
a.bucket[index] = nil
a.buckets[index] = nil
}
/* 获取所有键对 */
func (a *arrayHashMap) entrySet() []*entry {
var pairs []*entry
for _, pair := range a.bucket {
for _, pair := range a.buckets {
if pair != nil {
pairs = append(pairs, pair)
}
@ -68,7 +68,7 @@ func (a *arrayHashMap) entrySet() []*entry {
/* 获取所有键 */
func (a *arrayHashMap) keySet() []int {
var keys []int
for _, pair := range a.bucket {
for _, pair := range a.buckets {
if pair != nil {
keys = append(keys, pair.key)
}
@ -79,7 +79,7 @@ func (a *arrayHashMap) keySet() []int {
/* 获取所有值 */
func (a *arrayHashMap) valueSet() []string {
var values []string
for _, pair := range a.bucket {
for _, pair := range a.buckets {
if pair != nil {
values = append(values, pair.val)
}
@ -89,7 +89,7 @@ func (a *arrayHashMap) valueSet() []string {
/* 打印哈希表 */
func (a *arrayHashMap) print() {
for _, pair := range a.bucket {
for _, pair := range a.buckets {
if pair != nil {
fmt.Println(pair.key, "->", pair.val)
}

View File

@ -20,12 +20,12 @@ class Entry {
/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
private List<Entry> bucket;
private List<Entry> buckets;
public ArrayHashMap() {
// 初始化一个长度为 100 的桶数组
bucket = new ArrayList<>();
buckets = new ArrayList<>();
for (int i = 0; i < 100; i++) {
bucket.add(null);
buckets.add(null);
}
}
@ -38,7 +38,7 @@ class ArrayHashMap {
/* 查询操作 */
public String get(int key) {
int index = hashFunc(key);
Entry pair = bucket.get(index);
Entry pair = buckets.get(index);
if (pair == null) return null;
return pair.val;
}
@ -47,20 +47,20 @@ class ArrayHashMap {
public void put(int key, String val) {
Entry pair = new Entry(key, val);
int index = hashFunc(key);
bucket.set(index, pair);
buckets.set(index, pair);
}
/* 删除操作 */
public void remove(int key) {
int index = hashFunc(key);
// 置为 null 代表删除
bucket.set(index, null);
buckets.set(index, null);
}
/* 获取所有键值对 */
public List<Entry> entrySet() {
List<Entry> entrySet = new ArrayList<>();
for (Entry pair : bucket) {
for (Entry pair : buckets) {
if (pair != null)
entrySet.add(pair);
}
@ -70,7 +70,7 @@ class ArrayHashMap {
/* 获取所有键 */
public List<Integer> keySet() {
List<Integer> keySet = new ArrayList<>();
for (Entry pair : bucket) {
for (Entry pair : buckets) {
if (pair != null)
keySet.add(pair.key);
}
@ -80,7 +80,7 @@ class ArrayHashMap {
/* 获取所有值 */
public List<String> valueSet() {
List<String> valueSet = new ArrayList<>();
for (Entry pair : bucket) {
for (Entry pair : buckets) {
if (pair != null)
valueSet.add(pair.val);
}

View File

@ -14,10 +14,10 @@ class Entry {
/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
#bucket;
#buckets;
constructor() {
// 初始化一个长度为 100 的桶(数组)
this.#bucket = new Array(100).fill(null);
this.#buckets = new Array(100).fill(null);
}
/* 哈希函数 */
@ -28,7 +28,7 @@ class ArrayHashMap {
/* 查询操作 */
get(key) {
let index = this.#hashFunc(key);
let entry = this.#bucket[index];
let entry = this.#buckets[index];
if (entry === null) return null;
return entry.val;
}
@ -36,22 +36,22 @@ class ArrayHashMap {
/* 添加操作 */
set(key, val) {
let index = this.#hashFunc(key);
this.#bucket[index] = new Entry(key, val);
this.#buckets[index] = new Entry(key, val);
}
/* 删除操作 */
delete(key) {
let index = this.#hashFunc(key);
// 置为 null ,代表删除
this.#bucket[index] = null;
this.#buckets[index] = null;
}
/* 获取所有键值对 */
entries() {
let arr = [];
for (let i = 0; i < this.#bucket.length; i++) {
if (this.#bucket[i]) {
arr.push(this.#bucket[i]);
for (let i = 0; i < this.#buckets.length; i++) {
if (this.#buckets[i]) {
arr.push(this.#buckets[i]);
}
}
return arr;
@ -60,9 +60,9 @@ class ArrayHashMap {
/* 获取所有键 */
keys() {
let arr = [];
for (let i = 0; i < this.#bucket.length; i++) {
if (this.#bucket[i]) {
arr.push(this.#bucket[i]?.key);
for (let i = 0; i < this.#buckets.length; i++) {
if (this.#buckets[i]) {
arr.push(this.#buckets[i]?.key);
}
}
return arr;
@ -71,9 +71,9 @@ class ArrayHashMap {
/* 获取所有值 */
values() {
let arr = [];
for (let i = 0; i < this.#bucket.length; i++) {
if (this.#bucket[i]) {
arr.push(this.#bucket[i]?.val);
for (let i = 0; i < this.#buckets.length; i++) {
if (this.#buckets[i]) {
arr.push(this.#buckets[i]?.val);
}
}
return arr;

View File

@ -19,7 +19,7 @@ class ArrayHashMap:
def __init__(self):
""" 构造方法 """
# 初始化一个长度为 100 的桶(数组)
self.bucket: List[Optional[Entry]] = [None] * 100
self.buckets: List[Optional[Entry]] = [None] * 100
def hash_func(self, key: int) -> int:
""" 哈希函数 """
@ -29,7 +29,7 @@ class ArrayHashMap:
def get(self, key: int) -> str:
""" 查询操作 """
index: int = self.hash_func(key)
pair: Entry = self.bucket[index]
pair: Entry = self.buckets[index]
if pair is None:
return None
return pair.val
@ -38,18 +38,18 @@ class ArrayHashMap:
""" 添加操作 """
pair = Entry(key, val)
index: int = self.hash_func(key)
self.bucket[index] = pair
self.buckets[index] = pair
def remove(self, key: int) -> None:
""" 删除操作 """
index: int = self.hash_func(key)
# 置为 None ,代表删除
self.bucket[index] = None
self.buckets[index] = None
def entry_set(self) -> List[Entry]:
""" 获取所有键值对 """
result: List[Entry] = []
for pair in self.bucket:
for pair in self.buckets:
if pair is not None:
result.append(pair)
return result
@ -57,7 +57,7 @@ class ArrayHashMap:
def key_set(self) -> List[int]:
""" 获取所有键 """
result: List[int] = []
for pair in self.bucket:
for pair in self.buckets:
if pair is not None:
result.append(pair.key)
return result
@ -65,14 +65,14 @@ class ArrayHashMap:
def value_set(self) -> List[str]:
""" 获取所有值 """
result: List[str] = []
for pair in self.bucket:
for pair in self.buckets:
if pair is not None:
result.append(pair.val)
return result
def print(self) -> None:
""" 打印哈希表 """
for pair in self.bucket:
for pair in self.buckets:
if pair is not None:
print(pair.key, "->", pair.val)

View File

@ -12,12 +12,12 @@ pub struct Entry {
}
/* 基于数组简易实现的哈希表 */
pub struct ArrayHashMap { bucket: Vec<Option<Entry>> }
pub struct ArrayHashMap { buckets: Vec<Option<Entry>> }
impl ArrayHashMap {
pub fn new() -> ArrayHashMap {
// 初始化一个长度为 100 的桶(数组)
Self { bucket: vec![None; 100] }
Self { buckets: vec![None; 100] }
}
/* 哈希函数 */
@ -28,13 +28,13 @@ impl ArrayHashMap {
/* 查询操作 */
pub fn get(&self, key: i32) -> Option<&String> {
let index = self.hash_func(key);
self.bucket[index].as_ref().map(|entry| &entry.val)
self.buckets[index].as_ref().map(|entry| &entry.val)
}
/* 添加操作 */
pub fn put(&mut self, key: i32, val: &str) {
let index = self.hash_func(key);
self.bucket[index] = Some(Entry {
self.buckets[index] = Some(Entry {
key,
val: val.to_string(),
});
@ -43,22 +43,22 @@ impl ArrayHashMap {
/* 删除操作 */
pub fn remove(&mut self, key: i32) {
let index = self.hash_func(key);
self.bucket[index] = None;
self.buckets[index] = None;
}
/* 获取所有键值对 */
pub fn entry_set(&self) -> Vec<&Entry> {
self.bucket.iter().filter_map(|entry| entry.as_ref()).collect()
self.buckets.iter().filter_map(|entry| entry.as_ref()).collect()
}
/* 获取所有键 */
pub fn key_set(&self) -> Vec<&i32> {
self.bucket.iter().filter_map(|entry| entry.as_ref().map(|entry| &entry.key)).collect()
self.buckets.iter().filter_map(|entry| entry.as_ref().map(|entry| &entry.key)).collect()
}
/* 获取所有值 */
pub fn value_set(&self) -> Vec<&String> {
self.bucket.iter().filter_map(|entry| entry.as_ref().map(|entry| &entry.val)).collect()
self.buckets.iter().filter_map(|entry| entry.as_ref().map(|entry| &entry.val)).collect()
}
/* 打印哈希表 */

View File

@ -17,12 +17,12 @@ class Entry {
/* */
class ArrayHashMap {
private var bucket: [Entry?] = []
private var buckets: [Entry?] = []
init() {
// 100
for _ in 0 ..< 100 {
bucket.append(nil)
buckets.append(nil)
}
}
@ -35,7 +35,7 @@ class ArrayHashMap {
/* */
func get(key: Int) -> String? {
let index = hashFunc(key: key)
let pair = bucket[index]
let pair = buckets[index]
return pair?.val
}
@ -43,20 +43,20 @@ class ArrayHashMap {
func put(key: Int, val: String) {
let pair = Entry(key: key, val: val)
let index = hashFunc(key: key)
bucket[index] = pair
buckets[index] = pair
}
/* */
func remove(key: Int) {
let index = hashFunc(key: key)
// nil
bucket[index] = nil
buckets[index] = nil
}
/* */
func entrySet() -> [Entry] {
var entrySet: [Entry] = []
for pair in bucket {
for pair in buckets {
if let pair = pair {
entrySet.append(pair)
}
@ -67,7 +67,7 @@ class ArrayHashMap {
/* */
func keySet() -> [Int] {
var keySet: [Int] = []
for pair in bucket {
for pair in buckets {
if let pair = pair {
keySet.append(pair.key)
}
@ -78,7 +78,7 @@ class ArrayHashMap {
/* */
func valueSet() -> [String] {
var valueSet: [String] = []
for pair in bucket {
for pair in buckets {
if let pair = pair {
valueSet.append(pair.val)
}

View File

@ -18,11 +18,11 @@ class Entry {
/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
private readonly bucket: (Entry | null)[];
private readonly buckets: (Entry | null)[];
constructor() {
// 初始化一个长度为 100 的桶(数组)
this.bucket = (new Array(100)).fill(null);
this.buckets = (new Array(100)).fill(null);
}
/* 哈希函数 */
@ -33,7 +33,7 @@ class ArrayHashMap {
/* 查询操作 */
public get(key: number): string | null {
let index = this.hashFunc(key);
let entry = this.bucket[index];
let entry = this.buckets[index];
if (entry === null) return null;
return entry.val;
}
@ -41,22 +41,22 @@ class ArrayHashMap {
/* 添加操作 */
public set(key: number, val: string) {
let index = this.hashFunc(key);
this.bucket[index] = new Entry(key, val);
this.buckets[index] = new Entry(key, val);
}
/* 删除操作 */
public delete(key: number) {
let index = this.hashFunc(key);
// 置为 null ,代表删除
this.bucket[index] = null;
this.buckets[index] = null;
}
/* 获取所有键值对 */
public entries(): (Entry | null)[] {
let arr: (Entry | null)[] = [];
for (let i = 0; i < this.bucket.length; i++) {
if (this.bucket[i]) {
arr.push(this.bucket[i]);
for (let i = 0; i < this.buckets.length; i++) {
if (this.buckets[i]) {
arr.push(this.buckets[i]);
}
}
return arr;
@ -65,9 +65,9 @@ class ArrayHashMap {
/* 获取所有键 */
public keys(): (number | undefined)[] {
let arr: (number | undefined)[] = [];
for (let i = 0; i < this.bucket.length; i++) {
if (this.bucket[i]) {
arr.push(this.bucket[i]?.key);
for (let i = 0; i < this.buckets.length; i++) {
if (this.buckets[i]) {
arr.push(this.buckets[i]?.key);
}
}
return arr;
@ -76,9 +76,9 @@ class ArrayHashMap {
/* 获取所有值 */
public values(): (string | undefined)[] {
let arr: (string | undefined)[] = [];
for (let i = 0; i < this.bucket.length; i++) {
if (this.bucket[i]) {
arr.push(this.bucket[i]?.val);
for (let i = 0; i < this.buckets.length; i++) {
if (this.buckets[i]) {
arr.push(this.buckets[i]?.val);
}
}
return arr;

View File

@ -21,7 +21,7 @@ const Entry = struct {
//
pub fn ArrayHashMap(comptime T: type) type {
return struct {
bucket: ?std.ArrayList(?T) = null,
buckets: ?std.ArrayList(?T) = null,
mem_allocator: std.mem.Allocator = undefined,
const Self = @This();
@ -30,16 +30,16 @@ pub fn ArrayHashMap(comptime T: type) type {
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
self.mem_allocator = allocator;
// 100
self.bucket = std.ArrayList(?T).init(self.mem_allocator);
self.buckets = std.ArrayList(?T).init(self.mem_allocator);
var i: i32 = 0;
while (i < 100) : (i += 1) {
try self.bucket.?.append(null);
try self.buckets.?.append(null);
}
}
//
pub fn deinit(self: *Self) void {
if (self.bucket != null) self.bucket.?.deinit();
if (self.buckets != null) self.buckets.?.deinit();
}
//
@ -51,7 +51,7 @@ pub fn ArrayHashMap(comptime T: type) type {
//
pub fn get(self: *Self, key: usize) []const u8 {
var index = hashFunc(key);
var pair = self.bucket.?.items[index];
var pair = self.buckets.?.items[index];
return pair.?.val;
}
@ -59,20 +59,20 @@ pub fn ArrayHashMap(comptime T: type) type {
pub fn put(self: *Self, key: usize, val: []const u8) !void {
var pair = Entry.init(key, val);
var index = hashFunc(key);
self.bucket.?.items[index] = pair;
self.buckets.?.items[index] = pair;
}
//
pub fn remove(self: *Self, key: usize) !void {
var index = hashFunc(key);
// null
self.bucket.?.items[index] = null;
self.buckets.?.items[index] = null;
}
//
pub fn entrySet(self: *Self) !*std.ArrayList(T) {
var entry_set = std.ArrayList(T).init(self.mem_allocator);
for (self.bucket.?.items) |item| {
for (self.buckets.?.items) |item| {
if (item == null) continue;
try entry_set.append(item.?);
}
@ -82,7 +82,7 @@ pub fn ArrayHashMap(comptime T: type) type {
//
pub fn keySet(self: *Self) !*std.ArrayList(usize) {
var key_set = std.ArrayList(usize).init(self.mem_allocator);
for (self.bucket.?.items) |item| {
for (self.buckets.?.items) |item| {
if (item == null) continue;
try key_set.append(item.?.key);
}
@ -92,7 +92,7 @@ pub fn ArrayHashMap(comptime T: type) type {
//
pub fn valueSet(self: *Self) !*std.ArrayList([]const u8) {
var value_set = std.ArrayList([]const u8).init(self.mem_allocator);
for (self.bucket.?.items) |item| {
for (self.buckets.?.items) |item| {
if (item == null) continue;
try value_set.append(item.?.val);
}