Last active
December 9, 2022 18:51
-
-
Save assyrianic/41b073eb9cd3bc1ba40f0b199c11fb7e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* arraymap.inc | |
* | |
* Copyright 2020 Nergal the Ashurian aka Nergal. | |
* | |
* This program is free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation; either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program; if not, write to the Free Software | |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
* MA 02110-1301, USA. | |
*/ | |
#if defined _arraymap_included | |
#endinput | |
#endif | |
#define _arraymap_included | |
#include <adt_array> | |
#include <adt_trie> | |
/// Port of my C ordmap but for strictly enum struct objects. | |
enum struct ArrayMap { | |
ArrayList list; /// []Obj | |
StringMap map; /// map[string]int - stores indexes for arraylist. | |
//StringMap strIDs; /// map[int, len]string - stores strings for index into arraylist. | |
bool Insert(const char[] key, any[] obj, int size) { | |
int index = this.list.PushArray(obj, size); | |
return this.map.SetValue(key, index); | |
} | |
bool GetByIndex(int index, any[] obj, int size) { | |
int len = this.Len(); | |
return ( index >= len || index < 0 )? false : this.list.GetArray(index, obj, size)==size; | |
} | |
bool GetByName(const char[] key, any[] obj, int size) { | |
int index = -1; | |
return( this.map.GetValue(key, index) )? this.GetByIndex(index, obj, size) : false; | |
} | |
bool SetByName(const char[] key, any[] obj, int size) { | |
int index = -1; | |
return( this.map.GetValue(key, index) )? this.SetByIndex(index, obj, size) : false; | |
} | |
bool SetByIndex(int index, any[] obj, int size) { | |
if( index < 0 || index >= this.list.Length ) { | |
return false; | |
} | |
this.list.SetArray(index, obj, size); | |
return true; | |
} | |
bool DelByName(const char[] key) { | |
int index = -1; | |
if( this.map.GetValue(key, index) && index < this.Len() ) { | |
this.map.Remove(key); | |
this.list.Erase(index); | |
return true; | |
} | |
return false; | |
} | |
/// TODO: look up string from index for faster deletion-by-index. | |
bool DelByIndex(int index) { | |
bool result = false; | |
if( index < 0 ) { | |
return result; | |
} | |
StringMapSnapshot snap = this.map.Snapshot(); | |
if( snap==null ) { | |
return result; | |
} | |
int len = snap.Length; | |
for( int i; i < len; i++ ) { | |
int keysize = snap.KeyBufferSize(i); | |
char[] buf = new char[keysize]; | |
int id_delete = -1; | |
if( snap.GetKey(i, buf, keysize) > 0 && this.map.GetValue(buf, id_delete) && id_delete==index ) { | |
result = this.DelByName(buf); | |
break; | |
} | |
} | |
delete snap; | |
return result; | |
} | |
void Clear() { | |
this.map.Clear(); | |
this.list.Clear(); | |
} | |
int Len() { | |
return this.list.Length; | |
} | |
void Destroy() { | |
delete this.map; | |
delete this.list; | |
} | |
} | |
stock any[] MakeArrayMap(int size) { | |
ArrayMap lm; | |
lm.list = new ArrayList(size); | |
lm.map = new StringMap(); | |
return lm; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment