www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - dragon prototype stl

reply Joey Peters <Joey_member pathlink.com> writes:
After working a while with D I've made myself a small little STL that does some
pretty useful things (in my eyes anyway). For now, not all features have been
implemented yet, or are fully tested.

http://www.nidhogg.com/dragon.rar

Using arrays:

alias Array!(int) IntArray;
IntArray test = new IntArray;

test.push(10);
test.push(50);
test.front(2);
test.front(22);
test.front(84);
test.push(22);
test.front(84);
test.insert(2, 24);

void printInteger(int which) {
printf("-> %i\n", which);	
}

printf(">> Printing all items\n");

test.call(&printInteger);

printf(">> Printing where 84\n");

test.where(84).call(&printInteger);

printf(">> Printing where 84, 84\n");

const int[2] tc = [84, 84];

test.where(tc).call(&printInteger);

printf(">> Doing a find.\n");

foreach(Position i; test.find(84)) {
printf("%i, %i\n", i.start, i.end);	
}

printf(">> Doing a complex find.\n");

const int[] citms = [84, 84];

foreach(test.findItem i; test.find(citms)) {
printf("%i, %i -\n", i.start, i.end);
test.grab(i).fill(10);
}

printf(">> Now it is: \n");

test.call(&printInteger);

printf(">> Slicing 22\n");

test.slice(test.find(22));
test.call(&printInteger);

printf(">> Putting 99 where once was ten, but as slice\n");
// complex version of replace
test.squeeze(99, test.slice(test.find(10))).call(&printInteger);

printf(">> Putting 666 twice for each 99\n");

const int[] t_ = [666, 666];
// again a complex version
test.squeeze(t_, test.slice(test.find(99))).call(&printInteger);

printf(">> Making replacing 666 with 500\n");

test.replace(666, 200).call(&printInteger);

printf(">> Replacing 200, 200 with 1\n");

const int[] t__ = [200, 200];

test.replace(t__, 1).call(&printInteger);

test.push(1);
test.push(1);

printf(">> Turning 1, 1 in 2, 2\n");

const int[] _1_ = [1, 1];
const int[] _2_ = [2, 2];

test.replace(_1_, _2_).call(&printInteger);

They work the same for strings:

String str = new String("Hello world");
printf(">> Print string\n");
// ~str returns array as [] type, or .getString
printf(~str ~ "\n");

printf(">> Replace o to e\n");
str.replace('o', 'e');
printf(~str ~ "\n");

printf(">> Replace Helle to Hallo\n");
str.replace(r"Helle", r"Hallo");
printf(~str ~ "\n");

And finally a pretty new thing, spawns:

class Foo : Spawn {
char[] name;
this(char[] n) {
name = n;
}
this() {} 
void bar() {
printf("My name is %.*s\n", name);
}
}
..
// first make a few of them
new Foo("Apple");
new Foo("Peach");
new Foo("Orange");
Foo p = new Foo("Pear");
new Foo("Mellon");
new Foo("Banana");
new Foo("Prume");

printf(">> Print them all.\n");	
// then make them all call bar
void manualCall(Foo which) {
which.bar();	
}
Foo.select.as!(Foo).all().call(&manualCall);

printf(">> Print them by 'P'\n");
bit startP(Foo which) {
return which.name[0] == 'P';	
}
Foo.select.as!(Foo).by(&startP).call(&manualCall);
printf(">> Delete Pear and do it again\n");
delete p;
Foo.select.as!(Foo).by(&startP).call(&manualCall);
printf(">> Delete them from starting with P and then show all\n");
Foo.select.as!(Foo).by(&startP).del();
Foo.select.as!(Foo).all().call(&manualCall);
.. etc

Another small thing it can do is convert bases

import base;
base.use(); // makes hex bin and dec
base.hex(255) -> "FF";
bsae.bin(255) -> 11111111
base.hex("FF") -> 255
Base!(radix) custom = new Base!(radix)("0123456789etc");

Things for 0.2 or higher:

- More array functionality
- String formatting overloading () and %, () for sequences and % for
dictionaries:

String a = new String("Hello %s");
a("world",...).getString() -> Hello world

String a = new String("Hello %(item)s");
Dictionary b = new Dictionary;
b["item"] = new String("world");
(a % b).getString()

- Bodies (another new concept)
- Books (lists with (variable?) delegates/functions)
- Dictionaries (hashes with objects as hashes with a base class 'item')
- Variants (automatically type things, and remember the type)
- Math functions
- Custom binary streams (saving objects to files, for example hashed, pure
binary (no size checking), xml, etc)
- Sorting functions and other common algorithms
- Improve selector classes (ie: by can take a static member function)

Suggestions/bugs, anyone?
Sep 09 2004
parent Ant <Ant_member pathlink.com> writes:
In article <chpp8p$28uv$1 digitaldaemon.com>, Joey Peters says...
Suggestions/bugs, anyone?

setup a project at http://www.dsource.org and if it is open source also at http://sourceforge.net Ant
Sep 09 2004