Arrays are part of the Ruby core library. Each Ruby implementation has its own array implementation. The Ruby language specification only indicates the behavior of Ruby arrays; it does not specify any specific implementation strategy. It does not even indicate any performance limitations that could force or at least offer a specific implementation strategy.
However, most rubists have some expectation of performance characteristics of arrays that would force an implementation that does not meet them into the unknown, because no one actually used it:
- Inserting, adding or adding, as well as deleting an element has the worst complexity of phasing O (n) and amortization of the worst stage - complexity O (1)
- Accessing an element has worse O step complexity (1)
- iterating over all elements has the worst degree of complexity O (n)
This means that arrays must be implemented as dynamic arrays with exponential resizing, otherwise these performance guarantees cannot be fulfilled. You can leave with a very wide and small tree, but an AFAIK implementation without Ruby does this.
Here's an implementation of the Rubinius array , which I personally find the easiest to read for all Ruby implementations. (Note: in C ++, only the basic requirements are implemented, most of the array methods are implemented in Ruby, for example, in kernel/common/array.rb ).
Set and SortedSet are part of the Set library in stdlib. Normally stdlib does not change between Ruby implementations (at least the parts that are actually written in Ruby, obviously the parts written in other languages โโcannot be separated), and since Set written in Ruby, you can expect that it will be the same in all Ruby implementations.
Set is implemented as a Hash , where only keys are used, the values โโare always always true : see Set#add in lib/set.rb
SortedSet supported by a red-black tree that is not implemented in Ruby.
source share