Update array_hash_map
This commit is contained in:
parent
669388128f
commit
d5afd93571
@ -20,11 +20,11 @@ public:
|
|||||||
/* 基于数组简易实现的哈希表 */
|
/* 基于数组简易实现的哈希表 */
|
||||||
class ArrayHashMap {
|
class ArrayHashMap {
|
||||||
private:
|
private:
|
||||||
vector<Entry*> bucket;
|
vector<Entry*> buckets;
|
||||||
public:
|
public:
|
||||||
ArrayHashMap() {
|
ArrayHashMap() {
|
||||||
// 初始化一个长度为 100 的桶(数组)
|
// 初始化一个长度为 100 的桶(数组)
|
||||||
bucket= vector<Entry*>(100);
|
buckets= vector<Entry*>(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 哈希函数 */
|
/* 哈希函数 */
|
||||||
@ -36,7 +36,7 @@ public:
|
|||||||
/* 查询操作 */
|
/* 查询操作 */
|
||||||
string get(int key) {
|
string get(int key) {
|
||||||
int index = hashFunc(key);
|
int index = hashFunc(key);
|
||||||
Entry* pair = bucket[index];
|
Entry* pair = buckets[index];
|
||||||
if (pair == nullptr)
|
if (pair == nullptr)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
return pair->val;
|
return pair->val;
|
||||||
@ -46,20 +46,20 @@ public:
|
|||||||
void put(int key, string val) {
|
void put(int key, string val) {
|
||||||
Entry* pair = new Entry(key, val);
|
Entry* pair = new Entry(key, val);
|
||||||
int index = hashFunc(key);
|
int index = hashFunc(key);
|
||||||
bucket[index] = pair;
|
buckets[index] = pair;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 删除操作 */
|
/* 删除操作 */
|
||||||
void remove(int key) {
|
void remove(int key) {
|
||||||
int index = hashFunc(key);
|
int index = hashFunc(key);
|
||||||
// 置为 nullptr ,代表删除
|
// 置为 nullptr ,代表删除
|
||||||
bucket[index] = nullptr;
|
buckets[index] = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键值对 */
|
/* 获取所有键值对 */
|
||||||
vector<Entry*> entrySet() {
|
vector<Entry*> entrySet() {
|
||||||
vector<Entry*> entrySet;
|
vector<Entry*> entrySet;
|
||||||
for (Entry* pair: bucket) {
|
for (Entry* pair: buckets) {
|
||||||
if (pair != nullptr) {
|
if (pair != nullptr) {
|
||||||
entrySet.push_back(pair);
|
entrySet.push_back(pair);
|
||||||
}
|
}
|
||||||
@ -70,7 +70,7 @@ public:
|
|||||||
/* 获取所有键 */
|
/* 获取所有键 */
|
||||||
vector<int> keySet() {
|
vector<int> keySet() {
|
||||||
vector<int> keySet;
|
vector<int> keySet;
|
||||||
for (Entry* pair: bucket) {
|
for (Entry* pair: buckets) {
|
||||||
if (pair != nullptr) {
|
if (pair != nullptr) {
|
||||||
keySet.push_back(pair->key);
|
keySet.push_back(pair->key);
|
||||||
}
|
}
|
||||||
@ -81,7 +81,7 @@ public:
|
|||||||
/* 获取所有值 */
|
/* 获取所有值 */
|
||||||
vector<string> valueSet() {
|
vector<string> valueSet() {
|
||||||
vector<string> valueSet;
|
vector<string> valueSet;
|
||||||
for (Entry* pair: bucket) {
|
for (Entry* pair: buckets) {
|
||||||
if (pair != nullptr){
|
if (pair != nullptr){
|
||||||
valueSet.push_back(pair->val);
|
valueSet.push_back(pair->val);
|
||||||
}
|
}
|
||||||
|
@ -23,14 +23,14 @@ class Entry
|
|||||||
/* 基于数组简易实现的哈希表 */
|
/* 基于数组简易实现的哈希表 */
|
||||||
class ArrayHashMap
|
class ArrayHashMap
|
||||||
{
|
{
|
||||||
private List<Entry?> bucket;
|
private List<Entry?> buckets;
|
||||||
public ArrayHashMap()
|
public ArrayHashMap()
|
||||||
{
|
{
|
||||||
// 初始化一个长度为 100 的桶(数组)
|
// 初始化一个长度为 100 的桶(数组)
|
||||||
bucket = new();
|
buckets = new();
|
||||||
for (int i = 0; i < 100; i++)
|
for (int i = 0; i < 100; i++)
|
||||||
{
|
{
|
||||||
bucket.Add(null);
|
buckets.Add(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,7 +45,7 @@ class ArrayHashMap
|
|||||||
public String? get(int key)
|
public String? get(int key)
|
||||||
{
|
{
|
||||||
int index = hashFunc(key);
|
int index = hashFunc(key);
|
||||||
Entry? pair = bucket[index];
|
Entry? pair = buckets[index];
|
||||||
if (pair == null) return null;
|
if (pair == null) return null;
|
||||||
return pair.val;
|
return pair.val;
|
||||||
}
|
}
|
||||||
@ -55,7 +55,7 @@ class ArrayHashMap
|
|||||||
{
|
{
|
||||||
Entry pair = new Entry(key, val);
|
Entry pair = new Entry(key, val);
|
||||||
int index = hashFunc(key);
|
int index = hashFunc(key);
|
||||||
bucket[index] = pair;
|
buckets[index] = pair;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 删除操作 */
|
/* 删除操作 */
|
||||||
@ -63,14 +63,14 @@ class ArrayHashMap
|
|||||||
{
|
{
|
||||||
int index = hashFunc(key);
|
int index = hashFunc(key);
|
||||||
// 置为 null ,代表删除
|
// 置为 null ,代表删除
|
||||||
bucket[index] = null;
|
buckets[index] = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键值对 */
|
/* 获取所有键值对 */
|
||||||
public List<Entry> entrySet()
|
public List<Entry> entrySet()
|
||||||
{
|
{
|
||||||
List<Entry> entrySet = new();
|
List<Entry> entrySet = new();
|
||||||
foreach (Entry? pair in bucket)
|
foreach (Entry? pair in buckets)
|
||||||
{
|
{
|
||||||
if (pair != null)
|
if (pair != null)
|
||||||
entrySet.Add(pair);
|
entrySet.Add(pair);
|
||||||
@ -82,7 +82,7 @@ class ArrayHashMap
|
|||||||
public List<int> keySet()
|
public List<int> keySet()
|
||||||
{
|
{
|
||||||
List<int> keySet = new();
|
List<int> keySet = new();
|
||||||
foreach (Entry? pair in bucket)
|
foreach (Entry? pair in buckets)
|
||||||
{
|
{
|
||||||
if (pair != null)
|
if (pair != null)
|
||||||
keySet.Add(pair.key);
|
keySet.Add(pair.key);
|
||||||
@ -94,7 +94,7 @@ class ArrayHashMap
|
|||||||
public List<String> valueSet()
|
public List<String> valueSet()
|
||||||
{
|
{
|
||||||
List<String> valueSet = new();
|
List<String> valueSet = new();
|
||||||
foreach (Entry? pair in bucket)
|
foreach (Entry? pair in buckets)
|
||||||
{
|
{
|
||||||
if (pair != null)
|
if (pair != null)
|
||||||
valueSet.Add(pair.val);
|
valueSet.Add(pair.val);
|
||||||
|
@ -14,14 +14,14 @@ type entry struct {
|
|||||||
|
|
||||||
/* 基于数组简易实现的哈希表 */
|
/* 基于数组简易实现的哈希表 */
|
||||||
type arrayHashMap struct {
|
type arrayHashMap struct {
|
||||||
bucket []*entry
|
buckets []*entry
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 初始化哈希表 */
|
/* 初始化哈希表 */
|
||||||
func newArrayHashMap() *arrayHashMap {
|
func newArrayHashMap() *arrayHashMap {
|
||||||
// 初始化一个长度为 100 的桶(数组)
|
// 初始化一个长度为 100 的桶(数组)
|
||||||
bucket := make([]*entry, 100)
|
buckets := make([]*entry, 100)
|
||||||
return &arrayHashMap{bucket: bucket}
|
return &arrayHashMap{buckets: buckets}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 哈希函数 */
|
/* 哈希函数 */
|
||||||
@ -33,7 +33,7 @@ func (a *arrayHashMap) hashFunc(key int) int {
|
|||||||
/* 查询操作 */
|
/* 查询操作 */
|
||||||
func (a *arrayHashMap) get(key int) string {
|
func (a *arrayHashMap) get(key int) string {
|
||||||
index := a.hashFunc(key)
|
index := a.hashFunc(key)
|
||||||
pair := a.bucket[index]
|
pair := a.buckets[index]
|
||||||
if pair == nil {
|
if pair == nil {
|
||||||
return "Not Found"
|
return "Not Found"
|
||||||
}
|
}
|
||||||
@ -44,20 +44,20 @@ func (a *arrayHashMap) get(key int) string {
|
|||||||
func (a *arrayHashMap) put(key int, val string) {
|
func (a *arrayHashMap) put(key int, val string) {
|
||||||
pair := &entry{key: key, val: val}
|
pair := &entry{key: key, val: val}
|
||||||
index := a.hashFunc(key)
|
index := a.hashFunc(key)
|
||||||
a.bucket[index] = pair
|
a.buckets[index] = pair
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 删除操作 */
|
/* 删除操作 */
|
||||||
func (a *arrayHashMap) remove(key int) {
|
func (a *arrayHashMap) remove(key int) {
|
||||||
index := a.hashFunc(key)
|
index := a.hashFunc(key)
|
||||||
// 置为 nil ,代表删除
|
// 置为 nil ,代表删除
|
||||||
a.bucket[index] = nil
|
a.buckets[index] = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键对 */
|
/* 获取所有键对 */
|
||||||
func (a *arrayHashMap) entrySet() []*entry {
|
func (a *arrayHashMap) entrySet() []*entry {
|
||||||
var pairs []*entry
|
var pairs []*entry
|
||||||
for _, pair := range a.bucket {
|
for _, pair := range a.buckets {
|
||||||
if pair != nil {
|
if pair != nil {
|
||||||
pairs = append(pairs, pair)
|
pairs = append(pairs, pair)
|
||||||
}
|
}
|
||||||
@ -68,7 +68,7 @@ func (a *arrayHashMap) entrySet() []*entry {
|
|||||||
/* 获取所有键 */
|
/* 获取所有键 */
|
||||||
func (a *arrayHashMap) keySet() []int {
|
func (a *arrayHashMap) keySet() []int {
|
||||||
var keys []int
|
var keys []int
|
||||||
for _, pair := range a.bucket {
|
for _, pair := range a.buckets {
|
||||||
if pair != nil {
|
if pair != nil {
|
||||||
keys = append(keys, pair.key)
|
keys = append(keys, pair.key)
|
||||||
}
|
}
|
||||||
@ -79,7 +79,7 @@ func (a *arrayHashMap) keySet() []int {
|
|||||||
/* 获取所有值 */
|
/* 获取所有值 */
|
||||||
func (a *arrayHashMap) valueSet() []string {
|
func (a *arrayHashMap) valueSet() []string {
|
||||||
var values []string
|
var values []string
|
||||||
for _, pair := range a.bucket {
|
for _, pair := range a.buckets {
|
||||||
if pair != nil {
|
if pair != nil {
|
||||||
values = append(values, pair.val)
|
values = append(values, pair.val)
|
||||||
}
|
}
|
||||||
@ -89,7 +89,7 @@ func (a *arrayHashMap) valueSet() []string {
|
|||||||
|
|
||||||
/* 打印哈希表 */
|
/* 打印哈希表 */
|
||||||
func (a *arrayHashMap) print() {
|
func (a *arrayHashMap) print() {
|
||||||
for _, pair := range a.bucket {
|
for _, pair := range a.buckets {
|
||||||
if pair != nil {
|
if pair != nil {
|
||||||
fmt.Println(pair.key, "->", pair.val)
|
fmt.Println(pair.key, "->", pair.val)
|
||||||
}
|
}
|
||||||
|
@ -20,12 +20,12 @@ class Entry {
|
|||||||
|
|
||||||
/* 基于数组简易实现的哈希表 */
|
/* 基于数组简易实现的哈希表 */
|
||||||
class ArrayHashMap {
|
class ArrayHashMap {
|
||||||
private List<Entry> bucket;
|
private List<Entry> buckets;
|
||||||
public ArrayHashMap() {
|
public ArrayHashMap() {
|
||||||
// 初始化一个长度为 100 的桶(数组)
|
// 初始化一个长度为 100 的桶(数组)
|
||||||
bucket = new ArrayList<>();
|
buckets = new ArrayList<>();
|
||||||
for (int i = 0; i < 100; i++) {
|
for (int i = 0; i < 100; i++) {
|
||||||
bucket.add(null);
|
buckets.add(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,7 +38,7 @@ class ArrayHashMap {
|
|||||||
/* 查询操作 */
|
/* 查询操作 */
|
||||||
public String get(int key) {
|
public String get(int key) {
|
||||||
int index = hashFunc(key);
|
int index = hashFunc(key);
|
||||||
Entry pair = bucket.get(index);
|
Entry pair = buckets.get(index);
|
||||||
if (pair == null) return null;
|
if (pair == null) return null;
|
||||||
return pair.val;
|
return pair.val;
|
||||||
}
|
}
|
||||||
@ -47,20 +47,20 @@ class ArrayHashMap {
|
|||||||
public void put(int key, String val) {
|
public void put(int key, String val) {
|
||||||
Entry pair = new Entry(key, val);
|
Entry pair = new Entry(key, val);
|
||||||
int index = hashFunc(key);
|
int index = hashFunc(key);
|
||||||
bucket.set(index, pair);
|
buckets.set(index, pair);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 删除操作 */
|
/* 删除操作 */
|
||||||
public void remove(int key) {
|
public void remove(int key) {
|
||||||
int index = hashFunc(key);
|
int index = hashFunc(key);
|
||||||
// 置为 null ,代表删除
|
// 置为 null ,代表删除
|
||||||
bucket.set(index, null);
|
buckets.set(index, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键值对 */
|
/* 获取所有键值对 */
|
||||||
public List<Entry> entrySet() {
|
public List<Entry> entrySet() {
|
||||||
List<Entry> entrySet = new ArrayList<>();
|
List<Entry> entrySet = new ArrayList<>();
|
||||||
for (Entry pair : bucket) {
|
for (Entry pair : buckets) {
|
||||||
if (pair != null)
|
if (pair != null)
|
||||||
entrySet.add(pair);
|
entrySet.add(pair);
|
||||||
}
|
}
|
||||||
@ -70,7 +70,7 @@ class ArrayHashMap {
|
|||||||
/* 获取所有键 */
|
/* 获取所有键 */
|
||||||
public List<Integer> keySet() {
|
public List<Integer> keySet() {
|
||||||
List<Integer> keySet = new ArrayList<>();
|
List<Integer> keySet = new ArrayList<>();
|
||||||
for (Entry pair : bucket) {
|
for (Entry pair : buckets) {
|
||||||
if (pair != null)
|
if (pair != null)
|
||||||
keySet.add(pair.key);
|
keySet.add(pair.key);
|
||||||
}
|
}
|
||||||
@ -80,7 +80,7 @@ class ArrayHashMap {
|
|||||||
/* 获取所有值 */
|
/* 获取所有值 */
|
||||||
public List<String> valueSet() {
|
public List<String> valueSet() {
|
||||||
List<String> valueSet = new ArrayList<>();
|
List<String> valueSet = new ArrayList<>();
|
||||||
for (Entry pair : bucket) {
|
for (Entry pair : buckets) {
|
||||||
if (pair != null)
|
if (pair != null)
|
||||||
valueSet.add(pair.val);
|
valueSet.add(pair.val);
|
||||||
}
|
}
|
||||||
|
@ -14,10 +14,10 @@ class Entry {
|
|||||||
|
|
||||||
/* 基于数组简易实现的哈希表 */
|
/* 基于数组简易实现的哈希表 */
|
||||||
class ArrayHashMap {
|
class ArrayHashMap {
|
||||||
#bucket;
|
#buckets;
|
||||||
constructor() {
|
constructor() {
|
||||||
// 初始化一个长度为 100 的桶(数组)
|
// 初始化一个长度为 100 的桶(数组)
|
||||||
this.#bucket = new Array(100).fill(null);
|
this.#buckets = new Array(100).fill(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 哈希函数 */
|
/* 哈希函数 */
|
||||||
@ -28,7 +28,7 @@ class ArrayHashMap {
|
|||||||
/* 查询操作 */
|
/* 查询操作 */
|
||||||
get(key) {
|
get(key) {
|
||||||
let index = this.#hashFunc(key);
|
let index = this.#hashFunc(key);
|
||||||
let entry = this.#bucket[index];
|
let entry = this.#buckets[index];
|
||||||
if (entry === null) return null;
|
if (entry === null) return null;
|
||||||
return entry.val;
|
return entry.val;
|
||||||
}
|
}
|
||||||
@ -36,22 +36,22 @@ class ArrayHashMap {
|
|||||||
/* 添加操作 */
|
/* 添加操作 */
|
||||||
set(key, val) {
|
set(key, val) {
|
||||||
let index = this.#hashFunc(key);
|
let index = this.#hashFunc(key);
|
||||||
this.#bucket[index] = new Entry(key, val);
|
this.#buckets[index] = new Entry(key, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 删除操作 */
|
/* 删除操作 */
|
||||||
delete(key) {
|
delete(key) {
|
||||||
let index = this.#hashFunc(key);
|
let index = this.#hashFunc(key);
|
||||||
// 置为 null ,代表删除
|
// 置为 null ,代表删除
|
||||||
this.#bucket[index] = null;
|
this.#buckets[index] = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键值对 */
|
/* 获取所有键值对 */
|
||||||
entries() {
|
entries() {
|
||||||
let arr = [];
|
let arr = [];
|
||||||
for (let i = 0; i < this.#bucket.length; i++) {
|
for (let i = 0; i < this.#buckets.length; i++) {
|
||||||
if (this.#bucket[i]) {
|
if (this.#buckets[i]) {
|
||||||
arr.push(this.#bucket[i]);
|
arr.push(this.#buckets[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return arr;
|
return arr;
|
||||||
@ -60,9 +60,9 @@ class ArrayHashMap {
|
|||||||
/* 获取所有键 */
|
/* 获取所有键 */
|
||||||
keys() {
|
keys() {
|
||||||
let arr = [];
|
let arr = [];
|
||||||
for (let i = 0; i < this.#bucket.length; i++) {
|
for (let i = 0; i < this.#buckets.length; i++) {
|
||||||
if (this.#bucket[i]) {
|
if (this.#buckets[i]) {
|
||||||
arr.push(this.#bucket[i]?.key);
|
arr.push(this.#buckets[i]?.key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return arr;
|
return arr;
|
||||||
@ -71,9 +71,9 @@ class ArrayHashMap {
|
|||||||
/* 获取所有值 */
|
/* 获取所有值 */
|
||||||
values() {
|
values() {
|
||||||
let arr = [];
|
let arr = [];
|
||||||
for (let i = 0; i < this.#bucket.length; i++) {
|
for (let i = 0; i < this.#buckets.length; i++) {
|
||||||
if (this.#bucket[i]) {
|
if (this.#buckets[i]) {
|
||||||
arr.push(this.#bucket[i]?.val);
|
arr.push(this.#buckets[i]?.val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return arr;
|
return arr;
|
||||||
|
@ -19,7 +19,7 @@ class ArrayHashMap:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
""" 构造方法 """
|
""" 构造方法 """
|
||||||
# 初始化一个长度为 100 的桶(数组)
|
# 初始化一个长度为 100 的桶(数组)
|
||||||
self.bucket: List[Optional[Entry]] = [None] * 100
|
self.buckets: List[Optional[Entry]] = [None] * 100
|
||||||
|
|
||||||
def hash_func(self, key: int) -> int:
|
def hash_func(self, key: int) -> int:
|
||||||
""" 哈希函数 """
|
""" 哈希函数 """
|
||||||
@ -29,7 +29,7 @@ class ArrayHashMap:
|
|||||||
def get(self, key: int) -> str:
|
def get(self, key: int) -> str:
|
||||||
""" 查询操作 """
|
""" 查询操作 """
|
||||||
index: int = self.hash_func(key)
|
index: int = self.hash_func(key)
|
||||||
pair: Entry = self.bucket[index]
|
pair: Entry = self.buckets[index]
|
||||||
if pair is None:
|
if pair is None:
|
||||||
return None
|
return None
|
||||||
return pair.val
|
return pair.val
|
||||||
@ -38,18 +38,18 @@ class ArrayHashMap:
|
|||||||
""" 添加操作 """
|
""" 添加操作 """
|
||||||
pair = Entry(key, val)
|
pair = Entry(key, val)
|
||||||
index: int = self.hash_func(key)
|
index: int = self.hash_func(key)
|
||||||
self.bucket[index] = pair
|
self.buckets[index] = pair
|
||||||
|
|
||||||
def remove(self, key: int) -> None:
|
def remove(self, key: int) -> None:
|
||||||
""" 删除操作 """
|
""" 删除操作 """
|
||||||
index: int = self.hash_func(key)
|
index: int = self.hash_func(key)
|
||||||
# 置为 None ,代表删除
|
# 置为 None ,代表删除
|
||||||
self.bucket[index] = None
|
self.buckets[index] = None
|
||||||
|
|
||||||
def entry_set(self) -> List[Entry]:
|
def entry_set(self) -> List[Entry]:
|
||||||
""" 获取所有键值对 """
|
""" 获取所有键值对 """
|
||||||
result: List[Entry] = []
|
result: List[Entry] = []
|
||||||
for pair in self.bucket:
|
for pair in self.buckets:
|
||||||
if pair is not None:
|
if pair is not None:
|
||||||
result.append(pair)
|
result.append(pair)
|
||||||
return result
|
return result
|
||||||
@ -57,7 +57,7 @@ class ArrayHashMap:
|
|||||||
def key_set(self) -> List[int]:
|
def key_set(self) -> List[int]:
|
||||||
""" 获取所有键 """
|
""" 获取所有键 """
|
||||||
result: List[int] = []
|
result: List[int] = []
|
||||||
for pair in self.bucket:
|
for pair in self.buckets:
|
||||||
if pair is not None:
|
if pair is not None:
|
||||||
result.append(pair.key)
|
result.append(pair.key)
|
||||||
return result
|
return result
|
||||||
@ -65,14 +65,14 @@ class ArrayHashMap:
|
|||||||
def value_set(self) -> List[str]:
|
def value_set(self) -> List[str]:
|
||||||
""" 获取所有值 """
|
""" 获取所有值 """
|
||||||
result: List[str] = []
|
result: List[str] = []
|
||||||
for pair in self.bucket:
|
for pair in self.buckets:
|
||||||
if pair is not None:
|
if pair is not None:
|
||||||
result.append(pair.val)
|
result.append(pair.val)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def print(self) -> None:
|
def print(self) -> None:
|
||||||
""" 打印哈希表 """
|
""" 打印哈希表 """
|
||||||
for pair in self.bucket:
|
for pair in self.buckets:
|
||||||
if pair is not None:
|
if pair is not None:
|
||||||
print(pair.key, "->", pair.val)
|
print(pair.key, "->", pair.val)
|
||||||
|
|
||||||
|
@ -12,12 +12,12 @@ pub struct Entry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 基于数组简易实现的哈希表 */
|
/* 基于数组简易实现的哈希表 */
|
||||||
pub struct ArrayHashMap { bucket: Vec<Option<Entry>> }
|
pub struct ArrayHashMap { buckets: Vec<Option<Entry>> }
|
||||||
|
|
||||||
impl ArrayHashMap {
|
impl ArrayHashMap {
|
||||||
pub fn new() -> ArrayHashMap {
|
pub fn new() -> ArrayHashMap {
|
||||||
// 初始化一个长度为 100 的桶(数组)
|
// 初始化一个长度为 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> {
|
pub fn get(&self, key: i32) -> Option<&String> {
|
||||||
let index = self.hash_func(key);
|
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) {
|
pub fn put(&mut self, key: i32, val: &str) {
|
||||||
let index = self.hash_func(key);
|
let index = self.hash_func(key);
|
||||||
self.bucket[index] = Some(Entry {
|
self.buckets[index] = Some(Entry {
|
||||||
key,
|
key,
|
||||||
val: val.to_string(),
|
val: val.to_string(),
|
||||||
});
|
});
|
||||||
@ -43,22 +43,22 @@ impl ArrayHashMap {
|
|||||||
/* 删除操作 */
|
/* 删除操作 */
|
||||||
pub fn remove(&mut self, key: i32) {
|
pub fn remove(&mut self, key: i32) {
|
||||||
let index = self.hash_func(key);
|
let index = self.hash_func(key);
|
||||||
self.bucket[index] = None;
|
self.buckets[index] = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键值对 */
|
/* 获取所有键值对 */
|
||||||
pub fn entry_set(&self) -> Vec<&Entry> {
|
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> {
|
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> {
|
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()
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 打印哈希表 */
|
/* 打印哈希表 */
|
||||||
|
@ -17,12 +17,12 @@ class Entry {
|
|||||||
|
|
||||||
/* 基于数组简易实现的哈希表 */
|
/* 基于数组简易实现的哈希表 */
|
||||||
class ArrayHashMap {
|
class ArrayHashMap {
|
||||||
private var bucket: [Entry?] = []
|
private var buckets: [Entry?] = []
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
// 初始化一个长度为 100 的桶(数组)
|
// 初始化一个长度为 100 的桶(数组)
|
||||||
for _ in 0 ..< 100 {
|
for _ in 0 ..< 100 {
|
||||||
bucket.append(nil)
|
buckets.append(nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +35,7 @@ class ArrayHashMap {
|
|||||||
/* 查询操作 */
|
/* 查询操作 */
|
||||||
func get(key: Int) -> String? {
|
func get(key: Int) -> String? {
|
||||||
let index = hashFunc(key: key)
|
let index = hashFunc(key: key)
|
||||||
let pair = bucket[index]
|
let pair = buckets[index]
|
||||||
return pair?.val
|
return pair?.val
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,20 +43,20 @@ class ArrayHashMap {
|
|||||||
func put(key: Int, val: String) {
|
func put(key: Int, val: String) {
|
||||||
let pair = Entry(key: key, val: val)
|
let pair = Entry(key: key, val: val)
|
||||||
let index = hashFunc(key: key)
|
let index = hashFunc(key: key)
|
||||||
bucket[index] = pair
|
buckets[index] = pair
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 删除操作 */
|
/* 删除操作 */
|
||||||
func remove(key: Int) {
|
func remove(key: Int) {
|
||||||
let index = hashFunc(key: key)
|
let index = hashFunc(key: key)
|
||||||
// 置为 nil ,代表删除
|
// 置为 nil ,代表删除
|
||||||
bucket[index] = nil
|
buckets[index] = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键值对 */
|
/* 获取所有键值对 */
|
||||||
func entrySet() -> [Entry] {
|
func entrySet() -> [Entry] {
|
||||||
var entrySet: [Entry] = []
|
var entrySet: [Entry] = []
|
||||||
for pair in bucket {
|
for pair in buckets {
|
||||||
if let pair = pair {
|
if let pair = pair {
|
||||||
entrySet.append(pair)
|
entrySet.append(pair)
|
||||||
}
|
}
|
||||||
@ -67,7 +67,7 @@ class ArrayHashMap {
|
|||||||
/* 获取所有键 */
|
/* 获取所有键 */
|
||||||
func keySet() -> [Int] {
|
func keySet() -> [Int] {
|
||||||
var keySet: [Int] = []
|
var keySet: [Int] = []
|
||||||
for pair in bucket {
|
for pair in buckets {
|
||||||
if let pair = pair {
|
if let pair = pair {
|
||||||
keySet.append(pair.key)
|
keySet.append(pair.key)
|
||||||
}
|
}
|
||||||
@ -78,7 +78,7 @@ class ArrayHashMap {
|
|||||||
/* 获取所有值 */
|
/* 获取所有值 */
|
||||||
func valueSet() -> [String] {
|
func valueSet() -> [String] {
|
||||||
var valueSet: [String] = []
|
var valueSet: [String] = []
|
||||||
for pair in bucket {
|
for pair in buckets {
|
||||||
if let pair = pair {
|
if let pair = pair {
|
||||||
valueSet.append(pair.val)
|
valueSet.append(pair.val)
|
||||||
}
|
}
|
||||||
|
@ -18,11 +18,11 @@ class Entry {
|
|||||||
/* 基于数组简易实现的哈希表 */
|
/* 基于数组简易实现的哈希表 */
|
||||||
class ArrayHashMap {
|
class ArrayHashMap {
|
||||||
|
|
||||||
private readonly bucket: (Entry | null)[];
|
private readonly buckets: (Entry | null)[];
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
// 初始化一个长度为 100 的桶(数组)
|
// 初始化一个长度为 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 {
|
public get(key: number): string | null {
|
||||||
let index = this.hashFunc(key);
|
let index = this.hashFunc(key);
|
||||||
let entry = this.bucket[index];
|
let entry = this.buckets[index];
|
||||||
if (entry === null) return null;
|
if (entry === null) return null;
|
||||||
return entry.val;
|
return entry.val;
|
||||||
}
|
}
|
||||||
@ -41,22 +41,22 @@ class ArrayHashMap {
|
|||||||
/* 添加操作 */
|
/* 添加操作 */
|
||||||
public set(key: number, val: string) {
|
public set(key: number, val: string) {
|
||||||
let index = this.hashFunc(key);
|
let index = this.hashFunc(key);
|
||||||
this.bucket[index] = new Entry(key, val);
|
this.buckets[index] = new Entry(key, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 删除操作 */
|
/* 删除操作 */
|
||||||
public delete(key: number) {
|
public delete(key: number) {
|
||||||
let index = this.hashFunc(key);
|
let index = this.hashFunc(key);
|
||||||
// 置为 null ,代表删除
|
// 置为 null ,代表删除
|
||||||
this.bucket[index] = null;
|
this.buckets[index] = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键值对 */
|
/* 获取所有键值对 */
|
||||||
public entries(): (Entry | null)[] {
|
public entries(): (Entry | null)[] {
|
||||||
let arr: (Entry | null)[] = [];
|
let arr: (Entry | null)[] = [];
|
||||||
for (let i = 0; i < this.bucket.length; i++) {
|
for (let i = 0; i < this.buckets.length; i++) {
|
||||||
if (this.bucket[i]) {
|
if (this.buckets[i]) {
|
||||||
arr.push(this.bucket[i]);
|
arr.push(this.buckets[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return arr;
|
return arr;
|
||||||
@ -65,9 +65,9 @@ class ArrayHashMap {
|
|||||||
/* 获取所有键 */
|
/* 获取所有键 */
|
||||||
public keys(): (number | undefined)[] {
|
public keys(): (number | undefined)[] {
|
||||||
let arr: (number | undefined)[] = [];
|
let arr: (number | undefined)[] = [];
|
||||||
for (let i = 0; i < this.bucket.length; i++) {
|
for (let i = 0; i < this.buckets.length; i++) {
|
||||||
if (this.bucket[i]) {
|
if (this.buckets[i]) {
|
||||||
arr.push(this.bucket[i]?.key);
|
arr.push(this.buckets[i]?.key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return arr;
|
return arr;
|
||||||
@ -76,9 +76,9 @@ class ArrayHashMap {
|
|||||||
/* 获取所有值 */
|
/* 获取所有值 */
|
||||||
public values(): (string | undefined)[] {
|
public values(): (string | undefined)[] {
|
||||||
let arr: (string | undefined)[] = [];
|
let arr: (string | undefined)[] = [];
|
||||||
for (let i = 0; i < this.bucket.length; i++) {
|
for (let i = 0; i < this.buckets.length; i++) {
|
||||||
if (this.bucket[i]) {
|
if (this.buckets[i]) {
|
||||||
arr.push(this.bucket[i]?.val);
|
arr.push(this.buckets[i]?.val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return arr;
|
return arr;
|
||||||
|
@ -21,7 +21,7 @@ const Entry = struct {
|
|||||||
// 基于数组简易实现的哈希表
|
// 基于数组简易实现的哈希表
|
||||||
pub fn ArrayHashMap(comptime T: type) type {
|
pub fn ArrayHashMap(comptime T: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
bucket: ?std.ArrayList(?T) = null,
|
buckets: ?std.ArrayList(?T) = null,
|
||||||
mem_allocator: std.mem.Allocator = undefined,
|
mem_allocator: std.mem.Allocator = undefined,
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
@ -30,16 +30,16 @@ pub fn ArrayHashMap(comptime T: type) type {
|
|||||||
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
||||||
self.mem_allocator = allocator;
|
self.mem_allocator = allocator;
|
||||||
// 初始化一个长度为 100 的桶(数组)
|
// 初始化一个长度为 100 的桶(数组)
|
||||||
self.bucket = std.ArrayList(?T).init(self.mem_allocator);
|
self.buckets = std.ArrayList(?T).init(self.mem_allocator);
|
||||||
var i: i32 = 0;
|
var i: i32 = 0;
|
||||||
while (i < 100) : (i += 1) {
|
while (i < 100) : (i += 1) {
|
||||||
try self.bucket.?.append(null);
|
try self.buckets.?.append(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 析构方法
|
// 析构方法
|
||||||
pub fn deinit(self: *Self) void {
|
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 {
|
pub fn get(self: *Self, key: usize) []const u8 {
|
||||||
var index = hashFunc(key);
|
var index = hashFunc(key);
|
||||||
var pair = self.bucket.?.items[index];
|
var pair = self.buckets.?.items[index];
|
||||||
return pair.?.val;
|
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 {
|
pub fn put(self: *Self, key: usize, val: []const u8) !void {
|
||||||
var pair = Entry.init(key, val);
|
var pair = Entry.init(key, val);
|
||||||
var index = hashFunc(key);
|
var index = hashFunc(key);
|
||||||
self.bucket.?.items[index] = pair;
|
self.buckets.?.items[index] = pair;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除操作
|
// 删除操作
|
||||||
pub fn remove(self: *Self, key: usize) !void {
|
pub fn remove(self: *Self, key: usize) !void {
|
||||||
var index = hashFunc(key);
|
var index = hashFunc(key);
|
||||||
// 置为 null ,代表删除
|
// 置为 null ,代表删除
|
||||||
self.bucket.?.items[index] = null;
|
self.buckets.?.items[index] = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取所有键值对
|
// 获取所有键值对
|
||||||
pub fn entrySet(self: *Self) !*std.ArrayList(T) {
|
pub fn entrySet(self: *Self) !*std.ArrayList(T) {
|
||||||
var entry_set = std.ArrayList(T).init(self.mem_allocator);
|
var entry_set = std.ArrayList(T).init(self.mem_allocator);
|
||||||
for (self.bucket.?.items) |item| {
|
for (self.buckets.?.items) |item| {
|
||||||
if (item == null) continue;
|
if (item == null) continue;
|
||||||
try entry_set.append(item.?);
|
try entry_set.append(item.?);
|
||||||
}
|
}
|
||||||
@ -82,7 +82,7 @@ pub fn ArrayHashMap(comptime T: type) type {
|
|||||||
// 获取所有键
|
// 获取所有键
|
||||||
pub fn keySet(self: *Self) !*std.ArrayList(usize) {
|
pub fn keySet(self: *Self) !*std.ArrayList(usize) {
|
||||||
var key_set = std.ArrayList(usize).init(self.mem_allocator);
|
var key_set = std.ArrayList(usize).init(self.mem_allocator);
|
||||||
for (self.bucket.?.items) |item| {
|
for (self.buckets.?.items) |item| {
|
||||||
if (item == null) continue;
|
if (item == null) continue;
|
||||||
try key_set.append(item.?.key);
|
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) {
|
pub fn valueSet(self: *Self) !*std.ArrayList([]const u8) {
|
||||||
var value_set = std.ArrayList([]const u8).init(self.mem_allocator);
|
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;
|
if (item == null) continue;
|
||||||
try value_set.append(item.?.val);
|
try value_set.append(item.?.val);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user