Raised This Month: $32 Target: $400
 8% 

Clone/copy trie


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
KissLick
Veteran Member
Join Date: Nov 2012
Location: void
Old 04-09-2015 , 15:23   Clone/copy trie
Reply With Quote #1

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.
KissLick is offline
Spirit_12
Veteran Member
Join Date: Dec 2012
Location: Toronto, CA
Old 04-09-2015 , 16:10   Re: Clone/copy trie
Reply With Quote #2

Where are you trying to clone it? Website ?
Spirit_12 is offline
KissLick
Veteran Member
Join Date: Nov 2012
Location: void
Old 04-10-2015 , 10:58   Re: Clone/copy trie
Reply With Quote #3

This trie in plugin.
KissLick is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 04-10-2015 , 12:43   Re: Clone/copy trie
Reply With Quote #4

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);
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 04-10-2015 at 12:44.
Powerlord is offline
KissLick
Veteran Member
Join Date: Nov 2012
Location: void
Old 04-10-2015 , 12:54   Re: Clone/copy trie
Reply With Quote #5

Yep... Unfortunately I am using SM 1.6 (might consider to use newer version) :-( Thx Powerlord
KissLick is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 04-10-2015 , 18:48   Re: Clone/copy trie
Reply With Quote #6

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?
Miu is offline
KissLick
Veteran Member
Join Date: Nov 2012
Location: void
Old 04-11-2015 , 06:43   Re: Clone/copy trie
Reply With Quote #7

That would work, but I already found a way, how to work around cloning trie in my algorithm.
KissLick is offline
Drixevel
AlliedModders Donor
Join Date: Sep 2009
Location: Somewhere headbangin'
Old 07-31-2016 , 20:21   Re: Clone/copy trie
Reply With Quote #8


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;
}

Last edited by Drixevel; 07-31-2016 at 20:22.
Drixevel is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:54.


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