Skip to content

Commit ad11ea3

Browse files
authored
Merge pull request #1 from Aleff13/dev
2 parents af4239e + 897c7ec commit ad11ea3

File tree

16 files changed

+9379
-297
lines changed

16 files changed

+9379
-297
lines changed

.github/workflows/code_quality.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Node.js CI
2+
3+
on:
4+
push:
5+
branches: [master, dev]
6+
pull_request:
7+
branches: [master, dev]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
node-version: [18.x]
16+
17+
steps:
18+
- uses: actions/checkout@v3
19+
- name: Use Node.js ${{ matrix.node-version }}
20+
uses: actions/setup-node@v3
21+
with:
22+
node-version: ${{ matrix.node-version }}
23+
- run: npm ci
24+
- run: npm run build --if-present
25+
- run: npm test

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"files.associations": {
3+
"*.tcc": "cpp",
4+
"iostream": "cpp"
5+
}
6+
}

Algorithms/search/tree.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var tree = {
2+
children: {
3+
children: {
4+
children: {
5+
children: {
6+
children: {
7+
"id": "12345678",
8+
"type": "here"
9+
},
10+
"id": "1234567",
11+
"type": "tp"
12+
},
13+
"id": "123456",
14+
"type": "tp"
15+
},
16+
"id": "12345",
17+
"type": "tp"
18+
},
19+
"id": "1234",
20+
"type": "tp"
21+
},
22+
"id": "123",
23+
"type": "tp"
24+
};
25+
var getIdOfFirstType = function (tree, type) {
26+
if (tree.type == type) {
27+
return tree.id;
28+
}
29+
if (!(tree === null || tree === void 0 ? void 0 : tree.children)) {
30+
return null;
31+
}
32+
return getIdOfFirstType(tree === null || tree === void 0 ? void 0 : tree.children, type);
33+
};
34+
var getAllIdsByType = function (tree, type, ids) {
35+
if (ids === void 0) { ids = []; }
36+
if (tree.type == type) {
37+
ids.push(tree.id);
38+
}
39+
if (!(tree === null || tree === void 0 ? void 0 : tree.children)) {
40+
return ids;
41+
}
42+
return getAllIdsByType(tree === null || tree === void 0 ? void 0 : tree.children, type, ids);
43+
};
44+
var getNodeById = function (tree, id) {
45+
if (tree.id == id) {
46+
return tree.children;
47+
}
48+
if (!(tree === null || tree === void 0 ? void 0 : tree.children)) {
49+
return null;
50+
}
51+
return getNodeById(tree === null || tree === void 0 ? void 0 : tree.children, id);
52+
};
53+
var firstIdOfType = getIdOfFirstType(tree, "here");
54+
console.log(firstIdOfType);
55+
var allIdsOfTypeTp = getAllIdsByType(tree, "tp");
56+
console.log(allIdsOfTypeTp);
57+
var firstNodeOfId = getNodeById(tree, "12345");
58+
console.log(firstNodeOfId);

Algorithms/search/tree.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
interface Children {
2+
children?: Children
3+
id: string
4+
type: string
5+
}
6+
7+
interface Tree {
8+
children?: Children
9+
id: string
10+
type: string
11+
}
12+
13+
const tree = {
14+
children: {
15+
children: {
16+
children: {
17+
children: {
18+
children: {
19+
"id": "12345678",
20+
"type": "here"
21+
},
22+
"id": "1234567",
23+
"type": "tp"
24+
},
25+
"id": "123456",
26+
"type": "tp"
27+
},
28+
"id": "12345",
29+
"type": "tp"
30+
},
31+
"id": "1234",
32+
"type": "tp"
33+
},
34+
"id": "123",
35+
"type": "tp"
36+
}
37+
38+
const getIdOfFirstType = (tree: Tree | Children, type: string): void | string | null => {
39+
if (tree.type == type) {
40+
return tree.id
41+
}
42+
43+
if (!tree?.children) {
44+
return null
45+
}
46+
47+
return getIdOfFirstType(tree?.children, type)
48+
}
49+
50+
const getAllIdsByType = (tree: Tree | Children, type: string, ids: string[] = []): void | string[] | null => {
51+
if (tree.type == type) {
52+
ids.push(tree.id)
53+
}
54+
55+
if (!tree?.children) {
56+
return ids
57+
}
58+
59+
return getAllIdsByType(tree?.children, type, ids)
60+
}
61+
62+
const getNodeById = (tree: Tree | Children, id: string): void | Children | null => {
63+
if (tree.id == id) {
64+
return tree.children
65+
}
66+
67+
if (!tree?.children) {
68+
return null
69+
}
70+
71+
return getNodeById(tree?.children, id)
72+
}
73+
74+
const firstIdOfType = getIdOfFirstType(tree, "here")
75+
console.log(firstIdOfType)
76+
77+
const allIdsOfTypeTp = getAllIdsByType(tree, "tp")
78+
console.log(allIdsOfTypeTp)
79+
80+
const firstNodeOfId = getNodeById(tree, "12345")
81+
console.log(firstNodeOfId)
82+

Stack/cpp/stack.addon.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
var stack_lib_1 = require("./stack.lib");
4+
var Stack = /** @class */ (function () {
5+
function Stack() {
6+
this.obj = stack_lib_1["default"].createStack();
7+
}
8+
Stack.prototype.push = function (value) {
9+
stack_lib_1["default"].push(this.obj, value);
10+
};
11+
Stack.prototype.pop = function () {
12+
return stack_lib_1["default"].keyToHash(this.obj);
13+
};
14+
Stack.prototype.peek = function () {
15+
return stack_lib_1["default"].peek(this.obj);
16+
};
17+
Stack.prototype.destructor = function () {
18+
stack_lib_1["default"].deleteStack(this.obj);
19+
};
20+
return Stack;
21+
}());
22+
exports["default"] = Stack;
23+
// const stack = new Stack();
24+
// stack.push("first");
25+
// stack.peek();
26+
// console.log(stack.peek())

Stack/cpp/stack.addon.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import stackLib from './stack.lib';
2+
3+
class Stack {
4+
obj: any;
5+
6+
constructor() {
7+
this.obj = stackLib.createStack();
8+
}
9+
10+
push(value: string){
11+
stackLib.push(this.obj, value);
12+
}
13+
14+
pop() {
15+
return stackLib.keyToHash(this.obj);
16+
}
17+
18+
peek() {
19+
return stackLib.peek(this.obj);
20+
}
21+
22+
destructor() {
23+
stackLib.deleteStack(this.obj);
24+
}
25+
}
26+
27+
export default Stack
28+
29+
// const stack = new Stack();
30+
// stack.push("first");
31+
// stack.peek();
32+
33+
// console.log(stack.peek())

Stack/cpp/stack.cpp

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Stack
1818
}
1919
}
2020

21+
// Adds an element in top
2122
void push(const char *value)
2223
{
2324
size = size + 1;
@@ -26,34 +27,64 @@ class Stack
2627
stack[size] = newValue;
2728
}
2829

30+
// Removes the element in top
2931
void pop()
3032
{
3133
stack[size] = NULL;
3234
size = size - 1;
3335
}
3436

37+
// Show the value in top
3538
const char *peek()
3639
{
3740
return stack[size];
3841
}
3942
};
4043

41-
int main()
44+
extern "C"
4245
{
43-
Stack stackzin;
46+
Stack *createStack()
47+
{
48+
return new Stack();
49+
}
50+
51+
void deleteStack(Stack *obj)
52+
{
53+
delete obj;
54+
}
55+
56+
void push(Stack *obj, const char *value)
57+
{
58+
return obj->push(value);
59+
}
60+
61+
void pop(Stack *obj)
62+
{
63+
obj->pop();
64+
}
65+
66+
const char *peek(Stack *obj)
67+
{
68+
return obj->peek();
69+
}
70+
}
71+
72+
// int main()
73+
// {
74+
// Stack stackzin;
4475

45-
stackzin.push("uhu");
46-
stackzin.push("valor");
76+
// stackzin.push("uhu");
77+
// stackzin.push("valor");
4778

48-
cout << stackzin.peek() << endl; // show the last added element
79+
// cout << stackzin.peek() << endl; // show the last added element
4980

50-
stackzin.pop();
81+
// stackzin.pop();
5182

52-
cout << stackzin.peek() << endl; // show the last added element
83+
// cout << stackzin.peek() << endl; // show the last added element
5384

54-
stackzin.push("bloft");
85+
// stackzin.push("bloft");
5586

56-
cout << stackzin.peek() << endl; // show the last added element
87+
// cout << stackzin.peek() << endl; // show the last added element
5788

58-
return 0;
59-
}
89+
// return 0;
90+
// }

Stack/cpp/stack.lib.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
var ffi = require("ffi-napi");
4+
//import c++ lib
5+
var stackLib = ffi.Library("../cpp/stack.so", {
6+
"createStack": ['pointer', []],
7+
'deleteStack': ['void', ['pointer']],
8+
"push": ['void', ['pointer', 'string']],
9+
"pop": ['void', ['pointer']],
10+
"peek": ['string', ['pointer']]
11+
});
12+
exports["default"] = stackLib;

Stack/cpp/stack.lib.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const ffi = require("ffi-napi")
2+
//import c++ lib
3+
const stackLib = ffi.Library("../cpp/stack.so", {
4+
"createStack": ['pointer', []],
5+
'deleteStack': ['void', ['pointer']],
6+
"push": ['void', ['pointer', 'string']],
7+
"pop": ['void', ['pointer']],
8+
"peek": ['string', ['pointer']],
9+
});
10+
11+
export default stackLib

Stack/cpp/stack.o

4.95 KB
Binary file not shown.

Stack/cpp/stack.so

16.7 KB
Binary file not shown.

jest.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'node',
5+
};

0 commit comments

Comments
 (0)