VectorAbstracto.vb

  1. ' VectorAzarAbstracto.vb — ProInf.net — feb-2012
  2. '
  3. ' Gestión de los datos estadísticos contenidos en los recursos: "array_*.csv"
  4.  
  5. Public MustInherit Class VectorAzarAbstracto(Of T As {ValorVector, New})
  6.  
  7. Protected _items() As T
  8. Protected _sumatorio As Integer
  9.  
  10. Public Sub New(ByVal recurso As String)
  11. IniciarArray(recurso)
  12. CalcularSumatorioFrecuencias()
  13. End Sub
  14.  
  15. Protected Sub IniciarArray(ByVal recurso As String)
  16. Dim lista() As String = recurso.Split(vbLf)
  17. _items = New T(lista.Length - 1) {}
  18. For i = 0 To _items.Length - 1
  19. _items(i) = New T()
  20. _items(i).Analizar(lista(i))
  21. Next
  22. End Sub
  23.  
  24. Protected Overridable Sub CalcularSumatorioFrecuencias()
  25. _sumatorio = 0
  26. For Each item In _items
  27. _sumatorio += item.Frecuencia
  28. Next
  29. End Sub
  30.  
  31. Default Public Property Items(ByVal index As Integer) As T
  32. Get
  33. Return _items(index)
  34. End Get
  35. Set(ByVal value As T)
  36. _items(index) = value
  37. End Set
  38. End Property
  39.  
  40. Public ReadOnly Property Count() As Integer
  41. Get
  42. Return _items.Length
  43. End Get
  44. End Property
  45.  
  46. Public Overridable Function Aleatorio() As T
  47. Dim fortuna As Integer = Azar.Entre(0, _items.Count - 1)
  48. Return Items(fortuna)
  49. End Function
  50.  
  51. Public Overridable Function AleatorioPonderado() As T
  52. Dim aleatorio As Integer = Azar.Entre(0, _sumatorio)
  53. Dim frecuencia As Integer = 0
  54. For i = 0 To Count - 1
  55. Dim tipo = Items(i)
  56. frecuencia += tipo.Frecuencia
  57. If aleatorio < frecuencia Then
  58. Return tipo
  59. End If
  60. Next
  61. Return Nothing
  62. End Function
  63.  
  64. Public Overridable Function BuscarPorTexto(ByVal texto As String) As T
  65. For Each item As T In _items
  66. If item.Texto = texto Then
  67. Return item
  68. End If
  69. Next
  70. Return Nothing
  71. End Function
  72.  
  73. End Class

Proinf.net