mirror of
https://onedev.site.tesses.net/crosslang
synced 2026-02-09 01:25:45 +00:00
Forgot to add files
This commit is contained in:
114
src/types/associativearray.cpp
Normal file
114
src/types/associativearray.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
#include "CrossLang.hpp"
|
||||
|
||||
namespace Tesses::CrossLang
|
||||
{
|
||||
|
||||
TAssociativeArray* TAssociativeArray::Create(GCList& ls)
|
||||
{
|
||||
TAssociativeArray* list=new TAssociativeArray();
|
||||
GC* _gc = ls.GetGC();
|
||||
ls.Add(list);
|
||||
_gc->Watch(list);
|
||||
return list;
|
||||
}
|
||||
TAssociativeArray* TAssociativeArray::Create(GCList* ls)
|
||||
{
|
||||
TAssociativeArray* list=new TAssociativeArray();
|
||||
GC* _gc = ls->GetGC();
|
||||
ls->Add(list);
|
||||
_gc->Watch(list);
|
||||
return list;
|
||||
}
|
||||
void TAssociativeArray::Set(GC* gc, TObject key, TObject value)
|
||||
{
|
||||
if(std::holds_alternative<Undefined>(key)) return;
|
||||
gc->BarrierBegin();
|
||||
for(auto index = this->items.begin(); index < this->items.end(); index++)
|
||||
{
|
||||
auto first= index->first;
|
||||
gc->BarrierEnd();
|
||||
auto eq = Equals(gc,key,first);
|
||||
gc->BarrierBegin();
|
||||
if(eq)
|
||||
{
|
||||
if(std::holds_alternative<Undefined>(value))
|
||||
{
|
||||
this->items.erase(index);
|
||||
}
|
||||
else
|
||||
{
|
||||
index->second = value;
|
||||
}
|
||||
gc->BarrierEnd();
|
||||
return;
|
||||
}
|
||||
}
|
||||
this->items.push_back(std::pair<TObject,TObject>(key,value));
|
||||
gc->BarrierEnd();
|
||||
}
|
||||
TObject TAssociativeArray::Get(GC* gc, TObject key)
|
||||
{
|
||||
if(std::holds_alternative<Undefined>(key)) return Undefined();
|
||||
gc->BarrierBegin();
|
||||
for(auto& item : this->items)
|
||||
{
|
||||
auto first= item.first;
|
||||
gc->BarrierEnd();
|
||||
auto eq = Equals(gc,key,first);
|
||||
gc->BarrierBegin();
|
||||
if(eq)
|
||||
{
|
||||
gc->BarrierEnd();
|
||||
return item.second;
|
||||
}
|
||||
}
|
||||
gc->BarrierEnd();
|
||||
return Undefined();
|
||||
}
|
||||
TObject TAssociativeArray::GetKey(int64_t index)
|
||||
{
|
||||
if(index >= 0 && index < (int64_t)this->items.size())
|
||||
{
|
||||
return this->items[index].first;
|
||||
}
|
||||
return Undefined();
|
||||
}
|
||||
TObject TAssociativeArray::GetValue(int64_t index)
|
||||
{
|
||||
if(index >= 0 && index < (int64_t)this->items.size())
|
||||
{
|
||||
return this->items[index].second;
|
||||
}
|
||||
return Undefined();
|
||||
}
|
||||
void TAssociativeArray::SetKey(int64_t index, TObject key)
|
||||
{
|
||||
|
||||
if(std::holds_alternative<Undefined>(key)) return;
|
||||
if(index >= 0 && index < (int64_t)this->items.size())
|
||||
{
|
||||
this->items[index].first=key;
|
||||
}
|
||||
|
||||
}
|
||||
void TAssociativeArray::SetValue(int64_t index,TObject value)
|
||||
{
|
||||
if(std::holds_alternative<Undefined>(value)) return;
|
||||
if(index >= 0 && index < (int64_t)this->items.size())
|
||||
{
|
||||
this->items[index].first=value;
|
||||
}
|
||||
}
|
||||
int64_t TAssociativeArray::Count()
|
||||
{
|
||||
return (int64_t)this->items.size();
|
||||
}
|
||||
void TAssociativeArray::Mark()
|
||||
{
|
||||
for(auto& item : this->items)
|
||||
{
|
||||
GC::Mark(item.first);
|
||||
GC::Mark(item.second);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user