Created
April 29, 2024 09:54
-
-
Save joningold/d7d5446bb37e7d39439146b6015467b9 to your computer and use it in GitHub Desktop.
Array structure for ink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LIST Fruit = Apple, Banana, Orange, Lemon | |
VAR fruitSaladIndex = 0 | |
- (top) | |
* [add apple ] | |
~ addToSalad(Apple) | |
* [ add banana ] | |
~ addToSalad(Banana) | |
* [ add orange ] | |
~ addToSalad(Orange) | |
* [ add lemon ] | |
~ addToSalad(Lemon) | |
+ [ What have I got? ] | |
1: {retrieveFruitSaladItem(1)} | |
2: {retrieveFruitSaladItem(2)} | |
3: {retrieveFruitSaladItem(3)} | |
4: {retrieveFruitSaladItem(4)} | |
- -> top | |
=== function addToSalad(fruit) | |
~ fruitSaladIndex++ | |
~ _setValueOfState(fruit, fruitSaladIndex) | |
=== function retrieveFruitSaladItem(itemNum) | |
~ return getItemFromListAtIndex(LIST_ALL(Fruit), itemNum) | |
=== function getItemFromListAtIndex(allFruits, itemNum ) | |
~ temp fr = pop(allFruits) | |
{ fr: | |
{ _getValueOfState(fr) == itemNum: | |
~ return fr | |
} | |
~ return getItemFromListAtIndex(allFruits, itemNum) | |
} | |
~ return () | |
// 1) Storage space | |
VAR StatesBinary1 = "" | |
VAR StatesBinary2 = "" | |
VAR StatesBinary4 = "" | |
VAR StatesBinary8 = "" | |
VAR StatesBinary16 = "" | |
VAR StatesBinary32 = "" | |
VAR StatesBinary64 = "" | |
VAR StatesBinary128 = "" | |
VAR StatesBinary256 = "" | |
VAR StatesBinary512 = "" | |
VAR StatesBinary1024 = "" | |
VAR StatesBinary2048 = "" // storage up to 4095, well over the 24 hour clock! | |
// --> ADDITIONAL STORAGE GOES HERE | |
CONST MAX_BINARY_BIT = 2048 | |
VAR StatesInStorage = () | |
// 2) Get value for state being set | |
=== function _getValueOfState(state) // always single | |
// do this the dumb long way rather than a fancy loop | |
~ temp value = 0 | |
~ temp id = stateID(state) | |
~ value += returnIfTrue(StatesBinary1 ? id, 1) | |
~ value += returnIfTrue(StatesBinary2 ? id, 2) | |
~ value += returnIfTrue(StatesBinary4 ? id, 4) | |
~ value += returnIfTrue(StatesBinary8 ? id, 8) | |
~ value += returnIfTrue(StatesBinary16 ? id, 16) | |
~ value += returnIfTrue(StatesBinary32 ? id, 32) | |
~ value += returnIfTrue(StatesBinary64 ? id, 64) | |
~ value += returnIfTrue(StatesBinary128 ? id, 128) | |
~ value += returnIfTrue(StatesBinary256 ? id, 256) | |
~ value += returnIfTrue(StatesBinary512 ? id, 512) | |
~ value += returnIfTrue(StatesBinary1024 ? id, 1024) | |
~ value += returnIfTrue(StatesBinary2048 ? id, 2048) | |
// --> ADDITIONAL STORAGE GOES HERE | |
~ return value | |
// 3) Set value for state being set | |
=== function _setValueOfState(state, value) // always single | |
{ value >= 2 * MAX_BINARY_BIT: | |
[ ERROR - trying to store a value of {value}, which is the space provided. Please increase {MAX_BINARY_BIT}, and supply additional storage values. ] | |
} | |
~ temp currentValue = _getValueOfState(state) | |
{ currentValue != 0 && currentValue != value: | |
~ _removeValuesForState(state) | |
} | |
{ value != 0: | |
~ StatesInStorage += state | |
~ _setBinaryValuesForState(stateID(state), value, MAX_BINARY_BIT ) | |
} | |
// [ {value} - set value for {state} to {getValueOfState(state) } ] | |
=== function _setBinaryValuesForState(id, value, binaryValue) | |
{ value >= binaryValue: | |
~ value -= binaryValue | |
{binaryValue: | |
- 1: ~ StatesBinary1 += id | |
- 2: ~ StatesBinary2 += id | |
- 4: ~ StatesBinary4 += id | |
- 8: ~ StatesBinary8 += id | |
- 16: ~ StatesBinary16 += id | |
- 32: ~ StatesBinary32 += id | |
- 64: ~ StatesBinary64 += id | |
- 128: ~ StatesBinary128 += id | |
- 256: ~ StatesBinary256 += id | |
- 512: ~ StatesBinary512 += id | |
- 1024: ~ StatesBinary1024 += id | |
- 2048: ~ StatesBinary2048 += id | |
// --> ADDITIONAL STORAGE GOES HERE | |
} | |
} | |
{ binaryValue > 1: | |
~ _setBinaryValuesForState(id, value, binaryValue / 2) | |
} | |
// 2) Auxiliary functions | |
=== function stateID(x) | |
~ return ":{x};" | |
=== function returnIfTrue(test, val) | |
{test: | |
~ return val | |
- else: | |
~ return 0 | |
} | |
=== function pop(ref _list) | |
~ temp el = LIST_MIN(_list) | |
~ _list -= el | |
~ return el | |
// 3) Removal | |
=== function _removeValuesForState(state) | |
~ StatesInStorage -= state | |
~ StatesBinary1 = _rebuildStateStorage(StatesBinary1, StatesInStorage) | |
~ StatesBinary2 = _rebuildStateStorage(StatesBinary2, StatesInStorage) | |
~ StatesBinary4 = _rebuildStateStorage(StatesBinary4, StatesInStorage) | |
~ StatesBinary8 = _rebuildStateStorage(StatesBinary8, StatesInStorage) | |
~ StatesBinary16 = _rebuildStateStorage(StatesBinary16, StatesInStorage) | |
~ StatesBinary32 = _rebuildStateStorage(StatesBinary32, StatesInStorage) | |
~ StatesBinary64 = _rebuildStateStorage(StatesBinary64, StatesInStorage) | |
~ StatesBinary128 = _rebuildStateStorage(StatesBinary128, StatesInStorage) | |
~ StatesBinary256 = _rebuildStateStorage(StatesBinary256, StatesInStorage) | |
~ StatesBinary512 = _rebuildStateStorage(StatesBinary512, StatesInStorage) | |
~ StatesBinary1024 = _rebuildStateStorage(StatesBinary1024, StatesInStorage) | |
~ StatesBinary2048 = _rebuildStateStorage(StatesBinary2048, StatesInStorage) | |
=== function _rebuildStateStorage(storageVariable, statesToInclude) | |
~ temp state = LIST_MIN(statesToInclude) | |
~ statesToInclude -= state | |
{ state: | |
~ temp returnValue = "{stateID(state)}" | |
{ storageVariable !? returnValue: | |
~ returnValue = "" | |
} | |
~ return returnValue + _rebuildStateStorage(storageVariable, statesToInclude) | |
} | |
~ return "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment