Skip to content

Instantly share code, notes, and snippets.

@jerstlouis
Created August 15, 2022 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jerstlouis/2718a7db45cfc4c482aaeda7f111c1e8 to your computer and use it in GitHub Desktop.
Save jerstlouis/2718a7db45cfc4c482aaeda7f111c1e8 to your computer and use it in GitHub Desktop.
import "ecere"
class MyCube : Cube
{
int extraStuff;
}
Map<String, Array<MyCube>> cubesMap { };
class CubesMapTestForm : Window
{
caption = $"Cubes Array Map Test";
background = formColor;
borderStyle = sizable;
hasMaximize = true;
hasMinimize = true;
hasClose = true;
clientSize = { 320, 304 };
Button btnFind
{
this, caption = $"Find", position = { 120, 24 };
bool NotifyClicked(Button button, int x, int y, Modifiers mods)
{
const String buf1 = ebFindKey.contents;
MapIterator<String, Array<MyCube>> it { map = cubesMap };
// 'false' is for 'don't create if not found'
if(it.Index(buf1, false))
{
Array<MyCube> cubesArray = it.data;
String s = PrintString($"Found ", cubesArray.count, $" by that name");
lbMessage.caption = s;
delete s;
}
else
lbMessage.caption = $"No cubes found by that name";
return true;
}
};
Button btnAdd
{
this, caption = $"Add", anchor = { horz = -23, vert = -94 };
bool NotifyClicked(Button btnAdd, int x, int y, Modifiers mods)
{
const String buf1 = ebFindKey.contents;
// const String buf2 = editBox2.contents;
MapIterator<String, Array<MyCube>> it { map = cubesMap };
Array<MyCube> cubesArray;
// 'true' is for 'create if not found'
if(it.Index(buf1, true))
// cubes already exists
cubesArray = it.data;
else
{
// create a new cube array
cubesArray = { };
// add it in place to the map
it.data = cubesArray;
}
cubesArray.Add(MyCube { extraStuff = GetRandom(1, 100) });
lbMessage.caption = $"Added a cube.";
return true;
}
};
EditBox ebFindKey { this, caption = $"Cube Key", size = { 80, 20 }, position = { 8, 40 } };
Label lbFinKey { this, position = { 10, 20 }, labeledWindow = ebFindKey };
Label lbMessage { this, caption = $"Add or find a key", position = { 24, 88 }} ;
}
CubesMapTestForm testForm { };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment