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

Ticket #409 (closed defect: wontfix)

Opened 1 year ago

Last modified 8 months ago

LinkSeq opApply does not expose element index

Reported by: microm Assigned to: Jim Panic
Priority: major Milestone: 0.99.3
Component: Tango Version: trunk
Keywords: Cc: larsivi, sean_k

Description

LinkSeq? has addAt(index,element), however, I don't see how the index of a LinkSeq? element can be obtained. For example, the following code does not compile because LinkSeq? opApply does not expose the index:

import tango.util.collection.LinkSeq;

void main() {
  auto myints = new LinkSeq!(int);
  myints.append(5);
  int saved_idx;
  bool found = false;
  int idx=0;
  foreach (int idx, int i; myints) {
    if (i > 5) {
      saved_idx = idx;
      found = true;
      break;
   }
  }
  if (found) myints.addAt(saved_idx,4);
}

Is there a way to find the index of a LinkSeq? element so it can be used in addAt()?

Change History

06/02/07 04:22:45 changed by kris

  • status changed from new to assigned.
  • version changed from 0.97 RC1 to trunk.
  • milestone set to 1.0.

07/07/07 07:20:42 changed by Jim Panic

  • owner changed from kris to Jim Panic.
  • status changed from assigned to new.

I've added an opApply function that enables the above use case, hopefully in the same manner as you wanted it to have.

public class LinkSeq(T) : SeqCollection!(T), Sortable!(T) {
        /* .... */
        
	int opApply (int delegate (inout uint index, inout T value) dg) {
		auto scope iterator = new CellIterator!(T)(this);
		return iterator.opApply (dg);
        }
                
        /* .... */

        private static class CellIterator(T) : AbstractIterator!(T) {
                /* .... */

		int opApply (int delegate (inout uint index, inout T value) dg) {
			int result;

			for (auto i=remaining(); i--;)
				{
				auto value = get();
				if ((result = dg(i, value)) != 0)
					break;
				}
			return result;
        	}

                /* .... */
        }

07/07/07 08:13:21 changed by Jim Panic

  • status changed from new to closed.
  • resolution set to fixed.

07/07/07 08:22:18 changed by Jim Panic

  • status changed from closed to reopened.
  • resolution deleted.

07/07/07 08:22:44 changed by Jim Panic

  • status changed from reopened to new.

07/08/07 17:57:16 changed by Jim Panic

The above feature request could be applied to at least all *Seq containers. Yet, it doesn't seem to be possible to use a templated opApply in order to ensure different types for indices like int, uint, short, ushort, et al. This means, either there are opApply methods added for all basic integer types, or one index type is specified for the use with *Seq collection.

Following code would be appreciated, but doesn't quite work out as wanted:

public class LinkSeq(T) : SeqCollection!(T), Sortable!(T)
{
                /* .... */
		int opApply (U) (int delegate (inout U index, inout T value) dg) {
				auto scope iterator = new CellIterator!(T)(this);
				return iterator.opApply (dg);
		}

                /* ...same applies for LinkSeq.CellIterator */
}

Obviously, this would be the easiest way to add such functionality, yet it won't work out, as the compiler (1.018) complains about a method conflict:

/home/jim/tango/tango/util/collection/LinkSeq.d(148): template tango.util.collection.LinkSeq.LinkSeq!(int).LinkSeq.opApply(U) conflicts with function tango.util.collection.LinkSeq.LinkSeq!(int).LinkSeq.opApply at /home/jim/tango/tango/util/collection/LinkSeq.d(142)

Given the fact that this feature is only useful for sequences, it only applies to ArraySeq?, CircularSeq? and LinkSeq?, as those are the only collections that have 'addressable' (via index) elements, addAt methods so to speak.

Comments appreciated!

(follow-up: ↓ 8 ) 07/08/07 18:00:35 changed by kris

  • cc set to larsivi, sean_k.

Yeah, I guess it is quite limited in scope (3 containers). Perhaps we should add the iterator wrapper to these three?

(in reply to: ↑ 7 ) 07/08/07 18:08:14 changed by Jim Panic

Replying to kris:

Yeah, I guess it is quite limited in scope (3 containers). Perhaps we should add the iterator wrapper to these three?

It's still in question whether to add one index type, or all integer types available. Though.. I think uint/ushort would be the two that make most sense, as opposed to int, as used in the initial example.

07/09/07 06:03:46 changed by larsivi

I think Kris's suggestion sounds like the right thing to do. I think one index type is enough, probably uint.

07/09/07 07:05:16 changed by kris

Two index types? NOOOOOOO!

:)

07/09/07 08:10:28 changed by Jim Panic

I've just seen that the internal opApply methods index from $ to 0. Does anyone see a problem with this? (It's basically foreach_reverse that way..)

11/13/07 02:48:15 changed by Jim Panic

  • status changed from new to closed.
  • resolution set to wontfix.

11/13/07 03:57:43 changed by larsivi

  • milestone changed from 1.0 to 0.99.3.