< Summary - Igor Pro Universal Testing Framework

Information
Class: procedures.igortest-utils-vector
Assembly: procedures
File(s): /builds/mirror/igortest/procedures/igortest-utils-vector.ipf
Tag: 74147b3
Line coverage
57%
Covered lines: 44
Uncovered lines: 33
Coverable lines: 77
Total lines: 154
Line coverage: 57.1%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/builds/mirror/igortest/procedures/igortest-utils-vector.ipf

#LineLine coverage
 1#pragma rtGlobals=3
 2#pragma TextEncoding="UTF-8"
 3#pragma rtFunctionErrors=1
 4#pragma version=1.10
 5#pragma ModuleName=IUTF_Utils_Vector
 6
 7// Vector is a special concept that is similar to Vec<> in Rust or List<T> in C#. It represents a
 8// dynamically sized array of elements. All operation should be done using these methods. The wave
 9// that is returned here is the internal buffer and can contain more elements then it's length.
 10
 11static StrConstant LENGTH_KEY = "NOTE_LENGTH"
 12
 102913static Function SetNumberInWaveNote(wv, key, value)
 14  WAVE     wv
 15  string   key
 16  variable value
 17
 102918  Note/K wv, ReplaceNumberByKey(key, note(wv), value)
 102919End
 20
 21/// @brief Set the length of the vector. This doesn't change the capacity or size of any dimension.
 102922static Function SetLength(wv, value)
 23  WAVE     wv
 24  variable value
 25
 102926  SetNumberInWaveNote(wv, LENGTH_KEY, value)
 102927End
 28
 29/// @brief Returns the length of the vector. This doesn't output the capacity or the real size of
 30/// any dimension.
 101631static Function GetLength(wv)
 32  WAVE wv
 33
 101634  return NumberByKey(LENGTH_KEY, note(wv))
 101635End
 36
 37/// @brief Automatically increase the wave row size if required to fit the specified index.
 38///        The actual (filled) wave size is not tracked, the caller has to do that.
 39///        Returns 1 if the wave was resized, 0 if it was not resized
 40///
 41/// Known Limitations: Igor 32 bit has a limit of 2 GB and 64bit a limit of 200 GB a wave can be.
 41242static Function EnsureCapacity(wv, indexShouldExist)
 43
 44  WAVE     wv
 45  variable indexShouldExist
 46
 41247  variable size = DimSize(wv, UTF_ROW)
 41248  variable targetSize
 49
 41250  if(indexShouldExist < size)
 40651    return 0
 652  endif
 53
 654  // the wave is smaller than any usable chunk
 655  if(size < IUTF_WAVECHUNK_SIZE && indexShouldExist < IUTF_WAVECHUNK_SIZE)
 656    targetSize = IUTF_WAVECHUNK_SIZE
 657    // exponential sizing for smaller waves as this behave asymptotic better
 058  elseif(indexShouldExist < IUTF_BIGWAVECHUNK_SIZE)
 059    // Calculate the target size. This is a shortcut because we need most times to increase the
 060    // size only for a small amount and a single multiplication is faster then the complex
 061    // operation below.
 062    targetSize = size * 2
 063    if(targetSize <= indexShouldExist)
 064      // target size: n
 065      // indexShouldExist: m
 066      // chunk size: c
 067      // exponent: e
 068      //
 069      // n = c * 2 ^ e >= m + 1
 070      // => 2 ^ e >= (m + 1)/c
 071      // => e >= log_2((m + 1) / c)
 072      // => e = ceil(log_2((m + 1) / c))
 073      // => n = c * 2 ^ ceil(log_2((m + 1) / c)) = c * 2 ^ ceil(ln((m + 1) / c) / ln(2))
 074      targetSize = IUTF_WAVECHUNK_SIZE * 2^ceil(ln((indexShouldExist + 1) / IUTF_WAVECHUNK_SIZE) / ln(2))
 075    endif
 076    // linear sizing for really large waves with high system memory impact. This is to reduce system
 077    // memory stress.
 078  else
 079    // target size: n
 080    // indexShouldExist: m
 081    // big chunk size: c
 082    // multiplicator: a
 083    //
 084    // n = c * a >= m + 1
 085    // => a >= (m + 1) / c
 086    // => a = ceil((m + 1) / c)
 087    // => n = c * ceil((m + 1) / c)
 088    targetSize = IUTF_BIGWAVECHUNK_SIZE * ceil((indexShouldExist + 1) / IUTF_BIGWAVECHUNK_SIZE)
 689  endif
 90
 691  Redimension/N=(targetSize, -1, -1, -1) wv
 92
 693  return 1
 41294End
 95
 96/// @brief Add a new row to the end of the vector and ensures if the wave has enough capacity for it.
 97/// This also updates the dimension label "CURRENT" to the new added row.
 98/// @param wv The wave for which a new row should be added
 99/// @returns The row index of the added row.
 323100static Function AddRow(wv)
 101  WAVE wv
 102
 323103  return AddRows(wv, 1)
 323104End
 105
 106/// @brief Add count new rows at the end of the vector and ensures if the wave has enough capacity
 107/// for it. This also updates the dimension label "CURRENT" to the last added row.
 108/// @param wv    The wave for which new rows should be added.
 109/// @param count The number of new rows to add. If this parameter is less or equal than 0 the wave
 110///              remains unchanged.
 111/// @returns The row index of the last added row or -1 if the list is kept unchanged.
 328112static Function AddRows(wv, count)
 113  WAVE     wv
 114  variable count
 115
 328116  variable oldLength, newLength
 117
 328118  if(count <= 0)
 0119    return -1
 328120  endif
 121
 328122  oldLength = GetLength(wv)
 328123  newLength = oldLength + count
 328124  EnsureCapacity(wv, newLength - 1)
 328125  SetLength(wv, newLength)
 126
 328127  IUTF_Utils_Waves#RemoveDimLabel(wv, UTF_ROW, "CURRENT")
 328128  SetDimLabel UTF_ROW, newLength - 1, CURRENT, wv
 129
 328130  return newLength - 1
 328131End
 132
 133/// @brief Find the requested string inside the text wave. The full string has to match. If the
 134/// requested text is found multiple times in the wave it will return one of them.
 135///
 136/// @param wv    The text vector to search in
 137/// @param text  The string to search
 138///
 139/// @returns The index if found or -1 if not.
 322140static Function FindText(wv, text)
 141  WAVE/T wv
 142  string text
 143
 322144  variable length
 145
 146#if (IgorVersion() >= 8.00)
 322147  length = GetLength(wv)
 322148  FindValue/Z/TEXT=(text)/TXOP=5/RMD=[0, length - 1] wv
 149#else
 0150  FindValue/Z/TEXT=(text)/TXOP=5 wv
 151#endif
 152
 322153  return V_value
 322154End