On Wed, Nov 12, 2003 at 05:17:28PM +0000, Simon Cozens wrote: > Randy W. Sims: > > Sounds like a set/multiset/bag structure. > > I thought it sounded more like a sorted array, but I'm prepared to be > persuaded otherwise. (Primarily because I've already released the module > to CPAN. ;)
I think the point is that T::S::A is closer to a set than an array, or more to the point it's interace is just an expansion of a Set's interface. Whereas it is a restriction of an array's interface - well it doesn't have to be a restriction but as you point out in the docs, using the full array interface doesn't make sense and for instance push(@a, $n); pop(@a); doesn't leave @a unchanged and a whole load of other un-arraylike things So if Perl's standard collection types had been objects from the start and had been implemented as a hierarchy with consistent interfaces then you probably would have called it Set::Ordered because it looks like a Set with extra stuff: Set methods insert($elem) delete($elem) count get_iterator Sorted Set methods # same as Set and also deleteindex($index) Array methods # same as Sorted Set and also insertindex($index, $elem) # as you point out in the docs, this makes no sense for a T::A::S splice pop etc etc And a Sorted Set could be passed into a function expecting a genuine Set with nothing to worry about. However you cannot pass a T::A::S into a function expecting a genuine array because it might do something like $a->[0] = "defcon 5"; ... launch_missiles() unless $a->[0] = "defcon 5"; All that said, it probably makes more sense to leave the name as it is but how about implementing it as a nice object oriented ordered set and making the Tie stuff a very thin wrapper around that? Actually you could do with *PUSH = \&insert; *DELETE = \&deleteindex; sub delete { # a good home for your binary search implementation } Thinking of it in terms of usage, this module is useful when you have some already existing code that expects an array and wants to - read some values from it - push/unshift some stuff onto it - fill it with completely new contents. If the routine does anything besides that it will not work properly with a T::A::S array. So I'd throw an expcetion in STORE rather than trying to do "the right thing" because it's almost certainly not the right thing. Any code that knows it's a sorted array can achieve the same effect with a delete and a push and it could also use the OO interface which would be faster than going through all the tie stuff, F