cashew.utils.array 0.1a.2

Chris Nicholson-Sauls ibisbasenji at gmail.com
Thu Sep 14 06:49:38 PDT 2006



Reiner Pope wrote:
> 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

Because I'm mildly paranoid and wanted to avoid memory allocation where I could.  :)  I do 
suppose it could safely be 'in' though.

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

That's a D limitation.  At the moment, function templates cannot be overloaded.  *snort 
stomp*  Originally I /did/ have them named the same, but alas.

> 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);
>   }
> }

That's exactly how it was written (once I'd modified .indexOf) but it doesn't act quite 
right for some reason.  Until I figure out what the bug is, I figured I could slap 
together a less pretty but working version.

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

-- Chris Nicholson-Sauls



More information about the Digitalmars-d-announce mailing list