Subclassing Integer

I’m writing a state space search program where each state can be consisely
described by an Integer. I need these states to have several new methods.

I can either

  1. create a new class with an Integer attribute
    or
  2. try to subclass Integer
    or
  3. pass around Integers to methods of another class

Option 2) appears to be possible, but perhaps useless, as I cannot
seem to generate objects of the new type:

class State < Integer
def initialize(n)
super(n)
end
end

a=State.new(5) # -> undefined method `new’ for State:Class (NameError)

The only reason I would try option 2) is that
I expect to have 100s of millions of these States around,
and like 3) it might save memory.

Does anyone know if option 1) will take more memory than option 3)
and if anything like option 2) can combine their advantages?

regards,

%!PS % -John Tromp (http://www.cwi.nl/~tromp/)
42 42 scale 7 9 translate .07 setlinewidth .5 setgray/c{arc clip fill
setgray}def 1 0 0 42 1 0 c 0 1 1{0 3 3 90 270 arc 0 0 6 0 -3 3 90 270
arcn 270 90 c -2 2 4{-6 moveto 0 12 rlineto}for -5 2 5{-3 exch moveto
9 0 rlineto}for stroke 0 0 3 1 1 0 c 180 rotate initclip}for showpage

Hi,

I think if you really have 100s of millions of states to be searched,
probably you should consider to code it in straight C to conserve memory
and to speed up execution.

Regards,

Bill

···

============================================================================
John Tromp tromp@dorado.ins.cwi.nl wrote:

I’m writing a state space search program where each state can be consisely
described by an Integer. I need these states to have several new methods.

(deleted)

The only reason I would try option 2) is that
I expect to have 100s of millions of these States around,
and like 3) it might save memory.

Does anyone know if option 1) will take more memory than option 3)
and if anything like option 2) can combine their advantages?

John Tromp wrote:

I’m writing a state space search program where each state can be consisely
described by an Integer. I need these states to have several new methods.

I can either

  1. create a new class with an Integer attribute
    or
  2. try to subclass Integer
    or
  3. pass around Integers to methods of another class

… Snip …

The only reason I would try option 2) is that
I expect to have 100s of millions of these States around,
and like 3) it might save memory.

Does anyone know if option 1) will take more memory than option 3)
and if anything like option 2) can combine their advantages?

1 or 2 would be equally inefficient (if 2 worked). 3) takes much
less memory than any subclassing or a new structure.

One efficient solution is to define new methods under the Integer class,
but of course it is a bit nasty.

Best Regards, Christian

I’m writing a state space search program where each state can be consisely
described by an Integer. I need these states to have several new methods.

I can either
[…]
class State < Integer
def initialize(n)
super(n)
end
end

a=State.new(5) # -> undefined method `new’ for State:Class (NameError)

Do you need to know the number when you create it? I’m wondering
how far Object#id gets you – i.e. that each object already "knows"
its own number. I can’t remember/find how to get back from the
number to the object though.

The only reason I would try option 2) is that
I expect to have 100s of millions of these States around,
and like 3) it might save memory.

Using the ids should help here I think because they exist anyway.

    Hugh
···

On Fri, 20 Sep 2002, John Tromp wrote:

Subclassing might give you undesired effects with return types. Example:

class Cart < Array
def total
sum = 0
each do |product|
sum += product.price
end
end
end

class Product
attr_reader :price
def initialize(price)
@price=price
end
end

c = Cart.new()
c << Product.new(5)
c << Product.new(10)
c.total # => 15

c += [Product.new(7)]
c.total # NameError: undefined method `total’ for #Array:0x402709e8

-billy.

···

On Fri, Sep 20, 2002 at 01:20:35AM +0900, John Tromp wrote:

I’m writing a state space search program where each state can be consisely
described by an Integer. I need these states to have several new methods.

I can either

  1. create a new class with an Integer attribute
    or
  2. try to subclass Integer


Meisterbohne Söflinger Straße 100 Tel: +49-731-399 499-0
eLösungen 89077 Ulm Fax: +49-731-399 499-9

Hi,

···

In message “subclassing Integer” on 02/09/20, John Tromp tromp@dorado.ins.cwi.nl writes:

I’m writing a state space search program where each state can be consisely
described by an Integer. I need these states to have several new methods.

I can either

  1. create a new class with an Integer attribute
    or
  2. try to subclass Integer
    or
  3. pass around Integers to methods of another class

Option 2) appears to be possible, but perhaps useless, as I cannot
seem to generate objects of the new type:

Integer is a pure virtual class (in C++ terms). And its concrete
subclass Fixnum is not suitable for subclassing, since it is a
immediate value. So your choice should be either 1 or 4) add methods
to Integer class.

						matz.

“John Tromp” tromp@dorado.ins.cwi.nl wrote in message news:amcs6e$j2g$1@news.surfnet.nl

I’m writing a state space search program where each state can be consisely
described by an Integer. I need these states to have several new methods.

I can either

  1. create a new class with an Integer attribute
    or
  2. try to subclass Integer
    or
  3. pass around Integers to methods of another class

Option 2) appears to be possible, but perhaps useless, as I cannot
seem to generate objects of the new type:

Well you could write (ASSUMING 1.7)

class << Integer
def allocate; ‘dummy’ end
def new; ‘dummy’ end
remove_method :allocate
remove_method :new
private

def allocate; super end
def new(*rags); super end
def inherited(sub_klass)
class << sub_klass
public :new
private
def inherited(subsub_klass); end
end
end
end

class A < Integer; end

p A.new # <A:0x123…10>

You could also subclass Numeric. Your best is bet
(particularly memory wise) is to use Fixnum as your
State class + adding missing methods.

class State < Integer
def initialize(n)
ser(n)
end
end

a=State.new(5) # -> undefined method `new’ for State:Class (NameError)

The only reason I would try option 2) is that
I expect to have 100s of millions of these States around,

I kind of doubt that it is really realistic to keep 100s of millions of
(distinct?) States in memory around if you are using Ruby.

and like 3) it might save memory.

Does anyone know if option 1) will take more memory than option 3)
and if anything like option 2) can combine their advantages?

Some time ago I played around with a State machines
using something similar to Rec(recursive)Integers [ruby-talk:22264] …
I found this convenient (in particular the applied to add singleton
to RecInteger) and quite fast - however memory wise this
definitely inappropriate for a 100’s million state space …

/Christoph

William Djaja Tjokroaminata wrote:

Hi,

I think if you really have 100s of millions of states to be searched,
probably you should consider to code it in straight C to conserve memory
and to speed up execution.

If one uses Arrays of Integers in Ruby, then you would not spare much
memory using C. Large arrays of Integers are quite efficient in Ruby.
(You just loose one 1 bit precisin per integer, that’s all).

Best Regards, Christian

its own number. I can’t remember/find how to get back from the
number to the object though.

I found it in ObjectSpace:

s = “I am a string” » "I am a string"
r = ObjectSpace._id2ref(s.id) » "I am a string"
r == s » true

To quote the Pickaxe book.

    Hugh
···

On Fri, 20 Sep 2002, I wrote:

Hi,

Using C, if a state can be represented as a number between 0 and 255, we
can use an array of (unsigned) char’s. With this, 100 millions of states
= 100 Mbytes of memory. With Ruby array of integers, this is equal to 400
Mbytes of memory. Because these days a workstation typically has 0.5 to 2
gigabytes of memory, using either Ruby or C can be a “make or
break” case, depending on exact number of the 100 millions. Also,
depending on the algorithm, the C execution may be 10 times faster.

Regards,

Bill

···

=========================================================================
Christian Szegedy szegedy@nospam.or.uni-bonn.de wrote:

William Djaja Tjokroaminata wrote:

Hi,

I think if you really have 100s of millions of states to be searched,
probably you should consider to code it in straight C to conserve memory
and to speed up execution.

If one uses Arrays of Integers in Ruby, then you would not spare much
memory using C. Large arrays of Integers are quite efficient in Ruby.
(You just loose one 1 bit precisin per integer, that’s all).

Best Regards, Christian

William Djaja Tjokroaminata wrote:

Hi,

Using C, if a state can be represented as a number between 0 and 255, we
can use an array of (unsigned) char’s. With this, 100 millions of states
= 100 Mbytes of memory. With Ruby array of integers, this is equal to 400
Mbytes of memory. Because these days a workstation typically has 0.5 to 2
gigabytes of memory, using either Ruby or C can be a “make or
break” case, depending on exact number of the 100 millions. Also,
depending on the algorithm, the C execution may be 10 times faster.

You can use Strings in Ruby to achieve the same memory performance.
Of course, you are right about the execution performance, but
the development time is also an important factor.

Best Regards, Christian

Hi,

If the development time is an important factor, then probably it is better
to use Ruby NArray where the data byte size can really be controlled
(1/2/4 bytes), rather than using Strings. Also, I think NArray gives more
natural built-in methods then a string (e.g., the String [fixNum] method
returns the code of the character at that position, instead of the “direct
value”).

Regards,

Bill

···

============================================================================
Christian Szegedy szegedy@nospam.or.uni-bonn.de wrote:

You can use Strings in Ruby to achieve the same memory performance.
Of course, you are right about the execution performance, but
the development time is also an important factor.

Best Regards, Christian