Skip to content

Commit

Permalink
Merge pull request #1 from Aleff13/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleff13 authored Oct 14, 2023
2 parents af4239e + 897c7ec commit ad11ea3
Show file tree
Hide file tree
Showing 16 changed files with 9,379 additions and 297 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/code_quality.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Node.js CI

on:
push:
branches: [master, dev]
pull_request:
branches: [master, dev]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x]

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files.associations": {
"*.tcc": "cpp",
"iostream": "cpp"
}
}
58 changes: 58 additions & 0 deletions Algorithms/search/tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var tree = {
children: {
children: {
children: {
children: {
children: {
"id": "12345678",
"type": "here"
},
"id": "1234567",
"type": "tp"
},
"id": "123456",
"type": "tp"
},
"id": "12345",
"type": "tp"
},
"id": "1234",
"type": "tp"
},
"id": "123",
"type": "tp"
};
var getIdOfFirstType = function (tree, type) {
if (tree.type == type) {
return tree.id;
}
if (!(tree === null || tree === void 0 ? void 0 : tree.children)) {
return null;
}
return getIdOfFirstType(tree === null || tree === void 0 ? void 0 : tree.children, type);
};
var getAllIdsByType = function (tree, type, ids) {
if (ids === void 0) { ids = []; }
if (tree.type == type) {
ids.push(tree.id);
}
if (!(tree === null || tree === void 0 ? void 0 : tree.children)) {
return ids;
}
return getAllIdsByType(tree === null || tree === void 0 ? void 0 : tree.children, type, ids);
};
var getNodeById = function (tree, id) {
if (tree.id == id) {
return tree.children;
}
if (!(tree === null || tree === void 0 ? void 0 : tree.children)) {
return null;
}
return getNodeById(tree === null || tree === void 0 ? void 0 : tree.children, id);
};
var firstIdOfType = getIdOfFirstType(tree, "here");
console.log(firstIdOfType);
var allIdsOfTypeTp = getAllIdsByType(tree, "tp");
console.log(allIdsOfTypeTp);
var firstNodeOfId = getNodeById(tree, "12345");
console.log(firstNodeOfId);
82 changes: 82 additions & 0 deletions Algorithms/search/tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
interface Children {
children?: Children
id: string
type: string
}

interface Tree {
children?: Children
id: string
type: string
}

const tree = {
children: {
children: {
children: {
children: {
children: {
"id": "12345678",
"type": "here"
},
"id": "1234567",
"type": "tp"
},
"id": "123456",
"type": "tp"
},
"id": "12345",
"type": "tp"
},
"id": "1234",
"type": "tp"
},
"id": "123",
"type": "tp"
}

const getIdOfFirstType = (tree: Tree | Children, type: string): void | string | null => {
if (tree.type == type) {
return tree.id
}

if (!tree?.children) {
return null
}

return getIdOfFirstType(tree?.children, type)
}

const getAllIdsByType = (tree: Tree | Children, type: string, ids: string[] = []): void | string[] | null => {
if (tree.type == type) {
ids.push(tree.id)
}

if (!tree?.children) {
return ids
}

return getAllIdsByType(tree?.children, type, ids)
}

const getNodeById = (tree: Tree | Children, id: string): void | Children | null => {
if (tree.id == id) {
return tree.children
}

if (!tree?.children) {
return null
}

return getNodeById(tree?.children, id)
}

const firstIdOfType = getIdOfFirstType(tree, "here")
console.log(firstIdOfType)

const allIdsOfTypeTp = getAllIdsByType(tree, "tp")
console.log(allIdsOfTypeTp)

const firstNodeOfId = getNodeById(tree, "12345")
console.log(firstNodeOfId)

26 changes: 26 additions & 0 deletions Stack/cpp/stack.addon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use strict";
exports.__esModule = true;
var stack_lib_1 = require("./stack.lib");
var Stack = /** @class */ (function () {
function Stack() {
this.obj = stack_lib_1["default"].createStack();
}
Stack.prototype.push = function (value) {
stack_lib_1["default"].push(this.obj, value);
};
Stack.prototype.pop = function () {
return stack_lib_1["default"].keyToHash(this.obj);
};
Stack.prototype.peek = function () {
return stack_lib_1["default"].peek(this.obj);
};
Stack.prototype.destructor = function () {
stack_lib_1["default"].deleteStack(this.obj);
};
return Stack;
}());
exports["default"] = Stack;
// const stack = new Stack();
// stack.push("first");
// stack.peek();
// console.log(stack.peek())
33 changes: 33 additions & 0 deletions Stack/cpp/stack.addon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import stackLib from './stack.lib';

class Stack {
obj: any;

constructor() {
this.obj = stackLib.createStack();
}

push(value: string){
stackLib.push(this.obj, value);
}

pop() {
return stackLib.keyToHash(this.obj);
}

peek() {
return stackLib.peek(this.obj);
}

destructor() {
stackLib.deleteStack(this.obj);
}
}

export default Stack

// const stack = new Stack();
// stack.push("first");
// stack.peek();

// console.log(stack.peek())
53 changes: 42 additions & 11 deletions Stack/cpp/stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Stack
}
}

// Adds an element in top
void push(const char *value)
{
size = size + 1;
Expand All @@ -26,34 +27,64 @@ class Stack
stack[size] = newValue;
}

// Removes the element in top
void pop()
{
stack[size] = NULL;
size = size - 1;
}

// Show the value in top
const char *peek()
{
return stack[size];
}
};

int main()
extern "C"
{
Stack stackzin;
Stack *createStack()
{
return new Stack();
}

void deleteStack(Stack *obj)
{
delete obj;
}

void push(Stack *obj, const char *value)
{
return obj->push(value);
}

void pop(Stack *obj)
{
obj->pop();
}

const char *peek(Stack *obj)
{
return obj->peek();
}
}

// int main()
// {
// Stack stackzin;

stackzin.push("uhu");
stackzin.push("valor");
// stackzin.push("uhu");
// stackzin.push("valor");

cout << stackzin.peek() << endl; // show the last added element
// cout << stackzin.peek() << endl; // show the last added element

stackzin.pop();
// stackzin.pop();

cout << stackzin.peek() << endl; // show the last added element
// cout << stackzin.peek() << endl; // show the last added element

stackzin.push("bloft");
// stackzin.push("bloft");

cout << stackzin.peek() << endl; // show the last added element
// cout << stackzin.peek() << endl; // show the last added element

return 0;
}
// return 0;
// }
12 changes: 12 additions & 0 deletions Stack/cpp/stack.lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";
exports.__esModule = true;
var ffi = require("ffi-napi");
//import c++ lib
var stackLib = ffi.Library("../cpp/stack.so", {
"createStack": ['pointer', []],
'deleteStack': ['void', ['pointer']],
"push": ['void', ['pointer', 'string']],
"pop": ['void', ['pointer']],
"peek": ['string', ['pointer']]
});
exports["default"] = stackLib;
11 changes: 11 additions & 0 deletions Stack/cpp/stack.lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const ffi = require("ffi-napi")
//import c++ lib
const stackLib = ffi.Library("../cpp/stack.so", {
"createStack": ['pointer', []],
'deleteStack': ['void', ['pointer']],
"push": ['void', ['pointer', 'string']],
"pop": ['void', ['pointer']],
"peek": ['string', ['pointer']],
});

export default stackLib
Binary file added Stack/cpp/stack.o
Binary file not shown.
Binary file added Stack/cpp/stack.so
Binary file not shown.
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
Loading

0 comments on commit ad11ea3

Please sign in to comment.