cashew.utils.array 0.1a.2

Reiner Pope reiner.pope at REMOVE.THIS.gmail.com
Wed Sep 13 22:05:56 PDT 2006


Chris Nicholson-Sauls wrote:
> I've been pushing for some array utilities to get into Phobos, yes, but 
> in the meantime I updated the array module in my personal Cashew lib.  
> Included in the updates are the rotl/rotr that I recall someone asking 
> about.  In the process I've discovered two bugs, as well: the behavior 
> of 'is' versus '==' is incompatable if the operands are arrays, and DDoc 
> for abbreviated function templates is borked.  For an example of the 
> latter, just look at Cashew's own docs.
> 
> That said: if people think this is a decent collection, and Walter would 
> take it, I would be willing to release it to public domain for Phobos' 
> sake.
> 
> The array module is attached, and the docs are at:
> http://www.codemeu.com:81/~pontiff/projects/cashew/doc/array.html
> 
> -- Christopher Nicholson-Sauls
> 
It looks good. Thanks for doing this.

Some comments/questions:

Why does indexOfSub have bale as 'inout', not simply 'in'?
Same question for fill

Couldn't indexOfSub be named indexOf?
Same question for repeatSub and repeat.

Also, thinking about its possible use as the starting point for a 
standard library, I've got some other thoughts for functions which are 
implemented in other array processing libraries (std.string, Oskar 
Linde's templated array functions, mintl.array...). Here is what I was 
thinking could be useful:

splitting/joining:
   split
   join

string processing:
   stripl/stripr/strip/chomp

string matching/replacing:
   replace
   insert
   count
   squeeze // from std.string
   startsWith
   endsWith

searching:
   findmax
   findmin
   find(delegate matches)
   findAll(delegate matches)

I've written some code:
T[][] split (T) (T[] arr, T[] delim ...)
in { assert (delim.length > 0, "Splitting with no delimiters is useless"); }
body
{
   T[][] results;
   T[] token;

   foreach (i, x; arr)
   {
     if (delim.contains(x))
	{
	  if (token.length > 0)
	  {
	     results ~= token;
		 token.length = 0;
	  }
	}
	else
	{
	  token ~= x;
	}
   }
   if (token.length > 0) results ~= token;
   return results;
}

unittest {
   writef("\t.split(...) --------> ");
/+  auto foo = "a b cde   fg";
   auto result = foo.split(' ');
   assert(result == array!(char[])("a", "b", "cde", "fg")); +/

   auto bar = "cashew casehew";
   auto result2 = bar.split('s', 'h');
   assert(result2 == array!(char[])("ca", "ew ca", "e", "ew"));
   writefln("Pass");
}

Also, couldn't you avoid the temporary in removeAll by doing this:
void removeAll (T) (inout T[] haystack, T needle)
{
   size_t index = 0U;
   size_t indices;

   while ((index = haystack.indexOf(needle, index)) != size_t.max) {
     haystack.removeIndex(index);
   }
}

If you're interested, I'll write some more code.

Cheers,

Reiner



More information about the Digitalmars-d-announce mailing list