feat(csharp) .NET 8.0 code migration (#966)

* .net 8.0 migration

* update docs

* revert change

* revert change and update appendix docs

* remove static

* Update binary_search_insertion.cs

* Update binary_search_insertion.cs

* Update binary_search_edge.cs

* Update binary_search_insertion.cs

* Update binary_search_edge.cs

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
hpstory
2023-11-26 23:18:44 +08:00
committed by GitHub
parent d960c99a1f
commit 56b20eff36
93 changed files with 539 additions and 487 deletions

View File

@ -7,28 +7,24 @@
namespace hello_algo.chapter_hashing;
/* 键值对 int->string */
class Pair {
public int key;
public string val;
public Pair(int key, string val) {
this.key = key;
this.val = val;
}
class Pair(int key, string val) {
public int key = key;
public string val = val;
}
/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
private readonly List<Pair?> buckets;
List<Pair?> buckets;
public ArrayHashMap() {
// 初始化数组,包含 100 个桶
buckets = new();
buckets = [];
for (int i = 0; i < 100; i++) {
buckets.Add(null);
}
}
/* 哈希函数 */
private int HashFunc(int key) {
int HashFunc(int key) {
int index = key % 100;
return index;
}
@ -57,7 +53,7 @@ class ArrayHashMap {
/* 获取所有键值对 */
public List<Pair> PairSet() {
List<Pair> pairSet = new();
List<Pair> pairSet = [];
foreach (Pair? pair in buckets) {
if (pair != null)
pairSet.Add(pair);
@ -67,7 +63,7 @@ class ArrayHashMap {
/* 获取所有键 */
public List<int> KeySet() {
List<int> keySet = new();
List<int> keySet = [];
foreach (Pair? pair in buckets) {
if (pair != null)
keySet.Add(pair.key);
@ -77,7 +73,7 @@ class ArrayHashMap {
/* 获取所有值 */
public List<string> ValueSet() {
List<string> valueSet = new();
List<string> valueSet = [];
foreach (Pair? pair in buckets) {
if (pair != null)
valueSet.Add(pair.val);

View File

@ -25,7 +25,7 @@ public class built_in_hash {
int hashStr = str.GetHashCode();
Console.WriteLine("字符串 " + str + " 的哈希值为 " + hashStr);
object[] arr = { 12836, "小哈" };
object[] arr = [12836, "小哈"];
int hashTup = arr.GetHashCode();
Console.WriteLine("数组 [" + string.Join(", ", arr) + "] 的哈希值为 " + hashTup);

View File

@ -10,8 +10,8 @@ namespace hello_algo.chapter_hashing;
class HashMapChaining {
int size; // 键值对数量
int capacity; // 哈希表容量
readonly double loadThres; // 触发扩容的负载因子阈值
readonly int extendRatio; // 扩容倍数
double loadThres; // 触发扩容的负载因子阈值
int extendRatio; // 扩容倍数
List<List<Pair>> buckets; // 桶数组
/* 构造方法 */
@ -22,17 +22,17 @@ class HashMapChaining {
extendRatio = 2;
buckets = new List<List<Pair>>(capacity);
for (int i = 0; i < capacity; i++) {
buckets.Add(new List<Pair>());
buckets.Add([]);
}
}
/* 哈希函数 */
private int HashFunc(int key) {
int HashFunc(int key) {
return key % capacity;
}
/* 负载因子 */
private double LoadFactor() {
double LoadFactor() {
return (double)size / capacity;
}
@ -82,14 +82,14 @@ class HashMapChaining {
}
/* 扩容哈希表 */
private void Extend() {
void Extend() {
// 暂存原哈希表
List<List<Pair>> bucketsTmp = buckets;
// 初始化扩容后的新哈希表
capacity *= extendRatio;
buckets = new List<List<Pair>>(capacity);
for (int i = 0; i < capacity; i++) {
buckets.Add(new List<Pair>());
buckets.Add([]);
}
size = 0;
// 将键值对从原哈希表搬运至新哈希表
@ -103,7 +103,7 @@ class HashMapChaining {
/* 打印哈希表 */
public void Print() {
foreach (List<Pair> bucket in buckets) {
List<string> res = new();
List<string> res = [];
foreach (Pair pair in bucket) {
res.Add(pair.key + " -> " + pair.val);
}

View File

@ -8,12 +8,12 @@ namespace hello_algo.chapter_hashing;
/* 开放寻址哈希表 */
class HashMapOpenAddressing {
private int size; // 键值对数量
private int capacity = 4; // 哈希表容量
private readonly double loadThres = 2.0 / 3.0; // 触发扩容的负载因子阈值
private readonly int extendRatio = 2; // 扩容倍数
private Pair[] buckets; // 桶数组
private readonly Pair TOMBSTONE = new(-1, "-1"); // 删除标记
int size; // 键值对数量
int capacity = 4; // 哈希表容量
double loadThres = 2.0 / 3.0; // 触发扩容的负载因子阈值
int extendRatio = 2; // 扩容倍数
Pair[] buckets; // 桶数组
Pair TOMBSTONE = new(-1, "-1"); // 删除标记
/* 构造方法 */
public HashMapOpenAddressing() {
@ -22,17 +22,17 @@ class HashMapOpenAddressing {
}
/* 哈希函数 */
private int HashFunc(int key) {
int HashFunc(int key) {
return key % capacity;
}
/* 负载因子 */
private double LoadFactor() {
double LoadFactor() {
return (double)size / capacity;
}
/* 搜索 key 对应的桶索引 */
private int FindBucket(int key) {
int FindBucket(int key) {
int index = HashFunc(key);
int firstTombstone = -1;
// 线性探测,当遇到空桶时跳出
@ -100,7 +100,7 @@ class HashMapOpenAddressing {
}
/* 扩容哈希表 */
private void Extend() {
void Extend() {
// 暂存原哈希表
Pair[] bucketsTmp = buckets;
// 初始化扩容后的新哈希表

View File

@ -8,7 +8,7 @@ namespace hello_algo.chapter_hashing;
public class simple_hash {
/* 加法哈希 */
public static int AddHash(string key) {
int AddHash(string key) {
long hash = 0;
const int MODULUS = 1000000007;
foreach (char c in key) {
@ -18,7 +18,7 @@ public class simple_hash {
}
/* 乘法哈希 */
public static int MulHash(string key) {
int MulHash(string key) {
long hash = 0;
const int MODULUS = 1000000007;
foreach (char c in key) {
@ -28,7 +28,7 @@ public class simple_hash {
}
/* 异或哈希 */
public static int XorHash(string key) {
int XorHash(string key) {
int hash = 0;
const int MODULUS = 1000000007;
foreach (char c in key) {
@ -38,7 +38,7 @@ public class simple_hash {
}
/* 旋转哈希 */
public static int RotHash(string key) {
int RotHash(string key) {
long hash = 0;
const int MODULUS = 1000000007;
foreach (char c in key) {