11-04-2008 14:12
Hi all,

I guess some of you know what a collection is from other programming languages. Although the concept of collections does not exist in the LSL programming language until now!

I would like to introduce the "VeryPav Collection Concept". This is an extension to "Very Keynes" VK-DBMS-VM work last was released last week. If you go to the documentation link below and brief description of what a collection is given, plus the documentation of all the functions. This page also has a license agreement, which follows the DBMS license as well.

It would be interesting to see what you make of this code. It also shows how beneficial the DBMS is, because several other concepts can be produced from this. There is one more concept to release, but it currently under development.

Documentation of what a collection is, and its associate functions:
http://docs.google.com/Doc?docid=ddd96bqf_07v2jk87v

To see the collection code (also posted below):
http://docs.google.com/Doc?docid=ddd96bqf_2c7g7wkg3

To see an example of the code:
http://docs.google.com/Doc?docid=ddd96bqf_1ffdjk6fc

For more info about the DBMS system, see this topic:
/54/4a/290413/1.html

Have fun with collections. :)

--------------------- The Collection Code ------------------------

CODE

// VeryPav Collection
// Issue Date: 2008-11-04
// VK-DBMS-VM : Created by "Very Keynes"
// Collections : Created by "Pavcules Superior"


//Very Keynes - 2008
//
// 2008-10-09, 22:24GMT
//
//------------------Begin VK-DBMS-VM----------------------------\\
integer th_;integer tc_;integer tr_;integer dbIndex;integer ts_;
list _d_ = [];list _i_ = [0];list dbRow;string dbError;
integer t_i;string t_s;float t_f;list t_l;
string dbCreate(string tab, list col)
{ if(dbOpen(tab)){dbError = tab + " already exists"; return "";}
_i_ += [tab, tc_ = llGetListLength(col), 0, 0, 0];
th_= 0; dbOpen(tab); dbInsert(col); return tab; }
integer dbCol(string col){return llListFindList(dbGet(0), [_trm(col)]);}
integer dbDelete(integer ptr){if(ptr > 0 && ptr < tr_)
{_d_ = llDeleteSubList(_d_, t_i = ts_ + tc_ * ptr, t_i + tc_ - 1); --tr_;return tr_ - 1;}
else {dbError = (string)ptr+" is outside the Table Bounds"; return FALSE;}}
integer dbDrop(string tab)
{
if(-1 != (t_i = llListFindList(_i_, [tab])))
{ dbOpen(tab);
_d_ = llDeleteSubList(_d_, ts_, ts_ + tc_ * tr_ - 1);
_i_ = llDeleteSubList(_i_, th_, th_ + 4);th_= 0;
return TRUE; }
else {dbError = tab + " : Table name not recognised"; return FALSE;}
}
integer dbExists(list cnd)
{ for(dbIndex = tr_ ; --dbIndex > 0 ; ){if(dbTest(cnd))return dbIndex;}
return FALSE;}
list dbGet(integer ptr){
if(ptr < tr_ && ptr >= 0)return llList2List(_d_, t_i = ts_ + tc_ * ptr, t_i + tc_ - 1);
else dbError = (string)ptr+" is outside the Table Bounds";return [];}
integer _idx(integer hdl){return (integer)llListStatistics(6, llList2ListStrided(_i_, 0, hdl, 5));}
integer dbInsert(list val)
{ if(llGetListLength(val) == tc_){_d_ = llListInsertList(_d_, val, ts_ + tc_ * (dbIndex = tr_++));return dbIndex;}
else{dbError = "Insert Failed - too many or too few Columns specified"; return FALSE;}}
integer dbOpen(string tab)
{if(th_)_i_ = llListReplaceList(_i_, [tr_, dbIndex, tc_ * tr_], th_ + 2, th_ + 4);
if(-1 == (t_i = llListFindList(_i_, [tab]))) //if tab does not exist abort
{dbError = tab + " : Table name not recognised"; return FALSE;}
else if(th_ != t_i){th_ = t_i++;ts_ = _idx(th_);
tc_ = llList2Integer(_i_, t_i++);
tr_ = llList2Integer(_i_, t_i++);
dbIndex = llList2Integer(_i_, t_i);}
;return tr_ - 1;}
integer dbPut(list val){if(llGetListLength(val) == tc_)
{_d_ = llListReplaceList(_d_, val, t_i = ts_ + tc_ * dbIndex, t_i + tc_ - 1); return dbIndex;}
else {dbError = "Update Failed - too many or too few Columns specified"; return FALSE;}}
integer dbTest(list cnd)
{ if(llGetListEntryType(cnd,2) >= 3){
t_s = llList2String(dbGet(dbIndex), dbCol(llList2String(cnd, 0)));
if ("==" == llList2String(cnd, 1)){t_i = t_s == _trm(llList2String(cnd, 2));}
else if("!=" == llList2String(cnd, 1)){t_i = t_s != _trm(llList2String(cnd, 2));}
else if("~=" == llList2String(cnd, 1))
{t_i = !(llSubStringIndex(llToLower(t_s), llToLower(_trm(llList2String(cnd, 2)))));}}
else{
t_f = llList2Float(dbGet(dbIndex), dbCol(llList2String(cnd, 0)));
t_s = llList2String(cnd, 1);
if ("==" == t_s){t_i = t_f == llList2Float(cnd, 2);}
else if("!=" == t_s){t_i = t_f != llList2Float(cnd, 2);}
else if("<=" == t_s){t_i = t_f <= llList2Float(cnd, 2);}
else if(">=" == t_s){t_i = t_f >= llList2Float(cnd, 2);}
else if("<" == t_s){t_i = t_f < llList2Float(cnd, 2);}
else if(">" == t_s){t_i = t_f > llList2Float(cnd, 2);}}
if(t_i)return dbIndex; else return FALSE;}
string _trm(string val){return llStringTrim(val, STRING_TRIM);}
dbTruncate(string tab){t_i = dbOpen(tab);while(dbIndex > 0)dbDelete(dbIndex--);}
dbSort(integer dir){_d_ = llListReplaceList(_d_,llListSort(llList2List
(_d_, t_i = ts_ + tc_, t_i + tc_ * tr_ - 2), tc_, dir), t_i, t_i + tc_ * tr_ - 2);
}
float dbFn(string fn, string col)
{
t_l = llList2List(_d_, t_i = ts_ + tc_, t_i + tc_ * tr_ - 2);
if(dbCol(col) != 0) t_l = llDeleteSubList(t_l, 0, dbCol(col) - 1);
return llListStatistics(llSubStringIndex("ramimaavmedesusqcoge", llGetSubString(llToLower(fn),0,1)) / 2,
llList2ListStrided(t_l, 0, -1, tc_));
}
//
//--------------------------- End VK-DBMS-VM ---------------------------\\
//








// (EXPERRIMENTAL) New db Function to insert at an item in the list.
// Function created by Pavcules Superior, not part of the VK-ISAM-VM functions.
integer dbInsertAt(integer ptr, list p_lstList)
{
if(ptr > 0 && ptr < tr_)
{
_d_ = llListInsertList(_d_, p_lstList, t_i = ts_ + tc_ * ptr);
tr_++;
return TRUE;
}
else
{
dbError = (string)ptr+" is outside the Table Bounds"; return FALSE;
}
}



//------------------Begin VeryPav Collection ----------------------------\\


integer collectionLoopIndex;
list collectionLoopItem;

// Creates a new collection.
integer collectionNew(list p_lstName, list p_lstItems)
{
string strName = llList2String(p_lstName, 0);

// Check if the collection exists, if it does not, then create one.
if(!dbOpen(strName))
{

if(llGetListLength(p_lstItems) == 0)
{
dbCreate(strName, ["_colkey_", "_colitem_"]);
}
else
{
dbCreate(strName, ["_colkey_"] + p_lstItems);
}

return TRUE;
}

// Collection not created, as one already exists.
return FALSE;
}

// Opens a collection.
integer collectionOpen(list p_lstName)
{
return dbOpen(llList2String(p_lstName, 0));
}

// Deletes a collection.
integer collectionDelete(list p_lstName)
{
return dbDrop(llList2String(p_lstName, 0));
}

// Adds an item to the collecion.
integer collectionItemAdd(list p_lstKeyName, list p_lstItem)
{
string strKey = llList2String(p_lstKeyName, 0);
integer intPos = llList2Integer(p_lstKeyName, 1);

if(!collectionItemExists([strKey]))
{
if(intPos == 0)
{
// Add at the end of the collection.
dbInsert([strKey] + p_lstItem);
}
else
{


if(intPos > collectionItemCount())
{
// Add at the bottom of the list if the position is greater than the total items.
dbInsert([strKey] + p_lstItem);
}
else
{
// Add at a specific position.
dbInsertAt(intPos,[strKey] + p_lstItem);
}
}

return TRUE;
}

// Item exists in the collection, so fail.
return FALSE;
}

// Update an item in the collection.
collectionItemUpdate(list p_lstIndexOrKey, list p_lstItem)
{
if(llGetListEntryType(p_lstIndexOrKey, 0) == TYPE_INTEGER)
{
// Index number.
dbIndex = llList2Integer(p_lstIndexOrKey,0);
dbRow = dbGet(dbIndex);
dbPut( [llList2String(dbRow, 0)] + p_lstItem);

}
else if(llGetListEntryType(p_lstIndexOrKey, 0) == TYPE_STRING)
{
// Key name.
if(dbExists(["_colkey_", "==", llList2String(p_lstIndexOrKey, 0) ]))
{
dbRow = dbGet(dbIndex);
dbPut([llList2String(dbRow, 0)] + p_lstItem);
}
}
}


// Deletes an item from the collection.
collectionItemDelete(list p_lstIndexOrKey)
{
if(llGetListEntryType(p_lstIndexOrKey, 0) == TYPE_INTEGER)
{
// Index number.
dbDelete(llList2Integer(p_lstIndexOrKey, 0));
}
else if(llGetListEntryType(p_lstIndexOrKey, 0) == TYPE_STRING)
{
// Key name.
if(dbExists(["_colkey_", "==", llList2String(p_lstIndexOrKey, 0) ]))
{
dbDelete(dbIndex);
}
}
}

// Checks if the item exists in the collection.
integer collectionItemExists(list p_lstIndexOrKey)
{
if(llGetListEntryType(p_lstIndexOrKey, 0) == TYPE_INTEGER)
{
// Index number.
return (llList2Integer(p_lstIndexOrKey, 0) <= collectionItemCount());
}
else if(llGetListEntryType(p_lstIndexOrKey, 0) == TYPE_STRING)
{
// Key name.
return (dbExists(["_colkey_", "==", llList2String(p_lstIndexOrKey, 0) ]) > 0);
}

return FALSE;
}

// Gets the number of items in the collection.
integer collectionItemCount()
{
return tr_ - 1;
}

// Get the collection item.
list collectionItem(list p_lstIndexOrKey)
{
list lstItem;
dbRow = [];

if(llGetListEntryType(p_lstIndexOrKey, 0) == TYPE_INTEGER)
{
// Index being retrieved.
dbRow = dbGet(llList2Integer(p_lstIndexOrKey, 0));
}
else if(llGetListEntryType(p_lstIndexOrKey, 0) == TYPE_STRING)
{
// Key being retrieved.
if(dbExists(["_colkey_", "==", llList2String(p_lstIndexOrKey, 0) ]))
{
dbRow = dbGet(dbIndex);
}
}

return dbRow;
}

// Gets the item.
list collectionItemValue(list p_lstIndexOrKey)
{
return llList2List(collectionItem(p_lstIndexOrKey), 1, -1);
}

// Gets the collection item key.
string collectionItemKey(list p_lstIndex)
{
return llList2String(collectionItem(p_lstIndex), 0);
}

// Returns the collection item as a string value.
string collectionItemValueString(list p_lstIndexOrKey)
{
return llList2String(collectionItem(p_lstIndexOrKey), 1);
}

// Returns the collection item as an integer value.
integer collectionItemValueInteger(list p_lstIndexOrKey)
{
return llList2Integer(collectionItem(p_lstIndexOrKey), 1);
}

// Returns the collection item as a float value.
float collectionItemValueFloat(list p_lstIndexOrKey)
{
return llList2Float(collectionItem(p_lstIndexOrKey), 1);
}

// Returns the collection item as a vector value.
vector collectionItemValueVector(list p_lstIndexOrKey)
{
return llList2Vector(collectionItem(p_lstIndexOrKey), 1);
}


// Read the next item from the collection.
integer collectionLoopRead()
{
if(collectionLoopIndex < collectionItemCount())
{
collectionLoopIndex++;
collectionLoopItem = collectionItem([collectionLoopIndex]);
return TRUE;
}

collectionLoopIndex = 0;
collectionLoopItem = [];
return FALSE;
}


//------------------End VeryPav Collection ---------------------------\\