SourceForge: humaitrader/humaitrader: changeset 179:760f7b6cfb0c
Refined initialSize call
authorCaoyuan Deng <dcaoyuan@gmail.com>
Wed Nov 04 03:55:50 2009 +0800 (3 weeks ago)
changeset 179760f7b6cfb0c
parent 178e92ec4dc0927
child 1808562e9181761
Refined initialSize call
modules/lib.math/src/org/aiotrade/lib/math/timeseries/DefaultTSer.scala
modules/lib.math/src/org/aiotrade/lib/math/timeseries/TStamps.scala
modules/lib.math/src/org/aiotrade/lib/math/timeseries/TStampsFactory.scala
     1.1 --- a/modules/lib.math/src/org/aiotrade/lib/math/timeseries/DefaultTSer.scala	Wed Nov 04 03:26:31 2009 +0800
     1.2 +++ b/modules/lib.math/src/org/aiotrade/lib/math/timeseries/DefaultTSer.scala	Wed Nov 04 03:55:50 2009 +0800
     1.3 @@ -62,6 +62,14 @@
     1.4    val logger = Logger.getLogger(this.getClass.getName)
     1.5  
     1.6    private val INIT_CAPACITY = 400
     1.7 +
     1.8 +  val items = new ArrayList[TItem](INIT_CAPACITY)// this will cause timestamps' lock deadlock?
     1.9 +  /**
    1.10 +   * Each var element of array is a Var that contains a sequence of values for one field of SerItem.
    1.11 +   * @Note: Don't use scala's HashSet or HashMap to store Var, these classes seems won't get all of them stored
    1.12 +   */
    1.13 +  val vars = new ArrayList[TVar[Any]]
    1.14 +
    1.15    /**
    1.16     * we implement occurred timestamps and items in density mode instead of spare
    1.17     * mode, to avoid getItem(time) return null even in case of timestamps has been
    1.18 @@ -71,9 +79,7 @@
    1.19     * Should only get index from timestamps which has the proper mapping of :
    1.20     * position <-> time <-> item
    1.21     */
    1.22 -  private var _timestamps: TStamps = TStampsFactory.createInstance(INIT_CAPACITY)
    1.23 -
    1.24 -  private val _items = new ArrayList[TItem](INIT_CAPACITY)// this will cause timestamps' lock deadlock?
    1.25 +  private var _timestamps = TStampsFactory.createInstance(INIT_CAPACITY)
    1.26  
    1.27    private var tsLog = timestamps.log
    1.28    private var tsLogCheckedCursor = 0
    1.29 @@ -81,15 +87,10 @@
    1.30  
    1.31    private var description = ""
    1.32  
    1.33 -  /**
    1.34 -   * Each var element of array is a Var that contains a sequence of values for one field of SerItem.
    1.35 -   * @Note: Don't use scala's HashSet or HashMap to store Var, these classes seems won't get all of them stored
    1.36 -   */
    1.37 -  val vars = new ArrayList[TVar[Any]]
    1.38 -
    1.39    def this() = this(TFreq.DAILY)
    1.40  
    1.41    def timestamps: TStamps = _timestamps
    1.42 +  
    1.43    protected def attach(timestamps: TStamps): Unit = {
    1.44      this._timestamps = timestamps
    1.45      this.tsLog = timestamps.log
    1.46 @@ -112,8 +113,8 @@
    1.47       * position <-> time <-> item mapping
    1.48       */
    1.49      val idx = timestamps.indexOfOccurredTime(time)
    1.50 -    if (idx >= 0 && idx < _items.size) {
    1.51 -      _items(idx)
    1.52 +    if (idx >= 0 && idx < items.size) {
    1.53 +      items(idx)
    1.54      } else null
    1.55    }
    1.56  
    1.57 @@ -133,7 +134,7 @@
    1.58        if (existIdx >= 0) {
    1.59          vars foreach {_.addNullValue(itemTime)}
    1.60          // * as timestamps includes this time, we just always put in a none-null item
    1.61 -        _items.insert(existIdx, clearItem)
    1.62 +        items.insert(existIdx, clearItem)
    1.63        } else {
    1.64          val idx = timestamps.indexOfNearestOccurredTimeBehind(itemTime)
    1.65          assert(idx >= 0,  "Since itemTime < lastOccurredTime, the idx=" + idx + " should be >= 0")
    1.66 @@ -149,7 +150,7 @@
    1.67            vars foreach {_.addNullValue(itemTime)}
    1.68  
    1.69            // * as timestamps includes this time, we just always put in a none-null item
    1.70 -          _items.insert(idx, clearItem)
    1.71 +          items.insert(idx, clearItem)
    1.72          } finally {
    1.73            _timestamps.writeLock.unlock
    1.74          }
    1.75 @@ -166,7 +167,7 @@
    1.76          vars foreach {_.addNullValue(itemTime)}
    1.77  
    1.78          /** as timestamps includes this time, we just always put in a none-null item  */
    1.79 -        _items += clearItem
    1.80 +        items += clearItem
    1.81        } finally {
    1.82          _timestamps.writeLock.unlock
    1.83        }
    1.84 @@ -176,7 +177,7 @@
    1.85        if (existIdx >= 0) {
    1.86          vars foreach {_.addNullValue(itemTime)}
    1.87          // * as timestamps includes this time, we just always put in a none-null item
    1.88 -        _items += clearItem
    1.89 +        items += clearItem
    1.90        } else {
    1.91          assert(false,
    1.92                 "As it's an adding action, we should not reach here! " +
    1.93 @@ -335,8 +336,8 @@
    1.94          _timestamps.remove(i)
    1.95        }
    1.96  
    1.97 -      for (i <- _items.size - 1 to fromIdx) {
    1.98 -        _items.remove(i)
    1.99 +      for (i <- items.size - 1 to fromIdx) {
   1.100 +        items.remove(i)
   1.101        }
   1.102      } finally {
   1.103        _timestamps.writeLock.unlock
   1.104 @@ -349,8 +350,6 @@
   1.105                                            Long.MaxValue))
   1.106    }
   1.107  
   1.108 -  def items: ArrayList[TItem] = _items
   1.109 -
   1.110    def getItem(time: Long): TItem = {
   1.111      var item = internal_getItem(time)
   1.112      this match {
   1.113 @@ -473,8 +472,8 @@
   1.114          val time = timestamps(i)
   1.115          var break = false
   1.116          var v = null.asInstanceOf[V]
   1.117 -        while (j < _items.size && !break) {
   1.118 -          val vtime = _items(j).time
   1.119 +        while (j < items.size && !break) {
   1.120 +          val vtime = items(j).time
   1.121            if (vtime == time) {
   1.122              // found existed value
   1.123              v = values(j)
     2.1 --- a/modules/lib.math/src/org/aiotrade/lib/math/timeseries/TStamps.scala	Wed Nov 04 03:26:31 2009 +0800
     2.2 +++ b/modules/lib.math/src/org/aiotrade/lib/math/timeseries/TStamps.scala	Wed Nov 04 03:55:50 2009 +0800
     2.3 @@ -57,7 +57,7 @@
     2.4    val REMOVE = 0x8000 // 1000 0000 0000 0000
     2.5    val NUMBER = 0xC000 // 1100 0000 0000 0000
     2.6  }
     2.7 -class TStampsLog extends ArrayList[Short] {
     2.8 +class TStampsLog(initialSize: Int) extends ArrayList[Short](initialSize) {
     2.9    import TStampsLog._
    2.10  
    2.11    private var _logCursor = -1
    2.12 @@ -186,14 +186,14 @@
    2.13  import java.util.concurrent.locks.{Lock,ReentrantReadWriteLock}
    2.14  
    2.15  @cloneable
    2.16 -trait TStamps extends ArrayList[Long] {
    2.17 +abstract class TStamps(initialSize: Int) extends ArrayList[Long](initialSize) {
    2.18    val LONG_LONG_AGO = new GregorianCalendar(1900, Calendar.JANUARY, 1).getTimeInMillis
    2.19  
    2.20    private val readWriteLock = new ReentrantReadWriteLock
    2.21    val readLock:  Lock = readWriteLock.readLock
    2.22    val writeLock: Lock = readWriteLock.writeLock
    2.23  
    2.24 -  val log = new TStampsLog
    2.25 +  val log = new TStampsLog(initialSize)
    2.26  
    2.27    def isOnCalendar: Boolean
    2.28      
     3.1 --- a/modules/lib.math/src/org/aiotrade/lib/math/timeseries/TStampsFactory.scala	Wed Nov 04 03:26:31 2009 +0800
     3.2 +++ b/modules/lib.math/src/org/aiotrade/lib/math/timeseries/TStampsFactory.scala	Wed Nov 04 03:55:50 2009 +0800
     3.3 @@ -41,12 +41,15 @@
     3.4   * @since   1.0.4
     3.5   */
     3.6  object TStampsFactory {
     3.7 -  
     3.8 -  def createInstance(initialCapacity: Int) :TStamps = {
     3.9 -    new TStampsOnOccurred(initialCapacity){override val initialSize = initialCapacity}
    3.10 +
    3.11 +  private var initialCapacity: Int = 16
    3.12 +
    3.13 +  def createInstance(initialCapacity: Int): TStamps = {
    3.14 +    this.initialCapacity = initialCapacity
    3.15 +    new TStampsOnOccurred(initialCapacity)
    3.16    }
    3.17      
    3.18 -  private class TStampsOnOccurred(initialCapacity: Int) extends TStamps {
    3.19 +  private class TStampsOnOccurred(initialCapacity: Int) extends TStamps(initialCapacity) {
    3.20  
    3.21      private val onCalendarShadow = new TStampsOnCalendar(this)
    3.22  
    3.23 @@ -404,7 +407,7 @@
    3.24     * isOnCalendar() always return true.
    3.25     * Why not to use Proxy.class ? for performance reason.
    3.26     */
    3.27 -  private class TStampsOnCalendar(delegateTimestamps: TStamps) extends TStamps {
    3.28 +  private class TStampsOnCalendar(delegateTimestamps: TStamps) extends TStamps(initialCapacity) {
    3.29      /**
    3.30       * the timestamps to be wrapped, it not necessary to be a TimestampsOnOccurred,
    3.31       * any class implemented Timestamps is ok.