Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Improve my bland List

Name: Anonymous 2012-02-04 2:25

class List
        attr_reader :car, :cdr, :size
        def initialize(*args)
                if args.size.zero? then
                        @car = nil; @cdr = nil; @size = 0
                elsif args.size == 1
                        @car = args[0]; @cdr = List.new; @size = 1
                else
                        @car = args[0]
                        @cdr = List.new *args[1 .. args.size - 1]
                        @size = args.size
                end
        end
        def [](n)
                raise ArgumentError unless n.class == Fixnum
                unless n > @size - 1 then
                        if n.zero? then @car
                        else @cdr[n - 1] end
                end
        end
        def []=(n,a)
                raise ArgumentError unless n.class == Fixnum
                unless n > @size - 1 then
                        if n.zero? then @car = a
                        else @cdr[n-1] = a end
                else raise ArgumentError
                end
        end
        def length
                @size
        end
        def null?
                @cdr.nil?
        end
        def member?(a)
                if a.nil? then true end
                unless self.null? then
                        if @car == a then true
                        else @cdr.member? a end
                else false
                end
        end
        def include?(a)
                self.member? a
        end
end

Name: Anonymous 2012-02-05 0:01

>>51
The overhead of providing links is not that large if the size of the objects is much larger than a pointer, or two pointers. You'll be using a lot of extra memory if you try to do a linked list of characters though, and if you tried to store 500MB of characters in a linked list. But maybe that could be useful for some type of splicing algorithm. It kind of reminds me of DNA or something.

But there are times where it is important every operation must finish below a certain amount of time, even if it makes each operation slower. Being really fast for a while, and then suddenly taking time proportional  to the number of prior operations could be bad when you need to keep things in synch, like a media player or something.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List