ESP32 / ESP8266 cpplinq: Finding the union between two arrays

In this tutorial we will learn how to find the union of two arrays, using the C++ 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 union of two arrays, using the C++ cpplinq library. This will be done using the Arduino core. The code that will be shown on this tutorial works both on the ESP8266 and on the ESP32.

When applying the union operation over two arrays, our aim is to find the set of elements that are in array 1, array 2 or both . You can read a more mathematical explanation about the union operation here.

In cpplinq, this operation can be achieved using the union_with operator, as we will cover in more detail in the next section.

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 our code by including the cpplinq library and declaring the use of the cpplinq namespace.

#include "cpplinq.hpp"
  
using namespace cpplinq;

Moving on to the Arduino setup, we will start by opening a serial connection, so we are able to output the results of our program.

Serial.begin(115200);

Then we will define the two arrays of integers over which we will apply the union operation.

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

After this we will need to convert one of the arrays to a range object, so we can then apply the union_with operator. We will convert array1 first.

from_array(array1)

Then we will apply the union_with operator. Since this operator receives as input another range (the one to perform the union with) we will also need to convert array2 to a range object.

Note that the union_with operator will return a new range with the result of the operation.

union_with(from_array(array2))

Finally, we need to convert the resulting range back to a format that we can iterate, so we can print each element to the serial port. We will convert it to a C++ vector, using the to_vector operator.

The full expression tree can be seen below.

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

After that we will iterate over the vector and print all the elements to the serial port.

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

For comparison, we will repeat the procedure but now applying the union_with operator over array2 and passing as input array1.

auto result2 = from_array(array2)
               >> union_with(from_array(array1))
               >> to_vector();
                 
for(int i=0; i<result2.size(); ++i){
    Serial.print(result2[i]);
    Serial.print("|");
}

The complete 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)
               >> union_with(from_array(array2))
               >> to_vector();

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

  
  auto result2 = from_array(array2)
               >> union_with(from_array(array1))
               >> to_vector();
  
  Serial.println("\nResult 2:");              
  for(int i=0; i<result2.size(); ++i){
    Serial.print(result2[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. Once the procedure finishes, open the Serial Monitor.

You should get an output similar to figure 1. As can be seen, in both cases, we have obtained the union of the arrays. Depending on over which array the operator was applied and on which array was passed as input, the order of the elements of the resulting range differs.

Naturally, we can easily chain the union_with operator with some ordering operator and always obtain an ordered final result.

Output of the program, showing the result of the union operation when applied in different order.
Figure 1 – Output of the program in the Arduino IDE serial monitor. Taken from the tests on the ESP32.

One Reply to “ESP32 / ESP8266 cpplinq: Finding the union between two arrays”

Leave a Reply