Skip to content

Instantly share code, notes, and snippets.

View Morwenn's full-sized avatar
🏳️‍🌈
(∩ᄑ_ᄑ)⊃━☆゚。・:*:・゚’★,。・:*:・゚’☆

Morwenn Morwenn

🏳️‍🌈
(∩ᄑ_ᄑ)⊃━☆゚。・:*:・゚’★,。・:*:・゚’☆
  • Brittany
View GitHub Profile
@Morwenn
Morwenn / branchless_binary_search.h
Last active February 13, 2017 19:47
Branchless binary search insertion
/*
* This function inserts first in [first+1, first+8), which is a
* sorted sequence.
*/
template<typename RandomAccessIterator, typename Compare=std::less<>>
auto front_insert8(RandomAccessIterator first, Compare compare={}) const
-> void
{
auto it = first;
it += compare(it[4], *first) << 2;
@Morwenn
Morwenn / quick_merge_sorter.h
Last active May 5, 2017 13:54
File used to implement quick_merge_sort and wrap it in a sorter
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2016 Morwenn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@Morwenn
Morwenn / make_poplar.hpp
Created February 13, 2017 19:45
Alternative make_poplar implementation
template<typename RandomAccessIterator, typename Compare, typename Projection>
auto make_poplar(RandomAccessIterator first, RandomAccessIterator last,
Compare compare, Projection projection)
-> void
{
auto size = std::distance(first, last);
if (size < 16) {
// A sorted collection is a valid poplar heap;
// when the heap is small, using insertion sort
// should be faster
@Morwenn
Morwenn / lds_sort.hpp
Created March 17, 2017 19:23
Sorting algorithm based on the longest descending subsequence
#include <iterator>
#include <cpp-sort/detail/inplace_merge.h>
#include <cpp-sort/detail/lower_bound.h>
#include <cpp-sort/sorters/pdq_sorter.h>
#include <cpp-sort/utility/as_function.h>
#include <cpp-sort/utility/iter_move.h>
template<typename RandomAccessIterator, typename Compare, typename Projection>
auto lds_sort(RandomAccessIterator first, RandomAccessIterator last,
Compare compare, Projection projection)
@Morwenn
Morwenn / dary_heap_operations.h
Created March 21, 2017 18:46
Heap sorter with d-ary heaps
// // boost heap: d-ary heap as containter adaptor
//
// Copyright (C) 2010 Tim Blechmann
// Modified in 2016 by Morwenn for inclusion into cpp-sort
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef CPPSORT_DETAIL_DARY_HEAP_OPERATIONS_H_
@Morwenn
Morwenn / sorting-network-29.md
Created April 26, 2017 12:23
Sorting network of size 165 for 29 inputs

Sorting network for 29 inputs

The following sorting network for 29 inputs has 165 compare-exchange-units, which is one less that the most size-optimal 29-input sorting networks that I could find in the litterature. Here is how I generated it: first it sorts the first 16 inputs and the last 13 inputs independently. Then it merges the two sorted subarrays using a size 32 Batcher odd-even merge network (the version that does not need the inputs to be interleaved), where all compare-exchange units working on indexes greater than 28 have been dropped. Dropping comparators in such a way is ok: consider that the values at the indexes [29, 32) are greater than every other value in the array to sort, and it will become intuitive that dropping them generates a correct merging network of a smaller size.

That said, even though I have been unable to find a 29-input sorting network with as few compare-exchange units as 165 in the litterature, I can't claim that I found the technique used to generate it: the unclassif

@Morwenn
Morwenn / stable_adapter.h
Created May 27, 2018 17:04
WIP: Another take on stable_adapter
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2018 Morwenn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@Morwenn
Morwenn / split_sort.hpp
Created January 25, 2019 18:58
Simple in-place splitsort implementation
#include <algorithm>
#include <iterator>
template<typename Iterator, typename Compare>
void split_sort(Iterator first, Iterator last, Compare compare)
{
if (std::distance(first, last) < 2) return;
// Read and reorganize elements until middle is found
auto middle = first; // Last element of the LAS
@Morwenn
Morwenn / wtf-merge.hpp
Created May 1, 2019 14:28
Overly complicated merge function
template<typename InputIterator1, typename InputIterator2,
typename OutputIterator, typename Size,
typename Compare, typename Projection>
auto half_inplace_merge5(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Size,
Compare compare, Projection projection)
-> void
{
using utility::iter_move;
@Morwenn
Morwenn / insertion_sort.hpp
Created May 31, 2019 15:12
Try to take advantage of the stable nature of insertion sort
template<typename BidirectionalIterator, typename Compare, typename Projection>
auto insertion_sort(BidirectionalIterator first, BidirectionalIterator last,
stable_compare<Compare, Projection> compare, utility::identity)
-> void
{
auto projection = compare.projection();
cppsort::detail::insertion_sort(
first, last, compare.compare(),
[projection](auto&& value) mutable -> decltype(auto) {
return projection(std::forward<decltype(value)>(value).get());