Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

ArrayBag - what else to use

Moderators: larsivi kris

Posted: 07/24/09 10:06:16

I notice ArrayBag? (tango.util.collection.ArrayBag?) deprecated. I like it, what is used in place of it, (and possibly how do I use the replacement, eg. example). Thanks.

Author Message

Posted: 07/24/09 12:28:09

Depends a bit on why you use ArrayBag, but I would think that tango.util.container.HashSet should be a possible replacement.

Posted: 07/25/09 04:05:57 -- Modified: 07/25/09 04:47:37 by
joelcnz -- Modified 2 Times

I get this error with ArrayBag?, HashSet?, and others I get: object.Exception: Access Violation - Read at address 0x0

Bmp[] frames; with 'frames~=new Bmp;' - works though

Here's a minimal version:

import tango.util.container.HashSet;
void main() {
	HashSet!(Bmp) frames;
	frames.add( new Bmp );
class Bmp {
	this() {

Posted: 07/25/09 08:56:51

You cannot use classes as value types in D - you need to 'new' them.

Posted: 07/26/09 06:07:43

What do you mean by value types (is it things like 'int' and 'real')? And how do I 'new' them (I thought I was)?

Posted: 07/26/09 09:18:44

Value types as opposed to reference types.

When you do

a = b;

with a value type (such as int, float and structs), a becomes a copy of b. If however they are reference types, a will becomes a reference to b. And so changing b in the latter case will be reflected in a, but not in the first (value) case.

In your example you 'new' the element you put into your container, but you don't new the container itself.

Posted: 07/26/09 11:27:25

Oh, silly me! Thanks for your replies.

Another thing, how do I return the reference of an object in a collection? In my program I want the width and height values of the first bitmap in the collection.

int w,h;
foreach( f;frames ) {
  w=f.bitmap.w, h=f.bitmap.h;

Is my workaround.

Posted: 07/26/09 14:15:19

Do you mean a reference to the Bmp object? That's what you get inside foreach. If it's the name of an object, it's always a reference.

By the way, HashSet? probably doesn't keep the values in the order you add them. Just use a dynamic array if you need to control the order.

Posted: 08/13/09 20:36:13

I don't know if this reply would get noticed, it's been a while.

I've found ArraySeq? is deprecated so what do I use instead?


Posted: 08/13/09 21:58:34

Built-in dynamic arrays, probably. Look at tango.core.Array for some utility functions that work on arrays, like find, remove, etc.

Posted: 08/14/09 10:02:25

Thanks for the reply torhu (how come you aren't listed as a moderator in here?).

I noticed Array, but I'm not sure how to use it. I think I need some source code examples.

Posted: 08/14/09 14:57:58

Try this:

I suppose you know how to use the built-in arrays?

Posted: 08/15/09 05:11:05

Thanks, I hadn't thought of looking in the manual. I've had trouble understanding that manual before, and have just been looking in the API.

I use the built in arrays all the time. Do you mean tangos arrays are the same easy?

char[][] elements=["one","two","three"];
foreach(i,e;elements) {
  Stdout.formatln("{}. {}",i+1,e);

Can the data structures in tango support this (how the type 'i' is used), because it doesn't seem to have?

Posted: 08/15/09 16:56:45 -- Modified: 08/15/09 16:59:23 by

The tango.core.Array module doesn't define an array container or anything, it just adds utility functions that you can use with the built-in arrays. Not sure if that's what you meant.

As for using the index with foreach with tango container classes, I don't know if they support that or not. Just try it, and update the index manually if it doesn't work.