Dictionary Object

Is there any good fast way of dealing with a dictionary type object? I ran across IonDB but I'm not sure how to get it setup. Also didn't know if that was a good way

robertpickett3:
Is there any good fast way of dealing with a dictionary type object? I ran across IonDB but I’m not sure how to get it setup. Also didn’t know if that was a good way

What’s your hardware? If you have access to stl libraries, you can use std::map.

#include <map>

std::map<int,int> my_dictionary;

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

I’m trying to do it on an Uno

robertpickett3:
I’m trying to do it on an Uno

Good luck!

robertpickett3:
I’m trying to do it on an Uno

An Uno won't have necessary resources or power to run any kind of meaningful dictionary. What are you trying to do?

I saw somewhere that some guy had made a bunch of Arduinos that were addressed at at startup and daisy-chained tx to rx of the next. Basically it allows for a huge number of arduinos to be strung together in daisy chain. I have the addressing worked out as it is pretty simple, but I wanted the master to keep a dictionary of addresses and a “Type” name that would be what the Arduino is. (Eg. LED, Servo, etc.) Then the master would send commands to the addresses in the dictionary and also be able to track whether the command was appropriate for that particular kind of device.

How about an array of structs?

Here’s a simple dictionary, try and see if it works for you.
Note: this is just a simple version, it lacks common functionalities.

template<typename ValueType, size_t ElementSize=5, size_t KeySize=10>
class SimpleDictionary
{
public:
    SimpleDictionary() :
        Keys{{}},
        Values{}
    {
    }

    ValueType& operator[](const char* InKey)
    {
        int Index = LocateKey(InKey);
        if (Index == -1)
        {
            Index = LocateKey("");
        }
        if (Index != -1)
        {
            strncpy(Keys[Index], InKey, KeySize);
        } else
        {
            //The dictionary is full at this point
            //no handle
        }
        return Values[Index];
    }
private:
    char Keys[ElementSize][KeySize];
    ValueType Values[ElementSize];
    int LocateKey(const char* InKey)
    {
        for (size_t i=0; i<ElementSize; ++i)
        {
            if (strncmp(Keys[i], InKey, KeySize) == 0)
            {
                return i;
            }
        }
        return -1;
    }
};

void setup() {
  Serial.begin(115200);
  SimpleDictionary<int> IntDictionary;
  IntDictionary["Red"] = 100;
  IntDictionary["Blue"] = -1457;

  Serial.println(IntDictionary["Red"]);
  Serial.println(IntDictionary["Blue"]);

  constexpr size_t ElementSize = 10, KeySize = 20;
  SimpleDictionary<float, ElementSize, KeySize> FloatDictionary;
  FloatDictionary["Cube"] = 1.2;
  FloatDictionary["Sphere"] = 2.5;
}

void loop() {
  // put your main code here, to run repeatedly:

}

Is there a way to do a char array dictionary with that simple dictionary class?

robertpickett3:
Is there a way to do a char array dictionary with that simple dictionary class?

Can you give an example?

Like

{1:”servo”}

robertpickett3:
Like

{1:”servo”}

So the key will always be integer and the values are text?

Yup

robertpickett3:
Yup

It sounds like you need an array of enums, not a dictionary.

Pieter

robertpickett3:
Yup

You can try this:

template<size_t Size> struct Value
{
  Value() : Buffer{} {}
  char Buffer[Size];
  Value& operator=(const char* InValue)
  {
    strncpy(Buffer, InValue, Size);
    return *this;
  }

  bool operator==(const char* InValue)
  {
    return strncmp(Buffer, InValue, Size) == 0;
  }
};

template<size_t ElementSize=5, size_t BufferSize=10> struct SimplerDictionary
{
  Value<BufferSize>& operator[](int Index)
  {
    if (Index < 0 || Index >= (int)ElementSize)
    {
      return Internal;
    }
    return Values[Index];
  }
  Value<BufferSize> Values[ElementSize];
  Value<BufferSize> Internal;
};

void setup() {
  Serial.begin(115200);
  // put your setup code here, to run once:
  SimplerDictionary<> Dic;
  Dic[0] = "Servo";

  if (Dic[0] == "Servo")
  {
    Serial.println("Servo");
  } 
  else 
  {
    Serial.println("Not Servo");
  }
  
}

void loop() {
  // put your main code here, to run repeatedly:

}

Though, a simple array as suggested would do the job.

If you are talking about python dictionaries or php Arrays without any predefined size, you can Do the following:

You can create a struct with ID and following_memory.

Following memory has the memory Adress of next Element. That way, removing it from dictionaty would take only changing the next Element of previous Element. And adding a New Element to a certain Position is also easy. Therefore you have huge advantages to Standard Arrays.

I had the same requirements for my old project. But then I realized, it is much easier to use an oversized array than writing my own dictionary with using memory reads.