ESP32 / ESP8266 cpplinq: Finding the intersection between two arrays

In this tutorial we will learn how to find the intersection between two arrays, using the cpplinq library. The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The tests on the ESP8266 were performed on a DFRobot’s ESP8266 FireBeetle board.

Introduction

In this tutorial we will learn how to find the intersection between two arrays, using the cpplinq library. We will be using the Arduino core.

The code we will analyze on this post works both on the ESP32 and on the ESP8266. Since cpplinq is a C++ library, what we are going to learn on this tutorial should also be usable in general C++ programming.

The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The tests on the ESP8266 were performed on a DFRobot’s ESP8266 FireBeetle board.

The code

We will start the code by importing the cpplinq library and then declaring the use of the cpplinq namespace.

#include "cpplinq.hpp"
   
using namespace cpplinq;

Moving on to the Arduino setup, where we will write the rest of the code, we will start by opening a serial connection, to output the results of the program.

Serial.begin(115200);

Then we will define two arrays of integers. These will be the arrays over which we will apply the intersection operation.

int array1[] = {1,2,3,4};
int array2[] = {3,4,5,6};

Before we start using the cpplinq operators, we first need to convert our arrays to range objects. We will start by converting array1 to a range by calling the from_array operator.

Then we will apply the intersect_with operator to array1. As the name of the operator implies, we need to pass as input the second range to use in the intersection operation.

The intersect_with operator receives as input a range object, which means we need to also convert array2 to this type.

intersect_with(from_array(array2))

To finalize, we will convert the result to a format that we can iterate, so we are able to print the elements of the intersection. We will perform this conversion by calling the to_vector operator.

to_vector()

The full expression tree can be seen below.

auto result = from_array(array1)
           >> intersect_with(from_array(array2))
           >> to_vector();

Finally, we will iterator over our vector and print each element to the serial port.

for(int i=0; i<result.size(); ++i){
    Serial.print(result[i]);
    Serial.print("|");
}

The full code can be seen below.

#include "cpplinq.hpp"
   
using namespace cpplinq;
   
void setup() {
  Serial.begin(115200);
     
  int array1[] = {1,2,3,4};
  int array2[] = {3,4,5,6};
     
  auto result = from_array(array1)
               >> intersect_with(from_array(array2))
               >> to_vector();
 
  for(int i=0; i<result.size(); ++i){
    Serial.print(result[i]);
    Serial.print("|");
  }
 
}
   
void loop() {}

Testing the code

To test the code, simply compile it and upload it to your device, using the Arduino IDE. After the procedure ends, open the IDE serial monitor to obtain the output of the program.

You should see a result similar to figure 1. As can be seen, we have obtained the two elements that are common to both arrays, which corresponds to the intersection between both.

Output of the program, showing the intersection of both arrays,
Figure 1 – Output of the program, showing the intersection between the two arrays. Taken from the tests on the ESP32.

Leave a Reply

%d bloggers like this: