AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Clone/copy trie (https://forums.alliedmods.net/showthread.php?t=261300)

KissLick 04-09-2015 15:23

Clone/copy trie
 
Ciao guys,

I'd like to ask you if you know about any way, how to clone/copy trie structure.
I have a adt trie which I need to copy (like create a new branch) and modify both the original and copied tries separately.

Spirit_12 04-09-2015 16:10

Re: Clone/copy trie
 
Where are you trying to clone it? Website ?

KissLick 04-10-2015 10:58

Re: Clone/copy trie
 
This trie in plugin.

Powerlord 04-10-2015 12:43

Re: Clone/copy trie
 
I don't think its possible in SourceMod 1.6.

In 1.7 it IS possible. This example assumes that all of the tries value types are the same. It also does deep copies of strings and arrays (if the arrays are simple values) if those are the type of value in the trie.

PHP Code:

enum TrieValueType
{
    
TrieValue_Value,
    
TrieValue_String,
    
TrieValue_Array,
}

stock Handle:CloneTrie(Handle:oldTrieTrieValueType:valueType=TrieValue_ValuevalueLength=0)
{
    new 
Handle:newTrie CreateTrie();
    
    new 
Handle:snapshot CreateTrieSnapshot(oldTrie);
    
    new 
itemCount TrieSnapshotLength(snapshot);
    
    for (new 
0itemCounti++)
    {
        new 
length TrieSnapshotKeyBufferSize(snapshoti);
        
        new 
String:key[length];
        
GetTrieSnapshotKey(snapshotikeylength);
        switch(
valueType)
        {
            case 
TrieValue_Value:
            {
                new 
value;
                if (
GetTrieValue(oldTriekeyvalue))
                {
                    
SetTrieValue(newTriekeyvalue);
                }
            }
            
            case 
TrieValue_String:
            {
                new 
String:value[valueLength];
                if (
GetTrieString(oldTriekeyvaluevalueLength))
                {
                    new 
String:newValue[valueLength];
                    
strcopy(newValuevalueLengthvalue);
                    
                    
SetTrieString(newTriekeynewValue);
                }
            }
            
            case 
TrieValue_Array:
            {
                new 
valueArray[valueLength];
                if (
GetTrieArray(oldTriekeyvalueArrayvalueLength))
                {
                    new 
newValueArray[valueLength];
                    for (new 
0valueLengthj++)
                    {
                        
newValueArray[j] = valueArray[j];
                    }
                    
SetTrieArray(newTriekeynewValueArrayvalueLength);
                }
            }
        }
        
    }
    
    return 
newTrie;


Usage is:

Code:

// For a trie of Values
new Handle:newTrie = CloneTrie(oldTrie);

// For a trie of Strings, where the last arg is the string length
new Handle:newTrie = CloneTrie(oldTrie, TrieValue_String, 32);

// For a trie of Arrays, where the last arg is the array length
new Handle:newTrie = CloneTrie(oldTrie, TrieValue_Array, 32);


KissLick 04-10-2015 12:54

Re: Clone/copy trie
 
Yep... Unfortunately I am using SM 1.6 (might consider to use newer version) :-( Thx Powerlord

Miu 04-10-2015 18:48

Re: Clone/copy trie
 
couldn't you just store the keys (or w/e information it is you're missing in 1.6) in a separate array when you set stuff in the trie?

KissLick 04-11-2015 06:43

Re: Clone/copy trie
 
That would work, but I already found a way, how to work around cloning trie in my algorithm.

Drixevel 07-31-2016 20:21

Re: Clone/copy trie
 
http://vignette3.wikia.nocookie.net/...20150801173505
Code:

enum TrieValueType
{
        TrieValue_Value,
        TrieValue_String,
        TrieValue_Array,
}

stock Handle CloneTrie(Handle oldTrie, TrieValueType valueType = TrieValue_Value, int valueLength = 0)
{
        Handle newTrie = CreateTrie();
        Handle snapshot = CreateTrieSnapshot(oldTrie);
       
        int itemCount = TrieSnapshotLength(snapshot);
       
        for (int i = 0; i < itemCount; i++)
        {
                int length = TrieSnapshotKeyBufferSize(snapshot, i);
               
                char[] key = new char[length];
                GetTrieSnapshotKey(snapshot, i, key, length);
                switch(valueType)
                {
                case TrieValue_Value:
                        {
                                int value;
                                if (GetTrieValue(oldTrie, key, value))
                                {
                                        SetTrieValue(newTrie, key, value);
                                }
                        }
                       
                case TrieValue_String:
                        {
                                char[] value = new char[valueLength];
                                if (GetTrieString(oldTrie, key, value, valueLength))
                                {
                                        char[] newValue = new char[valueLength];
                                        strcopy(newValue, valueLength, value);
                                       
                                        SetTrieString(newTrie, key, newValue);
                                }
                        }
                       
                case TrieValue_Array:
                        {
                                int[] valueArray = new int[valueLength];
                                if (GetTrieArray(oldTrie, key, valueArray, valueLength))
                                {
                                        int newValueArray[valueLength];
                                        for (int j = 0; j < valueLength; j++)
                                        {
                                                newValueArray[j] = valueArray[j];
                                        }
                                        SetTrieArray(newTrie, key, newValueArray, valueLength);
                                }
                        }
                }
               
        }
       
        return newTrie;
}



All times are GMT -4. The time now is 15:12.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.