The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Storing an array of indices in an integer

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jan Lelis

Posts: 136
Nickname: rbjl
Registered: Aug, 2009

Jan Lelis is an IT student from Dresden/Germany
Storing an array of indices in an integer Posted: Aug 11, 2009 10:51 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Jan Lelis.
Original Post: Storing an array of indices in an integer
Feed Title: rbJ*_*L.net
Feed URL: http://feeds.feedburner.com/rbJL
Feed Description: Hi, I am a fan of Ruby and like to explore it and the world around ;). So I started this blog, where I am publishing code snippets, tutorials for beginners as well as general thoughts about Ruby, the web or programming in general.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jan Lelis
Latest Posts From rbJ*_*L.net

Advertisement

Sometimes you have an array of indices. These might, for example, act as flags, whether some specific options are set or not.

Consider that you have to store such an array. You could do this in some kind of integer list. However, a better way is, to store it in only one single number.This can be achieved by treating each index as an exponent of 2, so you can build a binary representation out of them. Then this binary representation can be displayed as a single decimal number – and vice versa.

An example
Let’s assume that our array is:

[3,6,9]

Now we can treat it as 2 3 + 2 6 + 2 9, which equals 584. In this number, all the information is still present and can be converted back to

[3,6,9]

The following snippet is a ruby implementation of this.

# # # # # # #
# 'binary representation': store an array of indices in one decimal number
# and vice versa

class Array
  def to_bri # array to integer
    return 0 if self == []

    # first convert indices to a binary number and then use to_i 
    # to convert it to an integer with base 2
    ret = [0] * (self.max + 1)
    self.each do |ele|
      ret[self.max - ele] = 1
    end

    ret.join.to_i(2)
  end
end

class Integer
  def to_bra # integer to array
    ret = []

    # convert the decimal integer to binary form with to_s and base 2 and then
    # check each char to add the current index to the array, if it is set
    self.to_s(2).reverse.chars.each_with_index do |ele,index|
      ret << index if ele == '1'
    end

    ret
  end
end

# Example of use
#
# jl = 584.to_bra  # => [3, 6, 9]
# jl.to_bri        # => 584

Please note: There is no type checking or error management.

CC-BY-SA (DE)

Read: Storing an array of indices in an integer

Topic: Blake Mizerany: How do I learn and master Sinatra? Previous Topic   Next Topic Topic: Agilepalooza in Charlotte

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use