Skip to content

Instantly share code, notes, and snippets.

View ugovaretto's full-sized avatar

Ugo Varetto ugovaretto

View GitHub Profile
@ugovaretto
ugovaretto / equalizer.rs
Created March 15, 2022 07:41
Height equalizer
/// Tower equaliser: givern list of tower heights return the minimum number
/// of moves required to obtain tower of the same height or return "No solution"
fn towers(t: &[u32]) -> Option<u32> {
let mut moves: u32 = 0;
let mut n = t.to_vec();
let max_moves: u32 = n.iter().sum();
loop {
let imax = match n.iter().enumerate().max_by(|&(_, x), &(_, y)| x.cmp(&y)) {
Some((m, _)) => m,
_ => usize::MIN,
@ugovaretto
ugovaretto / Cargo.toml
Last active February 14, 2022 13:25
Create web service that sends a request to github api
[package]
name = "rocket_reqw"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = "0.4"
serde = {version = "1", features = ["derive"]}
@ugovaretto
ugovaretto / Task-based executor.cpp
Last active February 12, 2022 02:54
Task executor and wrapper for concurrent access to variable
// Author: Ugo Varetto
// Implementation of task-based concurrency (similar to Java's Executor)
// and wrapper for concurrent access
// g++ task-based-executor-concurrent-generic.cpp -std=c++11 -pthread
#include <algorithm>
#include <chrono>
#include <condition_variable>
#include <cstdlib> //EXIT_*
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)
@ugovaretto
ugovaretto / barrier.cpp
Created April 6, 2021 16:09
C++ barrier
#include <chrono>
#include <condition_variable>
#include <iostream>
#include <thread>
class Barrier {
public:
Barrier(uint32_t count) : threadCount(count), counter(0), waiting(0) {}
void wait() {
@ugovaretto
ugovaretto / du_hidden.sh
Last active November 16, 2020 04:16
List disk usage for hidden files
#!/usr/bin/env bash
du -sch .[!.]* * | sort -h
@ugovaretto
ugovaretto / vector_allocation.cpp
Created October 24, 2020 02:54
Uninitialized vector
// Author: Ugo Varetto
// buffer allocation performance tests: vector, vector+pod allocator (same as
// vector), raw aligned buffer (faster)
#include <sys/mman.h>
#include <unistd.h>
#include <cstdlib>
#include <ctime>
#include <iostream>
@ugovaretto
ugovaretto / pwcpp_mm.cpp
Created October 15, 2020 15:32
Parallel copy with memory mapped files
/*******************************************************************************
* BSD 3-Clause License
*
* Copyright (c) 2020, Ugo Varetto
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions inputFile source code must retain the above copyright
@ugovaretto
ugovaretto / pwcp.cpp
Created October 15, 2020 07:43
Parallel copy
/*******************************************************************************
* BSD 3-Clause License
*
* Copyright (c) 2020, Ugo Varetto
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions inputFile source code must retain the above copyright
@ugovaretto
ugovaretto / csiro_license.txt
Created October 15, 2020 01:13
CSIRO open-source software license
CSIRO Open Source Software License Agreement (variation of the BSD / MIT License)
Copyright (c) 2016, Commonwealth Scientific and Industrial Research Organisation (CSIRO) ABN 41 687 119 230.
All rights reserved. CSIRO is willing to grant you a license to this tool named PSL:Proof Strategy Language on the following terms, except where otherwise indicated for third party material.
Redistribution and use of this software in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of CSIRO nor the names of its contributors may be used to endorse or promote products derived from this software without specific p