Class: MatrizDispersa

Inherits:
Matriz
  • Object
show all
Defined in:
lib/prac09/matrizdispersa.rb

Instance Method Summary collapse

Methods inherited from Matriz

#*, #+, #-, #==, #coerce, #columnas, constructor, #filas, #max, #min, #to_s

Constructor Details

#initialize(filas, columnas, velementos) ⇒ MatrizDispersa

Returns a new instance of MatrizDispersa.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/prac09/matrizdispersa.rb', line 4

def initialize(filas, columnas, velementos)
      super(filas, columnas)
               @vvalores= Array.new 
               @vfil= Array.new
               @vcol = Array.new

               longitud=(@filas*@columnas)
  i=0
  while(i<longitud)

          if(velementos[i]!=0)

          @vvalores<< (velementos[i])
          @vfil << (i/@columnas)
            @vcol<< (i%@columnas)
    end
    i=i+1
  end
end

Instance Method Details

#[](f, c) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/prac09/matrizdispersa.rb', line 24

def [](f,c)
    no_nulo = @vvalores.size
    i=0
    while(i < no_nulo)

        if (@vfil[i] == f && @vcol[i] == c)
          return @vvalores[i]
                end
i=i+1
      end
      return 0 
end

#[]=(i, j, nvalor) ⇒ Object

setter



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/prac09/matrizdispersa.rb', line 37

def []= (i,j,nvalor) #setter
  ind = 0
  while ind<@vvalores.size
    if (i == @vfil[ind] && j == @vcol[ind] && nvalor != 0) #para poner un nuevo valor no nulo
      @vvalor[ind]=nvalor
      return
    end
    if (i == @vfil[ind] && j == @vcol[ind] && nvalor == 0)  #para poner un nuevo valor nulo en una posicion ocupada anteriormente
      @vvalores.delete_at(ind)               #simplemente eliminamos el valor que habia antes pues los nulos no se almacenan
      @vfil.delete_at(ind)
      @vcol.delete_at(ind)
      return
    end
    ind += 1
  end #si la posicion no existia la anyadimos con el correspondiente valor
  @vvalores << (nvalor)
  @vfil << (i)
  @vcol << (j)
  
end