Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
DmitrySoshnikov / shunting-yard-algorithm.js
Last active April 14, 2024 05:00
Shunting yard algorithm
/**
* Shunting yard algorithm
* See: http://en.wikipedia.org/wiki/Shunting_yard_algorithm
*
* Converts infix notation to postfix notation
*
* by Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
* MIT Style License
*
* (C) 2011, updated on 2020
@DmitrySoshnikov
DmitrySoshnikov / LL1-parser-first-follow-sets.js
Last active March 27, 2024 07:24
LL(1) Parser. Parsing table, part 1: First and Follow sets.
/**
* LL(1) parser. Building parsing table, part 1: First and Follow sets.
*
* NOTICE: see full implementation in the Syntax tool, here:
* https://github.com/DmitrySoshnikov/syntax/blob/master/src/sets-generator.js
*
* by Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
* MIT Style License
*
* An LL(1)-parser is a top-down, fast predictive non-recursive parser,
@DmitrySoshnikov
DmitrySoshnikov / dfs-bfs-non-recursive.js
Created October 19, 2015 05:40
Non-recursive DFS and BFS algorithms
/**
* Depth-first and Breadth-first graph traversals.
*
* In this diff we implement non-recursive algorithms for DFS,
* and BFS maintaining an explicit stack and a queue.
*
* by Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
* MIT Style license
*/

@kangax's ES6 quiz, explained

@kangax created a new interesting quiz, this time devoted to ES6 (aka ES2015). I found this quiz very interesting and quite hard (made myself 3 mistakes on first pass).

Here we go with the explanations:

Question 1:
(function(x, f = () =&gt; x) {
/**
* Educational "Free-list" memory allocator.
*
* Maintains explicit list of free memory blocks, reuses blocks on free.
* Implements "first-fit" strategy. Uses pre-allocated heap of 64 bytes,
* with 32-bit machine word size.
*
* TODO:
*
* - Implement "best-fit" strategy
@DmitrySoshnikov
DmitrySoshnikov / writing-a-mark-sweep-garbage-collector.cpp
Created March 15, 2020 08:35
Writing a Mark-Sweep Garbage Collector
/**
* Writing a Mark-Sweep Garbage Collector in C++.
*
* This is a source code for the Lecture 9 from the
* Essentials of Garbage Collectors course:
*
* http://dmitrysoshnikov.com/courses/essentials-of-garbage-collectors/
*
* See full details in:
*
let offset = 20000;
let chunk_size = 10000;
// File handle:
let mut handle = BufReader::new(File::open("data.bin").await?);
// Set cursor to needed chunk:
let mut chunk_stream = handle
.bytes()
.skip(offset)
/**
* Basic Array class, similar to std::array.
*
* by Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
* MIT Style License (C) 2020
*/
#ifndef __Array_h
#define __Array_h

JNI Example (Mac OS)

JNI (Java Native Interface) allows implementing methods in C/C++, and use them in Java.

1. Create JNIExample.java file

class JNIExample {

  // Native method, no body.
@DmitrySoshnikov
DmitrySoshnikov / infix-to-postfix-regexp.js
Created September 24, 2011 20:14
Infix to postfix notation RegExp converter
/**
* Infix to postfix notation RegExp converter
*
* To implement RegExp machine (NFA, DFA) we need
* to transform first RegExp string to postfix notation
* for more convinient work with the stack. This function
* does exactly this.
*
* See: http://swtch.com/~rsc/regexp/regexp1.html
*