Forums

Multi-dimensional arrays and MultiList

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Multi-dimensional arrays and MultiList

Postby nerdzero » Tue Apr 30, 2013 10:11 am

I see my Code Jam post was deleted. That's fine, guess it was off topic. On topic then, one of the things I hit a snag on during the competition was multi-dimensional arrays. Since the first thing I tried didn't work I just fell back to List<of List<of whatever>> as it was what I was most familiar with.

I did some searching on the forum and it seems there's a patch floating around for multi-dimensional arrays that hasn't been applied yet. Either that or it's supported but I got the syntax wrong. I also seem to remember jaegs doing some enhancements to the MultiList class but since I had never used it before I thought the middle of a timed-event was not the best time to start :)

So, my questions are:

1. What is the current status and future plan for mutli-dimensional arrays?

2. I was reviewing this Wiki page: http://cobra-language.com/trac/cobra/wi ... ionalArray which is about MultiList and was wondering what this means? Maybe I am not understanding something but why would mutability prevent this?

def getHashCode
"""
As a completely mutable object, MultiList does not
support getHashCode
"""


3. For the equals override, this isn't the only criteria, right? The actual element values also have to be equal I assume?

def equals(m as MultiList<of T>)
"""
Equal if shapes are the same, and
elements are in the same order.
Ignores .isReadOnly, .isPermuted, .isReferred
"""


4. Any chance of seeing a HowTo for MutliList? I would be willing to make one if nobody else has the time. Of course, I'd have to figure out how to use it first.
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: Multi-dimensional arrays and MultiList

Postby hopscc » Wed May 01, 2013 7:48 am

The patch for an enhancement for min support for multi dimension(and jagged) arrays is still pending/ hasn't been applied.
Its on ticket:253
hopscc
 
Posts: 631
Location: New Plymouth, Taranaki, New Zealand

Re: Multi-dimensional arrays and MultiList

Postby Charles » Wed May 01, 2013 10:07 am

I don't recall deleting your Code Jam post. I've only been deleting spam messages. We had a ton of them recently. Maybe your message was collateral damage... If so, I apologize.

1. I haven't looked much at multi-dim arrays since MultiList can serve this function today and has various conveniences.

2. This is a general property of .getHashCode and largely independent of language. Basically, you don't want to use the hash of an object only to have it change later, thereby invalidating any place that you have used it such as dictionary keys and sets. If you need more info, you should be able to search for topics on the web like "hashcode mutability" and such.

3. I believe "elements are in the same order" implies "actual element values also have to be equal".

4. Between the doc string and "class TestMultiList", I'm comfortable enough. I don't planning on writing a How-To, but you're free to. Including a How-To in our list would certainly help advertise its features.
Charles
 
Posts: 2510
Location: Los Angeles, CA

Re: Multi-dimensional arrays and MultiList

Postby nerdzero » Wed May 01, 2013 12:07 pm

Charles wrote:I don't recall deleting your Code Jam post. I've only been deleting spam messages. We had a ton of them recently. Maybe message was collateral damage... If so, I apologize.

No worries.

Charles wrote:2. This is a general property of .getHashCode and largely independent of language. Basically, you don't want to use the hash of an object only to have it change later, thereby invalidating any place that you have used it such as dictionary keys and sets. If you need more info, you should be able to search for topics on the web like "hashcode mutability" and such.

Okay, yes I agree that you shouldn't use the hash of something that is for sure going to change, but that shouldn't necessarily mean that .getHashCode should not return a valid value. Right now it's throwing an InvalidOperationException which according to the MSDN Docs is a no-no: http://msdn.microsoft.com/en-us/library ... hcode.aspx
Implementations of the GetHashCode method must not throw exceptions

Just returning 0 would be "more correct" I think. Of course, using the size of the MultiList in conjunction with the element values would probably make more sense though. Just because it's a mutable data structure doesn't mean you couldn't treat it like an immutable one by discipline.

Charles wrote:4. Between the doc string and "class TestMultiList", I'm comfortable enough. I don't planning on writing a How-To, but you're free to. Including a How-To in our list would certainly help advertise its features.

Okay, I'll write something up after playing with it some more.
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: Multi-dimensional arrays and MultiList

Postby Charles » Wed May 01, 2013 5:40 pm

First let me say that right now, you could work around this with: multiList.toString.getHashCode

This will work because MultiList provides a good .toString implementation that includes the contents of the list.

You are correct that MSDN says "Implementations of the GetHashCode method must not throw exceptions" but they also don't give much guidance regarding mutable collections. So I tested List and Dictionary from the .NET std lib and they both give hash codes. Surprisingly, neither of them will change hash codes when their contents change. However, they do give different hash codes between different instances. I tested on both Mono and .NET.

This is the same behavior as Object. I double checked the Mono implementation and Dictionary is not overriding .getHashCode.

So we could do the same thing for consistency with other .NET collections. Or we could explore other ideas.
Charles
 
Posts: 2510
Location: Los Angeles, CA

Re: Multi-dimensional arrays and MultiList

Postby nerdzero » Thu May 02, 2013 8:58 am

Good idea regarding .toString.getHashCode

Regarding the hash code returned by lists and dictionaries, that is surprising. So, that means in Cobra we can have two lists that are equal but have different hash codes. I guess that's okay since they don't determine equality by overriding ".equals", right?
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: Multi-dimensional arrays and MultiList

Postby nerdzero » Thu May 02, 2013 8:18 pm

Well, here's something. I'm not really happy with it but it's a starting point and some examples for anybody in the future that searches for this stuff in the forums. I'm not sure I really understanding how slicing works. I tried to slice some individual rows and columns but couldn't figure out what Pairs to specify.

class MultiListExample

def main
grid3x3 = MultiList<of int>(3, 3)

# You can get the total number of elements in the MultiList
assert grid3x3.count == 9

# You can retrieve information about the dimensions
assert grid3x3.numDims == 2
assert grid3x3.shape == [3, 3]

# Elements are initialized with the default value for the data type
for row in 3, for col in 3, assert grid3x3[row, col] == 0

# Fill the MultiList with 1's
grid3x3.fill(for i in grid3x3.count get 1)
for row in 3, for col in 3, assert grid3x3[row, col] == 1

# prints '[[1, 1, 1], [1, 1, 1], [1, 1, 1]]'
print grid3x3

# You can loop over the elements.
# prints
# 111
# 111
# 111
for row in 3
for col in 3
print grid3x3[row, col] stop
print

# You can loop across a single axis
for col in grid3x3.shape[1]
grid3x3[1, col] = 2
grid3x3[2, col] = 3

# prints '[[1, 1, 1], [2, 2, 2], [3, 3, 3]]'
print grid3x3

# Transpose swaps rows with columns in-place
grid3x3.transpose

# prints '[[1, 2, 3], [1, 2, 3], [1, 2, 3]]'
print grid3x3

# you can slice off new MultiList instances
topLeft2x2 = grid3x3.slice(Pair<of int>(0, 2), Pair<of int>(0, 2))

# prints '[[1, 2], [1, 2]]'
print topLeft2x2

bottomRight2x2 = grid3x3.slice(Pair<of int>(1, 3), Pair<of int>(1, 3))
# prints '[[2, 3], [2, 3]]'
print bottomRight2x2

# Slices know where they came from
assert topLeft2x2.owner is grid3x3
assert bottomRight2x2.owner is grid3x3

# Changes in the owner are reflected in the slices
grid3x3[0, 0] = 0

# prints '[[0, 2], [1, 2]]'
print topLeft2x2

# And vice-versa
topLeft2x2[0, 0] = 1

# prints '[[1, 2, 3], [1, 2, 3], [1, 2, 3]]'
print grid3x3

# You can compare MultiLists for equality or inequality
grid2x2 = MultiList<of int>(2, 2)
assert grid2x2 <> topLeft2x2

for row in 2
for col in 2
grid2x2[row, col] = col + 1

assert grid2x2 == topLeft2x2

# MultiLists can have up to 10,000,000 dimensions or 2,100,000,000 elements
cube = MultiList<of int>(8, 8, 8)
assert cube.count == 512

# These examples will eat your RAM and probably crash on 32-bit systems
spaceTime = MultiList<of uint8>(100, 100, 100, 100)
assert spaceTime.count == 100_000_000

stringTheory = MultiList<of uint8>(10, 10, 10, 10, 10, 10, 10, 10, 10, 2)
assert stringTheory.count == 2_000_000_000
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: Multi-dimensional arrays and MultiList

Postby jaegs » Thu May 02, 2013 11:37 pm

Now that it's almost summer break, hopefully I'll get a chance to work on the MultiList class some more.
jaegs
 
Posts: 58

Re: Multi-dimensional arrays and MultiList

Postby hopscc » Fri May 03, 2013 1:13 am

Given this:
one of the things I hit a snag on during the competition was multi-dimensional arrays. Since the first thing I tried didn't work I just fell back to List<of List<of whatever>> as it was what I was most familiar with

then mayhaps this
haven't looked much at multi-dim arrays since MultiList can serve this function today and has various conveniences.


implies thats its not 100% clear a priori that multiList is a known obvious replacement for MultiDim arrays.

perhaps we should provide more support for multiDim arrays.
I think the points given on The previous Discussion Topic for some minimal support are still valid.

I'll undertake to do a soothing wiki writeup on multi dim array support in cobra and arrays vs Multilist if the patch gets applied.
hopscc
 
Posts: 631
Location: New Plymouth, Taranaki, New Zealand


Return to Discussion

Who is online

Users browsing this forum: No registered users and 10 guests

cron