/* TA-LIB Copyright (c) 1999-2025, Mario Fortier
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
*   notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
*   notice, this list of conditions and the following disclaimer in
*   the documentation and/or other materials provided with the
*   distribution.
*
* - Neither name of author nor the names of its contributors
*   may be used to endorse or promote products derived from this
*   software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* AUTO-GENERATED by ta_codegen — DO NOT EDIT.
* Source of truth: ta_codegen/input/<name>/  (regenerate: cd ta_codegen/generator && cargo run -- generate)
*/

#include <string.h>
#include <math.h>
#include "ta_func.h"
#include "ta_utility.h"
#include "ta_memory.h"
#include "ta_func_unguarded.h"

/* List of contributors:
 *
 *  Initial  Name/description
 *  -------------------------------------------------------------------
 *  MF       Mario Fortier
 *  EKO      echo999@ifrance.com
 *
 * Change history:
 *
 *  MMDDYY BY   Description
 *  -------------------------------------------------------------------
 *  010802 MF   Template creation.
 *  051103 EKO  Found bug and fix related to outFastD.
 *  052603 MF   Adapt code to compile with .NET Managed C++
 */

TA_LIB_API int TA_STOCHF_Lookback( int optInFastK_Period, int optInFastD_Period, TA_MAType optInFastD_MAType )
{
   int retValue;
   if( (int)optInFastK_Period == (int)0x80000000 )
      optInFastK_Period = 5;
   else if( (int)optInFastK_Period < 1 || (int)optInFastK_Period > 100000 )
      return -1;
   if( (int)optInFastD_Period == (int)0x80000000 )
      optInFastD_Period = 3;
   else if( (int)optInFastD_Period < 1 || (int)optInFastD_Period > 100000 )
      return -1;
   if( (int)optInFastD_MAType == (int)0x80000000 )
      optInFastD_MAType = 0;
   /* Account for the initial data needed for Fast-K. */
   retValue = optInFastK_Period - 1;
   /* Add the smoothing being done for Fast-D */
   retValue += TA_MA_Lookback(optInFastD_Period,optInFastD_MAType);
   return retValue;
}

TA_LIB_API TA_RetCode TA_STOCHF( int    startIdx,
                                 int    endIdx,
                                 const double inHigh[],
                                 const double inLow[],
                                 const double inClose[],
                                 int optInFastK_Period,
                                 int optInFastD_Period,
                                 TA_MAType optInFastD_MAType,
                                 int          *outBegIdx,
                                 int          *outNBElement,
                                 double        outFastK[],
                                 double        outFastD[] )
{
   TA_RetCode retCode;
   double lowest;
   double highest;
   double tmp;
   double diff;
   double *tempBuffer;
   int outIdx;
   int lowestIdx;
   int highestIdx;
   int lookbackTotal;
   int lookbackK;
   int lookbackFastD;
   int trailingIdx;
   int today;
   int i;
   int bufferIsAllocated;

   if( startIdx < 0 )
      return TA_OUT_OF_RANGE_START_INDEX;
   if( (endIdx < 0) || (endIdx < startIdx) )
      return TA_OUT_OF_RANGE_END_INDEX;

   if( !inHigh )
      return TA_BAD_PARAM;
   if( !inLow )
      return TA_BAD_PARAM;
   if( !inClose )
      return TA_BAD_PARAM;
   if( (int)optInFastK_Period == (int)0x80000000 )
      optInFastK_Period = 5;
   else if( (int)optInFastK_Period < 1 || (int)optInFastK_Period > 100000 )
      return TA_BAD_PARAM;
   if( (int)optInFastD_Period == (int)0x80000000 )
      optInFastD_Period = 3;
   else if( (int)optInFastD_Period < 1 || (int)optInFastD_Period > 100000 )
      return TA_BAD_PARAM;
   if( (int)optInFastD_MAType == (int)0x80000000 )
      optInFastD_MAType = 0;
   if( !outFastK )
      return TA_BAD_PARAM;
   if( !outFastD )
      return TA_BAD_PARAM;

   /* With stochastic, there is a total of 4 different lines that
    * are defined: FASTK, FASTD, SLOWK and SLOWD.
    *
    * The D is the signal line usually drawn over its
    * corresponding K function.
    *
    *                    (Today's Close - LowestLow)
    *  FASTK(Kperiod) =  --------------------------- * 100
    *                     (HighestHigh - LowestLow)
    *
    *  FASTD(FastDperiod, MA type) = MA Smoothed FASTK over FastDperiod
    *
    *  SLOWK(SlowKperiod, MA type) = MA Smoothed FASTK over SlowKperiod
    *
    *  SLOWD(SlowDperiod, MA Type) = MA Smoothed SLOWK over SlowDperiod
    *
    * The HighestHigh and LowestLow are the extreme values among the
    * last 'Kperiod'.
    *
    * SLOWK and FASTD are equivalent when using the same period.
    *
    * The following shows how these four lines are made available in TA-LIB:
    *
    *  TA_STOCH  : Returns the SLOWK and SLOWD
    *  TA_STOCHF : Returns the FASTK and FASTD
    *
    * The TA_STOCH function correspond to the more widely implemented version
    * found in many software/charting package. The TA_STOCHF is more rarely
    * used because its higher volatility cause often whipsaws.
    */
   /* Identify the lookback needed. */
   lookbackK = optInFastK_Period - 1;
   lookbackFastD = TA_MA_Lookback(optInFastD_Period,optInFastD_MAType);
   lookbackTotal = lookbackK + lookbackFastD;
   /* Move up the start index if there is not
    * enough initial data.
    */
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   /* Make sure there is still something to evaluate. */
   if( startIdx > endIdx )
   {
      /* Succeed... but no data in the output. */
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   /* Do the K calculation:
    *
    *    Kt = 100 x ((Ct-Lt)/(Ht-Lt))
    *
    * Kt is today stochastic
    * Ct is today closing price.
    * Lt is the lowest price of the last K Period (including today)
    * Ht is the highest price of the last K Period (including today)
    */
   /* Proceed with the calculation for the requested range.
    * Note that this algorithm allows the input and
    * output to be the same buffer.
    */
   outIdx = 0;
   /* Calculate just enough K for ending up with the caller
    * requested range. (The range of k must consider all
    * the lookback involve with the smoothing).
    */
   trailingIdx = startIdx - lookbackTotal;
   today = trailingIdx + lookbackK;
   highestIdx = 0 - 1;
   lowestIdx = highestIdx;
   lowest = 0.0;
   highest = lowest;
   diff = highest;
   /* Allocate a temporary buffer large enough to
    * store the K.
    *
    * If the output is the same as the input, great
    * we just save ourself one memory allocation.
    */
   bufferIsAllocated = 0;
   if( outFastK == inHigh || outFastK == inLow || outFastK == inClose )
   {
      tempBuffer = outFastK;
   } else if( outFastD == inHigh || outFastD == inLow || outFastD == inClose )
   {
      tempBuffer = outFastD;
   } else 
   {
      bufferIsAllocated = 1;
      tempBuffer = malloc((endIdx - today + 1) * sizeof(double));
   }
   /* Do the K calculation */
   while( today <= endIdx )
   {
      /* Set the lowest low */
      tmp = inLow[today];
      if( lowestIdx < trailingIdx )
      {
         lowestIdx = trailingIdx;
         lowest = inLow[lowestIdx];
         i = lowestIdx;
         while( ++i <= today )
         {
            tmp = inLow[i];
            if( tmp < lowest )
            {
               lowestIdx = i;
               lowest = tmp;
            }
         }
         diff = (highest - lowest) / 100.0;
      } else if( tmp <= lowest )
      {
         lowestIdx = today;
         lowest = tmp;
         diff = (highest - lowest) / 100.0;
      }
      /* Set the highest high */
      tmp = inHigh[today];
      if( highestIdx < trailingIdx )
      {
         highestIdx = trailingIdx;
         highest = inHigh[highestIdx];
         i = highestIdx;
         while( ++i <= today )
         {
            tmp = inHigh[i];
            if( tmp > highest )
            {
               highestIdx = i;
               highest = tmp;
            }
         }
         diff = (highest - lowest) / 100.0;
      } else if( tmp >= highest )
      {
         highestIdx = today;
         highest = tmp;
         diff = (highest - lowest) / 100.0;
      }
      /* Calculate stochastic. */
      if( diff != 0.0 )
      {
         tempBuffer[outIdx++] = (inClose[today] - lowest) / diff;
      } else 
      {
         tempBuffer[outIdx++] = 0.0;
      }
      trailingIdx += 1;
      today += 1;
   }
   /* Fast-K calculation completed. This K calculation is returned
    * to the caller. It is smoothed to become Fast-D.
    */
   retCode = TA_MA_Unguarded(0,outIdx - 1,tempBuffer,optInFastD_Period,optInFastD_MAType,outBegIdx,outNBElement,outFastD);
   if( retCode != TA_SUCCESS || (int)*outNBElement == 0 )
   {
      if( bufferIsAllocated )
      {
         free(tempBuffer);
      }
      /* Something wrong happen? No further data? */
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   /* Copy tempBuffer into the caller buffer.
    * (Calculation could not be done directly in the
    *  caller buffer because more input data then the
    *  requested range was needed for doing %D).
    */
   memcpy(outFastK,&tempBuffer[lookbackFastD],(int)*outNBElement * sizeof(double));
   /* Don't need K anymore, free it if it was allocated here. */
   if( bufferIsAllocated )
   {
      free(tempBuffer);
   }
   if( retCode != TA_SUCCESS )
   {
      /* Something wrong happen while processing %D? */
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   /* Note: Keep the outBegIdx relative to the
    *       caller input before returning.
    */
   *outBegIdx= startIdx;
   return TA_SUCCESS;
}

TA_LIB_API TA_RetCode TA_STOCHF_Unguarded( int    startIdx,
                                           int    endIdx,
                                           const double inHigh[],
                                           const double inLow[],
                                           const double inClose[],
                                           int optInFastK_Period,
                                           int optInFastD_Period,
                                           TA_MAType optInFastD_MAType,
                                           int          *outBegIdx,
                                           int          *outNBElement,
                                           double        outFastK[],
                                           double        outFastD[] )
{
   TA_RetCode retCode;
   double lowest;
   double highest;
   double tmp;
   double diff;
   double *tempBuffer;
   int outIdx;
   int lowestIdx;
   int highestIdx;
   int lookbackTotal;
   int lookbackK;
   int lookbackFastD;
   int trailingIdx;
   int today;
   int i;
   int bufferIsAllocated;

   lookbackK = optInFastK_Period - 1;
   lookbackFastD = TA_MA_Lookback(optInFastD_Period,optInFastD_MAType);
   lookbackTotal = lookbackK + lookbackFastD;
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   outIdx = 0;
   trailingIdx = startIdx - lookbackTotal;
   today = trailingIdx + lookbackK;
   highestIdx = 0 - 1;
   lowestIdx = highestIdx;
   lowest = 0.0;
   highest = lowest;
   diff = highest;
   bufferIsAllocated = 0;
   if( outFastK == inHigh || outFastK == inLow || outFastK == inClose )
   {
      tempBuffer = outFastK;
   } else if( outFastD == inHigh || outFastD == inLow || outFastD == inClose )
   {
      tempBuffer = outFastD;
   } else 
   {
      bufferIsAllocated = 1;
      tempBuffer = malloc((endIdx - today + 1) * sizeof(double));
   }
   while( today <= endIdx )
   {
      tmp = inLow[today];
      if( lowestIdx < trailingIdx )
      {
         lowestIdx = trailingIdx;
         lowest = inLow[lowestIdx];
         i = lowestIdx;
         while( ++i <= today )
         {
            tmp = inLow[i];
            if( tmp < lowest )
            {
               lowestIdx = i;
               lowest = tmp;
            }
         }
         diff = (highest - lowest) / 100.0;
      } else if( tmp <= lowest )
      {
         lowestIdx = today;
         lowest = tmp;
         diff = (highest - lowest) / 100.0;
      }
      tmp = inHigh[today];
      if( highestIdx < trailingIdx )
      {
         highestIdx = trailingIdx;
         highest = inHigh[highestIdx];
         i = highestIdx;
         while( ++i <= today )
         {
            tmp = inHigh[i];
            if( tmp > highest )
            {
               highestIdx = i;
               highest = tmp;
            }
         }
         diff = (highest - lowest) / 100.0;
      } else if( tmp >= highest )
      {
         highestIdx = today;
         highest = tmp;
         diff = (highest - lowest) / 100.0;
      }
      if( diff != 0.0 )
      {
         tempBuffer[outIdx++] = (inClose[today] - lowest) / diff;
      } else 
      {
         tempBuffer[outIdx++] = 0.0;
      }
      trailingIdx += 1;
      today += 1;
   }
   retCode = TA_MA_Unguarded(0,outIdx - 1,tempBuffer,optInFastD_Period,optInFastD_MAType,outBegIdx,outNBElement,outFastD);
   if( retCode != TA_SUCCESS || (int)*outNBElement == 0 )
   {
      if( bufferIsAllocated )
      {
         free(tempBuffer);
      }
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   memcpy(outFastK,&tempBuffer[lookbackFastD],(int)*outNBElement * sizeof(double));
   if( bufferIsAllocated )
   {
      free(tempBuffer);
   }
   if( retCode != TA_SUCCESS )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   *outBegIdx= startIdx;
   return TA_SUCCESS;
}

TA_RetCode TA_S_STOCHF( int    startIdx,
                        int    endIdx,
                        const float inHigh[],
                        const float inLow[],
                        const float inClose[],
                        int optInFastK_Period,
                        int optInFastD_Period,
                        TA_MAType optInFastD_MAType,
                        int          *outBegIdx,
                        int          *outNBElement,
                        double        outFastK[],
                        double        outFastD[] )
{
   TA_RetCode retCode;
   double lowest;
   double highest;
   double tmp;
   double diff;
   double *tempBuffer;
   int outIdx;
   int lowestIdx;
   int highestIdx;
   int lookbackTotal;
   int lookbackK;
   int lookbackFastD;
   int trailingIdx;
   int today;
   int i;
   int bufferIsAllocated;

   if( startIdx < 0 )
      return TA_OUT_OF_RANGE_START_INDEX;
   if( (endIdx < 0) || (endIdx < startIdx) )
      return TA_OUT_OF_RANGE_END_INDEX;

   if( !inHigh )
      return TA_BAD_PARAM;
   if( !inLow )
      return TA_BAD_PARAM;
   if( !inClose )
      return TA_BAD_PARAM;
   if( (int)optInFastK_Period == (int)0x80000000 )
      optInFastK_Period = 5;
   else if( (int)optInFastK_Period < 1 || (int)optInFastK_Period > 100000 )
      return TA_BAD_PARAM;
   if( (int)optInFastD_Period == (int)0x80000000 )
      optInFastD_Period = 3;
   else if( (int)optInFastD_Period < 1 || (int)optInFastD_Period > 100000 )
      return TA_BAD_PARAM;
   if( (int)optInFastD_MAType == (int)0x80000000 )
      optInFastD_MAType = 0;
   if( !outFastK )
      return TA_BAD_PARAM;
   if( !outFastD )
      return TA_BAD_PARAM;

   lookbackK = optInFastK_Period - 1;
   lookbackFastD = TA_MA_Lookback(optInFastD_Period,optInFastD_MAType);
   lookbackTotal = lookbackK + lookbackFastD;
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   outIdx = 0;
   trailingIdx = startIdx - lookbackTotal;
   today = trailingIdx + lookbackK;
   highestIdx = 0 - 1;
   lowestIdx = highestIdx;
   lowest = 0.0;
   highest = lowest;
   diff = highest;
   bufferIsAllocated = 0;
   if( (void *)outFastK == (void *)inHigh || (void *)outFastK == (void *)inLow || (void *)outFastK == (void *)inClose )
   {
      tempBuffer = outFastK;
   } else if( (void *)outFastD == (void *)inHigh || (void *)outFastD == (void *)inLow || (void *)outFastD == (void *)inClose )
   {
      tempBuffer = outFastD;
   } else 
   {
      bufferIsAllocated = 1;
      tempBuffer = malloc((endIdx - today + 1) * sizeof(double));
   }
   while( today <= endIdx )
   {
      tmp = inLow[today];
      if( lowestIdx < trailingIdx )
      {
         lowestIdx = trailingIdx;
         lowest = inLow[lowestIdx];
         i = lowestIdx;
         while( ++i <= today )
         {
            tmp = inLow[i];
            if( tmp < lowest )
            {
               lowestIdx = i;
               lowest = tmp;
            }
         }
         diff = (highest - lowest) / 100.0;
      } else if( tmp <= lowest )
      {
         lowestIdx = today;
         lowest = tmp;
         diff = (highest - lowest) / 100.0;
      }
      tmp = inHigh[today];
      if( highestIdx < trailingIdx )
      {
         highestIdx = trailingIdx;
         highest = inHigh[highestIdx];
         i = highestIdx;
         while( ++i <= today )
         {
            tmp = inHigh[i];
            if( tmp > highest )
            {
               highestIdx = i;
               highest = tmp;
            }
         }
         diff = (highest - lowest) / 100.0;
      } else if( tmp >= highest )
      {
         highestIdx = today;
         highest = tmp;
         diff = (highest - lowest) / 100.0;
      }
      if( diff != 0.0 )
      {
         tempBuffer[outIdx++] = (inClose[today] - lowest) / diff;
      } else 
      {
         tempBuffer[outIdx++] = 0.0;
      }
      trailingIdx += 1;
      today += 1;
   }
   retCode = TA_MA_Unguarded(0,outIdx - 1,tempBuffer,optInFastD_Period,optInFastD_MAType,outBegIdx,outNBElement,outFastD);
   if( retCode != TA_SUCCESS || (int)*outNBElement == 0 )
   {
      if( bufferIsAllocated )
      {
         free(tempBuffer);
      }
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   memcpy(outFastK,&tempBuffer[lookbackFastD],(int)*outNBElement * sizeof(double));
   if( bufferIsAllocated )
   {
      free(tempBuffer);
   }
   if( retCode != TA_SUCCESS )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   *outBegIdx= startIdx;
   return TA_SUCCESS;
}

TA_RetCode TA_S_STOCHF_Unguarded( int    startIdx,
                                  int    endIdx,
                                  const float inHigh[],
                                  const float inLow[],
                                  const float inClose[],
                                  int optInFastK_Period,
                                  int optInFastD_Period,
                                  TA_MAType optInFastD_MAType,
                                  int          *outBegIdx,
                                  int          *outNBElement,
                                  double        outFastK[],
                                  double        outFastD[] )
{
   TA_RetCode retCode;
   double lowest;
   double highest;
   double tmp;
   double diff;
   double *tempBuffer;
   int outIdx;
   int lowestIdx;
   int highestIdx;
   int lookbackTotal;
   int lookbackK;
   int lookbackFastD;
   int trailingIdx;
   int today;
   int i;
   int bufferIsAllocated;

   lookbackK = optInFastK_Period - 1;
   lookbackFastD = TA_MA_Lookback(optInFastD_Period,optInFastD_MAType);
   lookbackTotal = lookbackK + lookbackFastD;
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   outIdx = 0;
   trailingIdx = startIdx - lookbackTotal;
   today = trailingIdx + lookbackK;
   highestIdx = 0 - 1;
   lowestIdx = highestIdx;
   lowest = 0.0;
   highest = lowest;
   diff = highest;
   bufferIsAllocated = 0;
   if( (void *)outFastK == (void *)inHigh || (void *)outFastK == (void *)inLow || (void *)outFastK == (void *)inClose )
   {
      tempBuffer = outFastK;
   } else if( (void *)outFastD == (void *)inHigh || (void *)outFastD == (void *)inLow || (void *)outFastD == (void *)inClose )
   {
      tempBuffer = outFastD;
   } else 
   {
      bufferIsAllocated = 1;
      tempBuffer = malloc((endIdx - today + 1) * sizeof(double));
   }
   while( today <= endIdx )
   {
      tmp = inLow[today];
      if( lowestIdx < trailingIdx )
      {
         lowestIdx = trailingIdx;
         lowest = inLow[lowestIdx];
         i = lowestIdx;
         while( ++i <= today )
         {
            tmp = inLow[i];
            if( tmp < lowest )
            {
               lowestIdx = i;
               lowest = tmp;
            }
         }
         diff = (highest - lowest) / 100.0;
      } else if( tmp <= lowest )
      {
         lowestIdx = today;
         lowest = tmp;
         diff = (highest - lowest) / 100.0;
      }
      tmp = inHigh[today];
      if( highestIdx < trailingIdx )
      {
         highestIdx = trailingIdx;
         highest = inHigh[highestIdx];
         i = highestIdx;
         while( ++i <= today )
         {
            tmp = inHigh[i];
            if( tmp > highest )
            {
               highestIdx = i;
               highest = tmp;
            }
         }
         diff = (highest - lowest) / 100.0;
      } else if( tmp >= highest )
      {
         highestIdx = today;
         highest = tmp;
         diff = (highest - lowest) / 100.0;
      }
      if( diff != 0.0 )
      {
         tempBuffer[outIdx++] = (inClose[today] - lowest) / diff;
      } else 
      {
         tempBuffer[outIdx++] = 0.0;
      }
      trailingIdx += 1;
      today += 1;
   }
   retCode = TA_MA_Unguarded(0,outIdx - 1,tempBuffer,optInFastD_Period,optInFastD_MAType,outBegIdx,outNBElement,outFastD);
   if( retCode != TA_SUCCESS || (int)*outNBElement == 0 )
   {
      if( bufferIsAllocated )
      {
         free(tempBuffer);
      }
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   memcpy(outFastK,&tempBuffer[lookbackFastD],(int)*outNBElement * sizeof(double));
   if( bufferIsAllocated )
   {
      free(tempBuffer);
   }
   if( retCode != TA_SUCCESS )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   *outBegIdx= startIdx;
   return TA_SUCCESS;
}

