Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Array Deep Copy, TreeView viewer, and more!


  • Please log in to reply
12 replies to this topic
GeekDude
  • Spam Officer
  • 391 posts
  • Last active: Oct 05 2015 08:13 PM
  • Joined: 23 Nov 2009

Originally just one function, now a library of 4!

 

 

Functions included:

 

Array_Print(Array)

  • Returns a textual representation of an array
  • Won't print circular arrays (returns error)

 

Array_Gui(Array)

  • Creates a GUI with a treeview representation of an array
  • Remembers and goes back to the default GUI
  • Resizeable (Can I get a spell check in here?)
  • Won't print circular arrays (returns error)

 

Array_IsCircle(Array)

  • Returns a boolean value according to whether or not the array contains itself

 

Array_DeepClone(Array)

  • Returns a copy of the input array, with all sub arrays cloned as well
  • Will create a circular reference where there was one in the input array

 

 

Example:

Spoiler

 

Code:

Spoiler


GeekDude
  • Spam Officer
  • 391 posts
  • Last active: Oct 05 2015 08:13 PM
  • Joined: 23 Nov 2009
Update:
- Fixed re-size
- Made top level expanded by default
- Uses three global variables (Could possibly be reduced to two, maybe one, by using an array)
- Some other stuff too

GeekDude
  • Spam Officer
  • 391 posts
  • Last active: Oct 05 2015 08:13 PM
  • Joined: 23 Nov 2009
I was changing some code to make it so that you can edit the treeview, and about 3/4 (I think) of the way in to writing the treeview to array converter, I thought "I can't see how this would be useful, and it is very difficult to create". If anyone wants this feature, please say so. I will proceed to either continue working on it, or paste what I have (that is not working) here.

GeekDude
  • Spam Officer
  • 391 posts
  • Last active: Oct 05 2015 08:13 PM
  • Joined: 23 Nov 2009

Major update, with many new features!



raoyc
  • Members
  • 10 posts
  • Last active:
  • Joined: 26 Nov 2012

Thanks for your code



R3gX
  • Members
  • 307 posts
  • Last active: Dec 29 2013 04:50 PM
  • Joined: 28 Feb 2011

Yep, cool code!

 

I really like the 'Array_IsCircle()' code.

Could I use it in one of my scripts if I write you nickname (& post's url) ?


signature.png
Previously known as TomXIII
AutoHotkey version : 1.1.10


GeekDude
  • Spam Officer
  • 391 posts
  • Last active: Oct 05 2015 08:13 PM
  • Joined: 23 Nov 2009

Yep, cool code!

 

I really like the 'Array_IsCircle()' code.

Could I use it in one of my scripts if I write you nickname (& post's url) ?

 

Sure!



R3gX
  • Members
  • 307 posts
  • Last active: Dec 29 2013 04:50 PM
  • Joined: 28 Feb 2011

Thanks!

 

Yesterday I tried to do some modifications in your code to allow users to change the TreeView's values.

Next, I spent all day to code a function to save the treeview into an array. No so easy for me but I succeed (for at least one 'Leaf')!

The problem is that the treeview's array is... circle!

 

Do you have any ideas?!


signature.png
Previously known as TomXIII
AutoHotkey version : 1.1.10


nordanalt
  • Members
  • 51 posts
  • Last active: Mar 23 2013 03:05 PM
  • Joined: 22 Jan 2013

Thanks for these!

I was fiddling around and trying to implement deep cloning myself, and I came up with this:

deepCopy(pArray){
    eArray := Object()
    for ix, pElement in pArray
    {
        if (!isObject(pElement)){
            eArray.Insert(ix, pElement)
        } else {
            eArray.Insert(ix, deepCopy(pElement))
        }
    }
    return eArray
}

Using this function I get the same results for the examples in your code as with your function, but I'm not sure if my function works in every case. Do you think I'm missing something? Cheers.



GeekDude
  • Spam Officer
  • 391 posts
  • Last active: Oct 05 2015 08:13 PM
  • Joined: 23 Nov 2009

Thanks for these!

I was fiddling around and trying to implement deep cloning myself, and I came up with this:

-Snip-

 

Using this function I get the same results for the examples in your code as with your function, but I'm not sure if my function works in every case. Do you think I'm missing something? Cheers.

 

That's the approach I originally went with, but Uberi seems to think that approach is bad (Didn't get an answer out of him why). I notice that you are using a Not (!) in your if statement. I would suggest that you remove the not, and switch around the code blocks. You'll save cpu cycles that way. Also, many of your braces aren't necessary, but you probably already knew that.



Uberi
  • Moderators
  • 1119 posts
  • Last active: May 02 2015 06:05 PM
  • Joined: 23 Aug 2010

That's the approach I originally went with, but Uberi seems to think that approach is bad (Didn't get an answer out of him why). I notice that you are using a Not (!) in your if statement. I would suggest that you remove the not, and switch around the code blocks. You'll save cpu cycles that way. Also, many of your braces aren't necessary, but you probably already knew that.


I believe it was something about object keys, but to be honest, I don't remember anymore. Useful function, in any case.

Oh, and there was the issue of using Array.Insert with integer keys shifting values upwards, and how the object could possibly override the Insert method. But those are easily fixable, of course.

nordanalt
  • Members
  • 51 posts
  • Last active: Mar 23 2013 03:05 PM
  • Joined: 22 Jan 2013

That's the approach I originally went with, but Uberi seems to think that approach is bad (Didn't get an answer out of him why). I notice that you are using a Not (!) in your if statement. I would suggest that you remove the not, and switch around the code blocks. You'll save cpu cycles that way. Also, many of your braces aren't necessary, but you probably already knew that.

Thanks for the suggestions, yeah, I like to put in the braces because more often than not I need to add some more code to the blocks and then I get annoyed if the braces aren't there already.
What I've found out so far for sure is that my version doesn't handle circular references, it gets stuck in an infinite loop. If I find out anything else, I will post it.

Oh, and there was the issue of using Array.Insert with integer keys shifting values upwards

Can you elaborate on this, please?



Uberi
  • Moderators
  • 1119 posts
  • Last active: May 02 2015 06:05 PM
  • Joined: 23 Aug 2010

Thanks for the suggestions, yeah, I like to put in the braces because more often than not I need to add some more code to the blocks and then I get annoyed if the braces aren't there already.
What I've found out so far for sure is that my version doesn't handle circular references, it gets stuck in an infinite loop. If I find out anything else, I will post it.

Can you elaborate on this, please?


Yep. If you look at the ObjInsert documentation, you'll notice that it can be used in two ways with the same syntax: with an integer key or with an object, real, or string key. x.Insert(Key, Value) behaves differently in these two cases. If Key is an integer, all existing keys higher than it will be shifted upwards by one. This behaviour cannot be disabled. Otherwise, the key is inserted normally. I generally prefer to use x[Key] := Value, but this does not work if the object implements the __Set method.