Skip to content

Instantly share code, notes, and snippets.

View john9francis's full-sized avatar

John Francis john9francis

View GitHub Profile
@john9francis
john9francis / pos_energy_tensor.cc
Created September 20, 2024 02:52
An idea for a tensor class that holds positions as keys and energy as values. That way accessing it by simply myTensor[vector]+=energy adds the energy to that position or creates position and adds the energy. It's just a clever use of std::map.
// Demonstrating a Tensor class. This tensor holds vector3's as
// keys and doubles as values. It has the benefit of ordering the
// vector3's and counting duplicates in the value slot.
// Easy addition to the values.
//
// My use case is storing voxelized information about a phantom
// in a medical physics application and recording total energy
// deposited in each position voxel.
// pos_energy_tensor
@john9francis
john9francis / calculation_cache.c
Created September 6, 2024 03:24
Demonstration of a cache to speed up the performance of a computationally expensive function
/*
README
Simple cache program in C.
The goal here was to create a cache for the 'do_thing' function.
Theoretically do_thing would take a lot of computation and the
use of a cache would speed it up. So if a user puts in the same
value twice e.g. do_thing(2.0), do_thing(2.0) the second call
will extract the already calculated value from the cache instead
of calculating it again.
@john9francis
john9francis / how_many_threads.cc
Last active July 12, 2024 17:29
This simple program has a function that should take 5 seconds. The program creates a user-defined number of threads that each execute the function. This was to test how many threads c++ can handle.
// testing how many threads work in c++
// spoiler alert: at least 2000 work, probably more
#include <thread>
#include <iostream>
#include <chrono>
#include <vector>
void count(int threadNumber){
// Function that counts 5 seconds
@john9francis
john9francis / entangled_copy.cc
Created June 22, 2024 15:11
Copying a class in C++ creates a new instance of that class, but if any variables were declared with "new," those variables don't get copied, only their addresses do. Therefore the original instance and the copy essentially share the same variable.
// showing how copying an instance of a class
// doesn't copy the dynamically allocated variables,
// it simply copies over their address. So the new
// copy shares the old variable.
#include <iostream>
class Thing{
public:
Thing(){ pInt = new int; }
@john9francis
john9francis / this_inheritance.cc
Created June 19, 2024 20:33
How c++ 'this' keyword works with inheritance. Does it take the parent object or the child object?
// explanation:
// Defines 3 classes: Parent, Child, and Thing.
// Thing takes in a parent into its constructor.
// Parent initializes a Thing in its constructor using 'this.'
// Child does nothing but initialize Parent().
//
// main initializes a Child class, and calls a function from Thing.
// Thing, which was initialized using 'this' in the parent constructor,
// uses that 'this' to refer to the Child, not the Parent.
@john9francis
john9francis / map_counting.cc
Created May 4, 2024 12:43
Use case for std::map: counting instances of something
#include <iostream>
#include <string>
#include <map>
#include <vector>
int main()
{
// a map is a really good way to keep track of information.
// if the key doesn't exist, it will be created.
// if the key does exist, it will just reassign the value