Skip to content

Commit

Permalink
feat: 顺序搜索
Browse files Browse the repository at this point in the history
搜索算法-顺序搜索
- 遗留问题,ts枚举
- 枚举类型和成员类型关联
- 枚举类型使用
- 外部枚举声明
  • Loading branch information
wangshan authored and PachVerb committed Jul 15, 2021
1 parent e34792d commit 1a9e85e
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
* @Author: wangshan
* @Date: 2021-06-22 00:27:02
* @LastEditors: wangshan
* @LastEditTime: 2021-06-24 22:42:35
* @LastEditTime: 2021-07-15 23:18:06
* @Description: 入口文件
*/
import "@/utils/index"; // 导入自定义模块
import "@/sort/index";
// import "@/utils/index"; // 导入自定义模块
// import "@/sort/index";
import "./search/index";
8 changes: 8 additions & 0 deletions src/search/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* @Author: wangshan
* @Date: 2021-07-15 22:29:59
* @LastEditors: wangshan
* @LastEditTime: 2021-07-16 00:31:33
* @Description: 入口
*/
import "./liner_day1";
166 changes: 166 additions & 0 deletions src/search/liner_day1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* @Author: wangshan
* @Date: 2021-07-15 22:30:48
* @LastEditors: wangshan
* @LastEditTime: 2021-07-16 00:31:10
* @Description: 搜索算法-线性搜索
*/
/*
顺序搜索,含义是将列表中的元素和我们需要查找的元素做比较。从其特点来看,算法性能是比较低的。
*/

import { DOES_NOT_EXIST, defaultEquals } from "../utils/compareFn";

// 算法实现
function sequentialSearch(
arr: number[],
value: number,
equalsFn = defaultEquals
) {
let { length } = arr;
for (let i: number = 0; i < length; i++) {
if (equalsFn<number>(value, arr[i])) {
return i;
}
}
return DOES_NOT_EXIST;
}

// 测试
let arr = [5, 4, 3, 2, 1];
let res = sequentialSearch(arr, 3);
console.log(res, arr[res]); // i --> 2, arr[i] ---> 3

// 枚举使用

// 数值

// 在编译是进行初始化,作为运行时真是存在的对象
// enum Direction {
// Up = 1, // 第一位置带初始化成员, 其余位置均根据首个位置的成员值,累加初始化
// Down,
// Left,
// Right,
// }

// console.log(
// "访问枚举成员: ",
// "Up:" + Direction.Up,
// "Down:" + Direction.Down,
// "Left:" + Direction.Left,
// "Right:" + Direction.Right
// ); // 1, 2, 3, 4

// 成员不带初始化
// 默认从零开始初始化
enum Direction {
Up,
Down,
Left,
Right,
}
console.log(
"访问枚举成员: ",
"Up:" + Direction.Up,
"Down:" + Direction.Down,
"Left:" + Direction.Left,
"Right:" + Direction.Right
); // 0, 1, 2, 3
//不管是计算求值还是字面初始化成员,都是在编译阶段处理的。对于一些非操作运算符,或字面量的初始化。只会导致赋值失败。
// 例如,通过函数调用初始化成员值。

// 联合枚举和枚举成员类型

// 枚举成员类型
// enum ShapeKind {
// Circle,
// Square,
// }

// interface Circle {
// kind: ShapeKind.Circle;
// radius: number;
// }

// 枚举成员作为类型。换句话说,某些成员只能是枚举成员的值。相对枚举来说的
// let c: Circle = {
// kind: ShapeKind.Square,
// radius: 100,
// };

// 枚举类型成员枚举成员的联合类型
// enum E {
// Foo,
// Bar,
// }

// function f(x: E) {
// if (x !== E.Foo || x !== E.Bar) {
// // ~~~~~~~~~~~
// // Error! Operator '!==' cannot be applied to types 'E.Foo' and 'E.Bar'.
// }
// }

// 运行时对象
enum E {
X,
Y,
Z,
}
function f(obj: { X: number }) {
return obj.X;
}

// Works, since 'E' has a property named 'X' which is a number.
f(E);

//数字枚举--- 反向映射
// enum Enum {
// X,
// }
// let a = Enum.X;
// let nameOfA = Enum[a];

// console.log(Enum); // {0: X, X: 0}

// 反向映射,编译实现
// var Enum: any;
// (function (Enum) {
// Enum[(Enum["A"] = 0)] = "A";
// })(Enum || (Enum = {}));
// var a = Enum.A;
// var nameOfA = Enum[a]; //
// console.log(Enum, nameOfA); // { 0: "A", A: 0}

// 常量枚举 const
// const enum Enum {
// A = 1,
// B = A * 2,
// }

const enum Directions {
Up,
Down,
Left,
Right,
}

let directions = [
Directions.Up,
Directions.Down,
Directions.Left,
Directions.Right,
];

console.log(directions, Direction);

// 外部枚举
//外部枚举用来描述已经存在的枚举类型的形状。
// 外部枚举和非外部枚举之间有一个重要的区别,在正常的枚举里,没有初始化方法的成员被当成常数成员。 对于非常数的外部枚举而言,没有初始化方法时被当做需要经过计算的。
//

declare enum Enum {
A = 1,
B,
C = 2,
}
8 changes: 7 additions & 1 deletion src/utils/compareFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: wangshan
* @Date: 2021-06-27 23:57:07
* @LastEditors: wangshan
* @LastEditTime: 2021-07-08 00:16:15
* @LastEditTime: 2021-07-15 22:57:35
* @Description: 比较函数
*/

Expand All @@ -12,6 +12,8 @@ export enum Compare {
EQUALS = 0,
}

export const DOES_NOT_EXIST = -1;

export function defaultCompare<T>(a: T, b: T): number {
if (a === b) {
return Compare.EQUALS;
Expand All @@ -21,3 +23,7 @@ export function defaultCompare<T>(a: T, b: T): number {
export default function compareFn(a: number, b: number): boolean {
return a > b ? true : false;
}

export function defaultEquals<T>(a: T, b: T): boolean {
return a === b;
}

0 comments on commit 1a9e85e

Please sign in to comment.