[Closed] C++ style map in maxscript
Is it posible in maxscript to have something like map in C++? I would really like to do something like this in maxscript:
map[“name1”]=1
and if I do later something like this:
x=map[“name1”]
x should have the value 1.
This is just a simple example.
Max 17 seems to have Dictionary that should ofer that functionality, but I need something, that worcks with much older versions of max.
You can create your own ‘dictionary struct’.
See DenisT solution in this old thread: http://forums.cgsociety.org/showthread.php?f=98&t=1142705&highlight=struct+dictionary
Not a better solution, then what I have at the moment. My curent solution is just a 2 dimensional array, where the first value is the name string and the second one contain the coresponding value. The solution has a crapy performance of O(n), as I have to run in a for loop through the array and search for the name string.
If you go on with your method, you will surely get better performance with two unidimensional arrays and using ‘FindItem’ instead of the foor loop.
Nah, can still keep 2 dimensional aray, but just with a diferent structure. I mean something like this: #(#(), #()). Howevwer that ‘FindItem’ could be a problem, not shure if it worcks with older max versions.
I wonder why you ask for help if everything we propose is worse than your solution or could have problems.
Don’t make us lose our time!
Really sorry, never wanted to waste your, or somebody else time, like I said in the very first post need a solution that worcks with older max versions to.
OK, checked maxscript reference and findItem seems to be avaible for all max versions, but the explanaition there says it just makes a ‘==’ comparison.Here the link to the reference.
Yes, it’s available for all max versions. Seems incredible!
What’s the problem with “==” comparison? Which sort of comparison do you need? CaseInsensitive? Just use ‘toLower’ before checking or convert to name (‘as name’) every string.
I really think you still find/show problems to our proposals instead of looking deep into them.
Of course, you are free to use your ‘multiversion – no problems’ solution. I’m sure it works. But… why do you ask for help then?
My own solution is really crappy, so I was wondering if there is a better one, but under condition it worcks with older max versions.
Like I said my english is very bad, so I just wondering how that findItem is faster than my solution if it makes “==” comparison, like I do. You can call me dumb, but I am not understanding the diference, so if you explain it, would be really greatfull.
Perhaps you should put your search algorithm here so we can compare with other methods.
Don’t forget to also put a good test example.
PD: Even if what you have is as you said “crappy”, perhaps your idea is good but the implementation could be improved. It has been proved here more than once, than changing one line of code in MXS can make a huge difference in performance, and there are very clever and talented developers reading this and willing to help.
We all love to learn, and most of the times we learn from mistakes.
you have almost noting… how can you compare?
for your knowledge – dictionary is two linked arrays where the first one hashed and sorted for quick binary search
it’s your solution has a crappy performance…
see yourself…
let’s make a 100000 items of strings (i will make very bad strings for search).
fn randomchar = bit.intaschar (random (bit.charasint "A") (bit.charasint "Z"))
fn randomstr count:16 =
(
str = ""
for k=1 to count do str += randomchar()
str
)
data = for k=1 to 100000 collect (randomstr())
now let’s check a performance:
(
t = timestamp()
h = heapfree
seed = 0
for k=1 to 1000 do
(
finditem data data[random 1 data.count]
)
format "time:% heap:%
" (timestamp() - t) (h - heapfree)
)
-- 1000 items for time:3.355 sec
now sort them and use bsearch
qsort data stricmp
(
t = timestamp()
h = heapfree
seed = 0
for k=1 to 1000 do
(
bsearch data[random 1 data.count] data stricmp
)
format "time:% heap:%
" (timestamp() - t) (h - heapfree)
)
-- time: 0.004 sec
next time think better or/and watch your mouth
Really no reason to get agresive, for me the arrays in your solution looked just like normal arrays. Sorted arrays, as far I know are only avaible in newer max versions and .net is also something, I don´t want to use, as my script should worck with older max versions. Posibly I misunderstood something, as I am not really familiar with both maxscript and english, so apologize then.
Then refrain from telling the people who help you that their solution is crappy in the first place…not a good way to get more help in the future.
Like I said my english is very bad, so I was misunderstood. I said my solution is crappy. I have also apologiezed for that misunderstanding.
hmm…i read it as you are saying DenisT’s solution is crappy…but if you are saying you were talking about YOUR solution well…ok i’ll take your word for it, i will apologize to you.
If you read my very first post, you see that bsearch can´t worck for me, as it´s some feature avaible only in newer max versions. Never said your solution is crappy, so apologize for that misunderstanding again.
Do you really need to support versions prior to 2009?
( BSearch was added in 2009 )
(
gc()
count = 1000000
a = for j = 1 to count collect "A"
/* FINDITEM */
t = timestamp()
finditem a "B"
format "time:%
" (timestamp() - t)
/* DO NOTHING BUT ITERATE */
t = timestamp()
for j = 1 to count do()
format "time:%
" (timestamp() - t)
/* DO NOTHING BUT ITERATE */
t = timestamp()
for j in a do()
format "time:%
" (timestamp() - t)
)
this is more correct comparison:
(
gc()
count = 1000000
a = for j = 1 to count collect "A"
/* FINDITEM */
t = timestamp()
finditem a "B"
format "time:%
" (timestamp() - t)
/* DO NOTHING BUT ITERATE */
x = "B"
t = timestamp()
for j = 1 to count while (x != a[j]) do()
format "time:%
" (timestamp() - t)
/* DO NOTHING BUT ITERATE */
x = "B"
t = timestamp()
for j in a while (x != j) do()
format "time:%
" (timestamp() - t)
)
I know
I was not comparing searching methods but showing that even an empty loop is 20 times slower than findIten(), so anything you do beyond that will of course be a lot slower.
Just answering this:
“… I just wondering how that findItem is faster than my solution if it makes “==” comparison…”
i got it… but i want to show also that simple mxs comparison (and condition) takes time too
Thanks a lot for the explanaition, now I also know why most of my scripts are slow as hell, since I am often use for loops. In programming languages, I normally using for loop is not a performance killer, so from that point of wiev it was not really clear why findItem should be faster than my solution, but now I see.
Please don´t get it wrong, but your solution with qsort and bsearch has one tiny disadvantage, it worcks perfect only with static size arrays, every time a new item is added to the array it becomes unsorted and has to be sorted again. In my opinion the perfect solution would be using something like a hashMap, but my search for something like that was succesles. However thanks again for the help.
you are wrong again…
Using the same quick-search algorithm and sorting function (comparison method) you can easily and quick find an index where to insert new element. So the array in this case remains sorted
yeah…even a non-scripter like me would have enough nous to say…well don’t just append/prepend to the list, it would invalidate the sorted status…so you use the existing ‘smart’ search function, then ‘insert’ it…therefore it remains sorted.
Programming 101 would have thought.
OK, you are tallking about some kind of sorted insertion, right? I just haven´t seen something like this in your code, so posibly you could post it here. Unfortunatelly it´ll be useles for me, because incompatible with older max, but could be usefull for other people.
dude, you have a serious problem – the level of your skills and knowledge is much lower than the complexity of the task.
your thinking capabilities do not correspond to the level of solutions offered … that makes all my solutions are useless for you.
the continuation of the discussion in this vein, I see, does not make sense anymore
So you got it wrong, your solution is really good, but useless for me because bsearch not worcking with older max versions. Also my english is very bad, so you misunderstanding me. Apologize for wasting your time.
The bsearch() method available in 3ds Max 2009 and higher allows for very fast table lookup operations in sorted arrays.
so just curious, you need to work on Max versions earlier than 2009 ?
So what? I personally still on max 9 and 100% satisfied, as it can do all I need for modding and I am really not going to waste my money for the latest max version. When I am saing my script should worck with older max versions, I mean it should worck with posibly all version, at least starting with version 5 or 6. The main reason I am using maxscript is because it can worck with more then 1 max version, not like SDK. For me SDK could be an easier way since it´s usning C++ and my C++ knowledge is much beter then maxscript, OK my maxscript knowledge is about 0, but like I said compiling it for every single max version separatly is a huge pain in the as and Microsoft visual sh*t is also something I don´t want to use. Hope I wase able to explain my situation corectly.