http://scratch.mit.edu/projects/AddZero/346118#
This is for browsing/adding to data in scratch in a tree. I'll add the delete/copy/paste functions soon.
This has many uses, like your computer's file system.
directories inside of directories...
It's like having lists inside of lists.
---EDIT: Sorry, I wrote WAY to much here. Feel free to skip to the end. The point is: How else can you do this? What's the best way to have lists that you can add/remove from to, and sort though fast? ...without having to search though the whole list?
This could be useful in paces where you want to add and remove lists as needed.
It will also be useful for my 3d modeling/cad system for storing/editing/removing objects. (I started it so I could try this genetic programming method: http://www.stromcode.com/2008/03/01/evo)
So here's the tree data we want to store:
Colors
Red
Yellow
Earth
North-America
USA
California
Oakland
Canada
Europe
Denmark
England
AntarcticaI want to be able to go into 'Europe' and get Denmark, England.
We could put it in two lists like this:
NameList Level ---------------------- Colors 1 Red 2 Yellow 2 Earth 1 North-America 2 USA 3 California 4 Oakland 5 Canada 3 Europe 2 Denmark 3 England 3 Antarctica 2
So lets go down to Earth.
Is it Colors? nope.
is it Red? nope.
is it Yellow? nope.
is it Earth? yes!
then Europe, We would have to go through all of the items on the lists till we get to 'Europe'. Slow.
So I did it this way:
NameList ItemSize ---------------------- Colors 3 Red 0 Yellow 0 Earth 9 North-America 4 USA 2 California 1 Oakland 0 Canada 0 Europe 2 Denmark 0 England 0 Antarctica 0
ItemSize holds how many items are inside the current item.
So now as I look for Earth starting from the top,
I see Colors has 3 Items in it so I skip 3 down to Earth,
That's what I want so I add that to my CurrentPathName and save the index of that item(4) to the CurrentPathIndex list.
I continue down and see North-America has 4 items, so I skip 4 down the list and see 'Europe' I add that to my CurrentPathName and CurrentPathIndex lists, now we can see Denmark and England!
If we want to go back to Earth, we just delete the last item from the CurrentPathName and CurrentPathIndex lists.
To add an item we just insert at the end of the current level (Index+currentlevel size), then +1 to the ItemSize of each of the parents in CurrentPathIndex
To delete a tree we just the delete [ItemSize] items from the list. (We don't have to search down each item to find the end of the tree.) then -1 to the ItemSize of each of the parents in CurrentPathIndex
I'll write more and clarify things if there's interest.
Do you know a faster/better way to do this?
---EDIT: How would you solve this problem: so you can store/remove and find information fast from lists with the least number of steps?
Last edited by AddZero (2008-12-12 11:35:22)
Offline
I would make two lists:
one for the values, one for the path.
So, to use XML as an example.
If we have the file:
<xml>
<level1>
<level2>level2 info1</level2>
<level2>level2 info2</level2>
<level2>level2 info3</level2>
<level2>
<level3>level3 info</level3>
</level2>
</level1>
</xml>Now, we take this and extract the data:
1) level2 info1
2) level2 info2
3) level2 info3
4) level3 info
And extract the corresponding pathnames, to Xpath:
1) xml/level1/level2[1]/
2) xml/level1/level2[2]/
3) xml/level1/level2[3]/
4) xml/level1/level2[4]/level3/
From this data, we can build the tree:
1) read path 1.
2) create xml node.
3) create level1 node.
4) create level2 node.
5) note that there are more level 2's in this root.
6) add data for path 1.
7) read path 2.
8) note that xml node is already there, and there aren't any copies.
9) note that level1 node is already there, and there aren't any copies.
10) remember that there are copies of level2.
11) read that this is the second level2 in this level1.
12) add data for path 2.
13) continue on similarly.
As you can see, this is just a functional outline... but it should work.
Offline
billyedward wrote:
I would make two lists:
one for the values, one for the path.
So, to use XML as an example.
If we have the file:Code:
<xml> <level1> <level2>level2 info1</level2> <level2>level2 info2</level2> <level2>level2 info3</level2> <level2> <level3>level3 info</level3> </level2> </level1> </xml>Now, we take this and extract the data:
1) level2 info1
2) level2 info2
3) level2 info3
4) level3 info
And extract the corresponding pathnames, to Xpath:
1) xml/level1/level2[1]/
2) xml/level1/level2[2]/
3) xml/level1/level2[3]/
4) xml/level1/level2[4]/level3/
From this data, we can build the tree:
1) read path 1.
2) create xml node.
3) create level1 node.
4) create level2 node.
5) note that there are more level 2's in this root.
6) add data for path 1.
7) read path 2.
8) note that xml node is already there, and there aren't any copies.
9) note that level1 node is already there, and there aren't any copies.
10) remember that there are copies of level2.
11) read that this is the second level2 in this level1.
12) add data for path 2.
13) continue on similarly.
As you can see, this is just a functional outline... but it should work.
O_o XML! BTW you have to put this in an XML file first
<? xml version = "1.0" encoding = "utf-8" ?>
Offline
Greatdane wrote:
ThePCKid wrote:
O_o XML! BTW you have to put this in an XML file first
He's giving an example.
I'm not stupid!
Offline
cool, thanks for the the thoughtful responses, just noticed, sorry.
I'm working on a new version now.
from the project:
"instead of two lists, just one, each 'file' has a header, starting with how long (how many lines till the next file at that level) that file is, the name, the type. (1 for folder) the creator, the color of the icon. so the header is 5 lines long, if it is a folder type, the user can navigate into that.(adds that folder to the path list) then lists everything inside that folder. I'm still working on that one, should be out soon. "
This will be a 'file manager menu' type front end for projects that store information.
like my lathe 3d modeler and music program. (I'm working on)
I'll try to explain how it works better when I release it.
Offline
AddZero wrote:
cool, thanks for the the thoughtful responses, just noticed, sorry.
I'm working on a new version now.
from the project:
"instead of two lists, just one, each 'file' has a header, starting with how long (how many lines till the next file at that level) that file is, the name, the type. (1 for folder) the creator, the color of the icon. so the header is 5 lines long, if it is a folder type, the user can navigate into that.(adds that folder to the path list) then lists everything inside that folder. I'm still working on that one, should be out soon. "
This will be a 'file manager menu' type front end for projects that store information.
like my lathe 3d modeler and music program. (I'm working on)
I'll try to explain how it works better when I release it.
Nice. Something I've been thinking about for a long time is designing a text-based code- a bit like HTML or C++ within Scratch. You could then implement a file browser into an OS design to make a fully-working OS... You could even make an installer, to install other projects onto your system!
Offline
Yeah, if you made a interpreter-based code and a program to interpret it, you could just copy the code of someone else's app into your file system and the "OS" would be able to run it.
That'd be pretty hard in Scratch, though.
Offline