Skip to content

Instantly share code, notes, and snippets.

struct Int {
int x;
Int() = default;
Int(int x) : x(x) {
}
Int(const Int& rhs) {
UpdateCounter();
x = rhs.x;
}
@commander-trashdin
commander-trashdin / dfs.lisp
Last active January 18, 2020 13:19
a dfs macro
(defmacro dfs (graph start visitor) ;; graph is an adjacency vector
"(initialize-vertex visitor vertex) is invoked on every vertex of the graph before the start of the graph search.
(start-vertex visitor vertex) is invoked on the source vertex once before the start of the search.
(discover-vertex visitor vertex) is invoked when a vertex is encountered for the first time.
(examine-edge visitor edge) is invoked on every out-edge of each vertex after it is discovered.
(tree-edge visitor edge) is invoked on each edge as it becomes a member of the edges that form the search tree. If you wish to record predecessors, do so at this event point.
(back-edge visitor edge is invoked on the back edges in the graph.
(cross-edge visitor edge) is invoked on cross edges in the graph. In an undirected graph this method is never called.
I have no idea about what to do with forward edges.
(finish-edge visitor edge) is invoked on the non-tree edges in the graph as well as on each tree edge after its target vertex is finished.
@commander-trashdin
commander-trashdin / ks.cpp
Created March 7, 2020 02:41
B&B, a quick one
struct Item {
Item() = default;
Item(int value, int weight) : value_(value), weight_(weight) {
}
int value_;
int weight_;
};
@commander-trashdin
commander-trashdin / orgstuff.el
Created May 18, 2020 01:08
I'm just trying to help
(defun tt-heading-description ()
"Get header and description of first header element in buffer."
(with-temp-buffer
(insert "
* Top Heading
Some description
* Second Heading
Other description
")
(let ((first-headline (seq-find (lambda (part) (eql (car part) 'headline)) (org-element-parse-buffer))))
@commander-trashdin
commander-trashdin / currying.lisp
Last active June 11, 2020 17:25
My attempt at making almost first-class currying in CL.
(defun intern-gen-sym (&optional str)
(if str
(intern (format nil "intern-g~a~s" str (incf *gensym-counter*)))
(intern (format nil "intern-g~s" (incf *gensym-counter*)))))
(define-condition recursive-currying-lambda (error)
((datum :initform "Cannot curry lambda with 0 arguments" :allocation :class)))
(defun parse-annotation-lines (list)
"Pull out header and item information from LIST."
(let (acc (header-is-next nil) (item-is-next nil) (current-page))
(nreverse
(dolist (element list acc)
(cond (header-is-next
(push (make-parse-entry *header+ element current-page) acc)
(setq header-is-next nil))
(item-is-next
@commander-trashdin
commander-trashdin / infix-mode.lisp
Last active June 2, 2020 12:41 — forked from monroth/infix-mode.lisp
Infix mode for Common Lisp
(defparameter *infix-operator-order* '((!) (^ :reverse) (* /) (+ -) (> < >= <= =) (and) (or) (eql)))
(defparameter *infix-operator-set* '(+ * ^ - / ! = and or eql))
(defparameter *unary-operator-set* '(!))
(defparameter *infix-operator-hash* (let ((local-hash (make-hash-table)))
(setf (gethash '+ local-hash) '+)
(setf (gethash '* local-hash) '*)
(setf (gethash '^ local-hash) 'expt)
@commander-trashdin
commander-trashdin / dis.repl
Created June 8, 2020 07:19
Disassembly for different types with +.
CL-USER> (disassemble (lambda (a b) (declare (optimize (speed 3) (safety 0) (compilation-speed 0)))
(declare (long-float a) (long-float b))
(+ a b)))
; disassembly for (LAMBDA (A B))
; Size: 65 bytes. Origin: #x52C50EE0 ; (LAMBDA (A B))
; EE0: F20F58D1 ADDSD XMM2, XMM1
; EE4: 49896D28 MOV [R13+40], RBP ; thread.pseudo-atomic-bits
; EE8: 498B5568 MOV RDX, [R13+104] ; thread.alloc-region
; EEC: 4C8D5A10 LEA R11, [RDX+16]
; EF0: 4D3B5D70 CMP R11, [R13+112]
@commander-trashdin
commander-trashdin / optimization.lisp
Created June 8, 2020 12:22
Constantfolding go brrrrr
(defpackage #:optimizations
(:use #:cl)
(:shadow #:+))
(in-package #:optimizations)
(declaim (inline quaternion))
(defstruct quaternion
@commander-trashdin
commander-trashdin / optimization.lisp
Created June 8, 2020 12:22
Constantfolding go brrrrr
(defpackage #:optimizations
(:use #:cl)
(:shadow #:+))
(in-package #:optimizations)
(declaim (inline quaternion))
(defstruct quaternion