### 算法

``` 1const getContiguousIds = ({
2  contiguousIds = [],
3  node,
4  nodes,
5}) => (
6  node
8  .reduce(
9    (
10      contiguousIds,
12    ) => (
13      contiguousIds
15      ? contiguousIds
16      : (
17        getContiguousIds({
18          contiguousIds,
19          node: (
20            nodes
21            .find(({
22              id,
23            }) => (
24              id
26            ))
27          ),
28          nodes,
29        })
30      )
31    ),
32    (
33      contiguousIds
34      .concat(
35        node
36        .id
37      )
38    ),
39  )
40)```
``` 1const getLargestContiguousNodes = (
2  nodes,
3) => (
4  nodes
5  .reduce(
6    (
7      prevState,
8      node,
9    ) => {
10      if (
11        prevState
12        .scannedIds
13        .includes(node.id)
14      ) {
15        return prevState
16      }
17
18      const contiguousIds = (
19        getContiguousIds({
20          node,
21          nodes,
22        })
23      )
24
25      const {
26        largestContiguousIds,
27        scannedIds,
28      } = prevState
29
30      return {
31        largestContiguousIds: (
32          contiguousIds.length
33          > largestContiguousIds.length
34          ? contiguousIds
35          : largestContiguousIds
36        ),
37        scannedIds: (
38          scannedIds
39          .concat(contiguousIds)
40        ),
41      }
42    },
43    {
44      largestContiguousIds: [],
45      scannedIds: [],
46    },
47  )
48  .largestContiguousIds
49)```

### 递归函数

getousids是我们的递归函数。对每个节点调用一次。每次它返回时，您都会得到一个更新的连续节点列表。

### 顺序迭代

``` 1const getLargestContiguousNodes = (
2  nodes,
3) => (
4  nodes
5  .reduce(
6    (
7      contiguousIdsList,
8      {
10        id,
11      },
12    ) => {
14        contiguousIdsList
15        .reduce(
16          (
18            contiguousIds,
19          ) => (
20            contiguousIds
21            .has(id)
22            ? (
25            )
27          ),
28          new Set(),
29        )
30      )
31
32      return (
34        .size > 0
35        ? (
36          contiguousIdsList
37          .filter((
38            contiguousIds,
39          ) => (
40            !(
42              .has(contiguousIds)
43            )
44          ))
45          .concat(
46            Array
48            .reduce(
49              (
51                contiguousIds,
52              ) => (
53                new Set([
55                  ...contiguousIds,
56                ])
57              ),
59            )
60          )
61        )
62        : (
63          contiguousIdsList
64          .concat(
65            new Set([
67              id,
68            ])
69          )
70        )
71      )
72    },
73    [new Set()],
74  )
75  .reduce((
76    largestContiguousIds = [],
77    contiguousIds,
78  ) => (
79    contiguousIds.size
80    > largestContiguousIds.size
81    ? contiguousIds
82    : largestContiguousIds
83  ))
84)```

### 随机迭代

``` 1const getLargestContiguousNodes = (
2  nodes,
3) => {
4  let contiguousIds = []
5  let largestContiguousIds = []
6  let queuedIds = []
7  let remainingNodesIndex = 0
8
9  let remainingNodes = (
10    nodes
11    .slice()
12  )
13
14  while (remainingNodesIndex < remainingNodes.length) {
15    const [node] = (
16      remainingNodes
17      .splice(
18        remainingNodesIndex,
19        1,
20      )
21    )
22
23    const {
25      id,
26    } = node
27
28    contiguousIds
29    .push(id)
30
31    if (
33      .length > 0
34    ) {
35      queuedIds
37    }
38
39    if (
40      queuedIds
41      .length > 0
42    ) {
43      do {
44        const queuedId = (
45          queuedIds
46          .shift()
47        )
48
49        remainingNodesIndex = (
50          remainingNodes
51          .findIndex(({
52            id,
53          }) => (
54            id === queuedId
55          ))
56        )
57      }
58      while (
59        queuedIds.length > 0
60        && remainingNodesIndex === -1
61      )
62    }
63
64    if (
65      queuedIds.length === 0
66      && remainingNodesIndex === -1
67    ) {
68      if (
69        contiguousIds.length
70        > largestContiguousIds.length
71      ) {
72        largestContiguousIds = contiguousIds
73      }
74
75      contiguousIds = []
76      remainingNodesIndex = 0
77
78      if (
79        remainingNodes
80        .length === 0
81      ) {
82        break
83      }
84    }
85  }
86
87  return largestContiguousIds
88}
89
90module.exports = getLargestContiguousNodesIterative2```

RxJS：可维护性vs性能

1756 篇文章87 人订阅

0 条评论