Posts Tagged algorithm

C++ For C# Developers: Part 49 – Ranges and Parallel Algorithms

Tags: , , ,

Generic algorithms have been available in C++ for decades, but the last two versions of the language have really ramped up the functionality. C++17 added support for parallel execution of generic algorithms to easily take advantage of multi-core CPUs. Then C++20 added support for ranges, a composable version of generic algorithms that’s even closer to LINQ in C#. Today we’ll explore both of these!

Read the rest of this article »

No Comments

C++ For C# Developers: Part 48 – Algorithms Library

Tags: , ,

The C++ Standard Library’s algorithms are culmination of a lot of C++ language and library features. They’re like a much more featureful, much faster version of LINQ in C#. This powerful combination makes most “raw” loops unnecessary as they can be replaced by named function calls that are well-tested and often compile to the same machine code as a “raw” loop. Read on to learn about them!

Read the rest of this article »

No Comments

LINQ-Style Generic Algorithms for Burst

Tags: , , ,

We’ve seen how to create powerful, Burst-compatible generic algorithms already, but today we’ll take another approach to generic algorithms and implement them in the style of C#’s LINQ. Along the way, we’ll tackle a new challenge by implementing a generic algorithm that allocates a new collection.

Read the rest of this article »

2 Comments

Type-Agnostic Generic Algorithms

Tags: , , ,

We looked at some generic algorithm examples in the previous article, but they weren’t very generic in one respect: they all required a NativeArray<T>. What if we wanted to make them more generic so they could work on any type of collection? Today’s article shows two ways to do just that!

Read the rest of this article »

2 Comments

Generic Algorithm Examples

Tags: , ,

There are many common algorithms that we use constantly: searching, filtering, mapping, and so forth. Today we’ll write a few in the Burst-friendly way as examples of how to write Burst-friendly code that’s still reusable and efficient.

Read the rest of this article »

4 Comments

Burst-Compiled Generic Algorithms

Tags: , , ,

Many algorithms get used over and over: searching, sorting, filtering, etc. C# makes these available with LINQ and functions like Array.Sort, but these can’t be compiled by Burst because interfaces and delegates aren’t supported. So how do we implement these generic algorithms to avoid re-writing them over and over? Today we’ll explore one technique to solve this problem. Read on to learn how!

Read the rest of this article »

5 Comments

Enumerables Without the Garbage: Part 8

Tags: , , , , , , ,

NativeArray<T> is great, but very limited in functionality. We can fix this surprisingly easily! Today we revive a two year old series that created the iterator project. Iterators are like a no-GC version of IEnumerable<T> and LINQ which have a lot of power but only support managed arrays (T[]) and List<T>. Today we’ll add support for NativeArray<T> and inherit support for the same functionality. We’ll also spruce up the project with proper unit tests, assembly definitions, and runtime tests to confirm that zero garbage is created. Read on to see how this was done and how to use iterators with NativeArray<T>.

Read the rest of this article »

3 Comments

Enumerables Without the Garbage: Part 7

Tags: , , , , , ,

Today we’ll wrap up the iterator series by finishing up porting C++’s <algorithm> header. We end up with a library of functions for common LINQ-style algorithms but without any of the garbage creation that slows our games down. Read on for the source and examples!

Read the rest of this article »

No Comments

Enumerables Without the Garbage: Part 6

Tags: , , , , , ,

We’re nearing the end of the series to build a no-garbage replacement for System.Linq. Today we tackle functions that work on already-sorted ranges and functions that work on ranges that are in heap order. These include common set operations like “union” and “intersection”. Read on to see how to use them and for the updated library that you can use to eliminate your garbage creation!

Read the rest of this article »

No Comments

Enumerables Without the Garbage: Part 5

Tags: , , , , , ,

This week we continue with iterators to get the functionality of IEnumerable without the nasty garbage creation. This week the little iterator library gets support for sorting and binary searching. Read on for the details!

Read the rest of this article »

2 Comments