Files
harbour-core/harbour/source/vm/hashes.c
Przemyslaw Czerpak 26a11bd859 2009-03-21 16:07 UTC+0100 Przemyslaw Czerpak (druzus/at/priv.onet.pl)
* harbour/bin/hb-func.sh
    ! restored default optimization flag (-O3) in hb* scripts

  * harbour/harbour.spec
    ! fixed recent modification

  * harbour/contrib/rddads/ads1.c
  * harbour/contrib/rddads/adsfunc.c
  * harbour/contrib/hbmisc/strfmt.c
  * harbour/contrib/hbcrypt/sha1.c
    ! fixed wrongly used C reference operator

  * harbour/contrib/hbwin/win_prn2.c
  * harbour/contrib/hbwin/wapi_commctrl.c
  * harbour/source/vm/maindllp.c
    ! fixes for unicode builds

  * harbour/include/std.ch
    + added SET TIME FORMAT [TO] <f>

  * harbour/include/set.ch
  * harbour/include/hbset.h
  * harbour/source/vm/set.c
    + added _SET_TIMEFORMAT
      The folowing modifiers are supported:
         hh - hours
         mm - minutes
         ss - seconds
         fff - fraction part of seconds
         p - 1-st letter from PM / AM signature. When used time is shown
             in 12 hour format otherwise 24 hour format is used
      ex: Set( __SET_TIMEFORMAT, "hh:mm pm" )
      default _SET_TIMEFORMAT value is "hh:mm:ss:fff"
    + added C function: char * hb_setGetTimeFormat( void );

  * harbour/include/hbpp.h
  * harbour/source/pp/ppcore.c
    + added support for timestamp constant values in the form:
         t"YYYY-MM-DD HH:MM:SS.fff"
      The exact accepted timestamp pattern is is:
         YYYY-MM-DD [H[H][:M[M][:S[S][.f[f[f[f]]]]]]] [PM|AM]
      i.e.:
         tValue := t"2009-03-21 5:31:45.437 PM"
      or:
         YYYY-MM-DDT[H[H][:M[M][:S[S][.f[f[f[f]]]]]]] [PM|AM]
      with literal "T" as date and time part delimiters (XML timestamp
      format), i.e.:
         tValue := t"2009-03-21T17:31:45.437"
      The folowing characters can be used as date delimiters: "-", "/", "."
      if PM or AM is used HH is in range < 1 : 12 > otherwise
      in range < 0 : 23 >

  * harbour/source/compiler/complex.c
  * harbour/source/compiler/harbour.y
  * harbour/source/compiler/harbour.yyc
  * harbour/source/compiler/harbour.yyh
    + added support for t"YYYY-MM-DD HH:MM:SS.fff" time stamp constant
      to compiler
    + added support for VFP datetime constant values:
         { ^ [ YYYY-MM-DD [,] ] [ HH[:MM[:SS][.FFF]] [AM|PM] ] }
      The following characters can be used as date delimiters: "-", "/".
      Dot "." as date delimiter is not supported.
      There is no limit on number of characters in YYYY, MM, DD, HH, MM,
      SS, FFF parts. Important is only their value. This is the format
      in semi PP notation:
         { ^ <YEAR> <sep:/-> <MONTH> <sep:/-> <DAY> [[<sep2:,>]
           [ <HOUR> [ : <MIN> [ : <SEC> [ . <FRAQ> ] ] ] [AM|PP] ] }
      In practice it allows to also parse xHarbour datetime constant
      values with compatible conditions though xHarbour support onlu "/"
      as date delimiter.
      NOTE: there is one important difference to VFP and xHarbour in
      decoding above format. In VFP and xHarbour when date part is
      missing then it's set by default to: 1899-12-30 so this code:
         { ^ 12:00 }
      gived the same results as:
         { ^ 1899/12/30 12:00 }
      Harbour does not set any default date value when timestamp
      constant value contains only time part.
      If you want we can change it for strict VFP compatiblity.
      Now this extension is enabled by default for testing. In the future
      it will be probably covered by HB_COMPAT_FOXPRO macro.

  * harbour/source/macro/macro.yyc
  * harbour/source/macro/macro.y
  * harbour/source/macro/macro.yyh
  * harbour/source/macro/macrolex.c
    + added support for t"YYYY-MM-DD HH:MM:SS.fff" time stamp constant
      to macro compiler

  * harbour/include/hbmacro.h
  * harbour/source/vm/macro.c
    + added new function:
      void hb_macroGenPushTimeStamp( LONG lDate, LONG lTime, HB_COMP_DECL );

  * harbour/include/hberrors.h
  * harbour/source/compiler/hbgenerr.c
    + added new compile time error: "Invalid timestamp constant '%s'"

  * harbour/source/compiler/cmdcheck.c
    * use Harbour API timestamp functions instead of calling system
      functions

  * harbour/include/hbpcode.h
  * harbour/source/compiler/hbfix.c
  * harbour/source/compiler/hbpcode.c
  * harbour/source/compiler/hbdead.c
  * harbour/source/compiler/genc.c
  * harbour/source/compiler/gencc.c
  * harbour/source/compiler/hblbl.c
  * harbour/source/compiler/hbstripl.c
  * harbour/source/compiler/hbopt.c
    + added new PCODE HB_P_PUSHTIMESTAMP (replaced one unused PCODE value)
      to store timestamp constant values in the PCODE

  * harbour/include/hbcomp.h
  * harbour/source/compiler/hbmain.c
    + added new function:
      void hb_compGenPushTimeStamp( LONG lDate, LONG lTime, HB_COMP_DECL );

  * harbour/include/hbexprop.h
  * harbour/source/common/expropt1.c
    + added new C functions:
      HB_EXPR_PTR hb_compExprNewTimeStamp( LONG, LONG, HB_COMP_DECL );

  * harbour/include/hbexprb.c
  * harbour/include/hbcompdf.h
  * harbour/source/common/expropt2.c
    + added timestamp expressions support
    + added compile time optimization for timestamp expressions
    * updated function optimization for timestamp expressions

  * harbour/include/hbdate.h
  * harbour/source/common/hbdate.c
    * changed hb_dateMilliSeconds() to return UTC julian timestamp in
      milliseconds
    * changed existing seconds, time and timestamp functions to operate
      on common OS specific functions to eliminate possible problems
      with different OS time counters
    + added new functions to operates on time and timestamp values.
      See source code for details.

  + harbour/source/rtl/cputime.c
    * moved double hb_secondsCPU( int n ) function to separate file

  * harbour/source/rtl/seconds.c
    - removed old version of date/seconds functions

  * harbour/source/vm/hashes.c
  * harbour/source/vm/arrays.c
    + added support for timestamp values as hash item indexes

  * harbour/source/vm/hashfunc.c
    + added support for find timestamp values in hash and array items
      by simple date value in AScan(), hb_HScan(), hb_AScan() and hb_RAScan()
      functions when exact comparison is not used.

  * harbour/source/vm/asort.c
    + added support for sorting timestamp values

  * harbour/source/vm/classes.c
    + added support for timestamp scallar classes
    + added support for timestamp typed instance variables

  * harbour/source/vm/memvars.c
    + added support for timestamp values in .mem files

  * harbour/source/vm/estack.c
    + show timestamp values in stack dump

  * harbour/include/hbapi.h
  * harbour/source/vm/arrays.c
    + added new C functions:
      double hb_arrayGetTD( PHB_ITEM pArray, ULONG ulIndex );
      BOOL hb_arraySetTD( PHB_ITEM pArray, ULONG ulIndex, double dTimeStamp )
      BOOL hb_arrayGetTDT( PHB_ITEM pArray, ULONG ulIndex,
                           LONG * plJulian, LONG * plMilliSec );
      BOOL hb_arraySetTDT( PHB_ITEM pArray, ULONG ulIndex,
                           LONG lJulian, LONG lMilliSec );

  * harbour/include/hbapi.h
  * harbour/source/vm/extend.c
    + accept timestamp items in date functions - use only date part of
      timestamp value
    + added new C functions:
      double hb_partd( int iParam, ... );
      BOOL hb_partdt( LONG * plJulian, LONG * plMilliSec , int iParam, ... );
      void hb_rettd( double dTimeStamp );
      void hb_rettdt( LONG lJulian, LONG lMilliSec );
      int hb_stortd( double dTimeStamp, int iParam, ... );
      int hb_stortdt( LONG lJulian, LONG lMilliSec, int iParam, ... );

  * harbour/include/hbapiitm.h
  * harbour/source/vm/itemapi.c
    + accept timestamp items in date functions - use only date part of
      timestamp value
    + added new C functions:
      char * hb_itemGetTS( PHB_ITEM pItem, char * szDateTime );
      PHB_ITEM hb_itemPutTS( PHB_ITEM pItem, const char * szDateTime );
      double hb_itemGetTD( PHB_ITEM pItem );
      PHB_ITEM hb_itemPutTD( PHB_ITEM pItem, double dTimeStamp )
      BOOL hb_itemGetTDT( PHB_ITEM pItem, LONG * plJulian, LONG * plMilliSec );
      PHB_ITEM hb_itemPutTDT( PHB_ITEM pItem, LONG lJulian, LONG lMilliSec );
    + support for timestamp items in hb_itemString(), hb_itemPadConv() and
      hb_itemTypeStr() functions

  * harbour/include/hbvm.h
  * harbour/include/hbxvm.h
  * harbour/include/hbapi.h
  * harbour/source/vm/hvm.c
    + added HB_IT_TIMESTAMP items
    * modified HB_IT_DATE internal item structure to use common structure
      with HB_IT_TIMESTAMP - it  simplifies HVM code and eliminates additional
      RT conditions.
    + added HB_IS_TIMESTAMP() and ISTIMESTAMP() macros
    + added HB_IS_DATETIME() and ISDATETIME() macros - they return true
      if item/parameter is DATE or TIMESTAMP type.
    + added void hb_vmPushTimeStamp( long lJulian, long lMilliSec );
      and xhb_vmPushTimeStamp()
    + added support for timestamp values in relational operators:
         <, <=, >, >=, =, ==
      When two timestamp values are compared then VM compares date and
      time parts in both values.
      When date and timestamp values are used in <, <=, >, >=, = operations
      then VM compares only date part in both values.
      When date and timestamp values are used in == operation then VM
      compares date part in both values and then check if time part
      of timestamp value is 0.
    + added support for timestamp values in + and - math operations.
      The following rules are used in timestamp arithmetic:
         <t> + <t> => <t>
         <t> - <t> => <n>
         <t> + <n> => <t>
         <n> + <t> => <t>
         <t> - <n> => <t>
         <d> + <t> => <t>
         <t> + <d> => <t>
         <d> - <t> => <n>
         <t> - <d> => <n>
      When number is result or argument of timestamp operation then the
      its integer part is a number of day and fractional part is the time.

  * harbour/source/rtl/dateshb.c
    + added new .prg functions to mange date and timestamp values:
      HB_DATETIME() -> <tTimeStamp>
      HB_CTOD( <cDate> [, <cDateFormat> ] ) -> <dDate>
      HB_DTOC( <dDate> [, <cDateFormat> ] ) -> <cDate>
      HB_NTOT( <nValue> ) -> <tTimeStamp>
      HB_TTON( <tTimeStamp> ) -> <nValue>
      HB_TTOC( <tTimeStamp>, [ <cDateFormat> ] [, <cTimeFormat> ] ) ->
                                                            <cTimeStamp>
      HB_CTOT( <cTimeStamp>, [ <cDateFormat> ] [, <cTimeFormat> ] ) ->
                                                            <tTimeStamp>
      HB_TTOS( <tTimeStamp> ) -> <cYYYYMMDDHHMMSSFFF>
      HB_STOT( <cDateTime> ) -> <tTimeStamp>
         <cDateTime> should be in one of the above form:
            - "YYYYMMDDHHMMSSFFF"
            - "YYYYMMDDHHMMSSFF"
            - "YYYYMMDDHHMMSSF"
            - "YYYYMMDDHHMMSS"
            - "YYYYMMDDHHMM"
            - "YYYYMMDDHH"
            - "YYYYMMDD"
            - "HHMMSSFFF"
            - "HHMMSSF"
            - "HHMMSS"
            - "HHMM"
            - "HH"
         Important is number of digits.
      HB_TSTOSTR( <tTimeStamp> ) -> <cTimeStamp> // YYYY-MM-DD HH:MM:SS.fff
      HB_STRTOTS( <cTimeStamp> ) -> <tTimeStamp>
         <cTimeStamp> should be in one of the above form:
            YYYY-MM-DD [H[H][:M[M][:S[S][.f[f[f[f]]]]]]] [PM|AM]
            YYYY-MM-DDT[H[H][:M[M][:S[S][.f[f[f[f]]]]]]] [PM|AM]
         The folowing characters can be used as date delimiters: "-", "/", "."
         T - is literal "T" - it's for XML timestamp format
         if PM or AM is used HH is in range < 1 : 12 > otherwise
         in range < 0 : 23 >
      HB_HOUR( <tTimeStamp> ) -> <nHour>
      HB_MINUTE( <tTimeStamp> ) -> <nMinute>
      HB_SEC( <tTimeStamp> ) -> <nSeconds>   // with milliseconds

  * harbour/source/rtl/datec.c
    + accept timestamp parameters in CMONTH() and CDOW() functions

  * harbour/source/rtl/empty.c
    + added support for timestamp items

  * harbour/source/rtl/itemseri.c
    + added support for timestamp items serialization

  * harbour/source/rtl/minmax.c
    + added support for timestamp values to MIN() and MAX() functions.
      when only one of given parameters is timestamp value and other
      is date value and date parts are equal then always date item
      is returned as both MIN() or MAX() value.

  * harbour/source/rtl/dates.c
    + added new C functions: hb_timeFormat(), hb_timeUnformat(),
      hb_timeStampFormat(), hb_timeStampUnformat()

  * harbour/source/rtl/valtype.c
    + added .prg functions: HB_ISDATETIME(), HB_ISTIMESTAMP()

  * harbour/source/rtl/transfrm.c
    + added support for timestamp items formatting.
    + added new function modifier @T.
      When @T is used without @D then only time part of timestamp value
      is formatted and return.
      When @D is used without @T then only date part of timestamp value
      is formatted and return.

  * harbour/source/rtl/tbrowse.prg
    + display timestamp values

  * harbour/source/rtl/philes.c
    + accept timestamp value as second parameter of HB_FSETDATETIME()
      function
    + store file data and time in second parameter of HB_FGETDATETIME()
      as timestamp value.
    Old parameters (2-dDate, 3-cTime) are still supported but please think
    about removing them - they are redundant.

  * harbour/source/rtl/filesys.c
    * updated function names


  * harbour/source/rdd/dbf1.c
    + added native support for time and timestamp fields

  * harbour/include/hbrddcdx.h
  * harbour/include/hbrddnsx.h
  * harbour/source/rdd/dbfntx/dbfntx1.c
  * harbour/source/rdd/dbfcdx/dbfcdx1.c
  * harbour/source/rdd/dbfnsx/dbfnsx1.c
  * harbour/source/rdd/dbffpt/dbffpt1.c
    + added support for indexing timestamp fields
    + added support for using DATE values with timestamp fields
      which replicate HVM behavior.
      SEEK and SEEKLAST with date value when active index is on
      timestamp positions to 1-st or last record where date part
      of indexed timesamp value is equal.
      Settings scopes to date values when active index is on timestamp
      value reduce the visible record range to these ones which have
      date part of timestamp value in the range of dates values used
      for scopes. It possible to mix date and timestamp values in scope
      and set one scope to date value and the second to timesamp.

  * harbour/source/rdd/sdf1.c
  * harbour/source/rdd/delim1.c
    + added support for exporting timestamp fields

  * harbour/source/rdd/dbsql.c
    ! fixed typo in logical value export
    + export timestamp fields.
      Please update the format to given SQL syntax.


  * harbour/contrib/hbct/files.c
    + changed SETFDATI() to optionally accept timestamp parameter
      instead of two parameters dDate and cTime

  * harbour/contrib/hbct/misc1.c
    + added timestamp support to XTOC() function

  * harbour/contrib/hbct/misc2.c
    + added timestamp support to COMPLEMENT() function

  * harbour/contrib/hbct/dattime2.c
    + accept timestamp values in ADDMONTH(), DOY(), ISLEAP(), QUARTER()
      LASTDAYOM(), WEEK() functions

  * harbour/contrib/hbmzip/hbmzip.c
    + updated HB_ZipFileCreate( hZip, cZipName, tDateTime, cTime, ... )
      functions to optionaly accept timestamp value in 3-rd parameter
      instead od dDate, and cTime in 3-rd and 4-th parameters.
    + updated HB_UnzipFileInfo( hUnzip, @cZipName, @tDateTime, @cTime, ... )
      to return timestamp value in 3-rd parameter instead of date value.
      TODO: Please think about removing 4-th parameter from:
               HB_ZipFileCreate( hZip, cZipName, tDateTime, cTime, ... )
               HB_UnzipFileInfo( hUnzip, @cZipName, @tDateTime, @cTime, ... )
            Now it's redundant.
  * harbour/contrib/hbmzip/readme.txt
    * updated parameters description for above functions

  * harbour/contrib/hbnf/descendn.c
    + added timestamp support to FT_DESCEND() function

  * harbour/contrib/hbclipsm/date.c
    + accept timestamp values in DATEASAGE(), DATEASARRAY() and DATEISLEAP()
      functions

  * harbour/contrib/hbmisc/dates2.c
    + accept timestamp values in ISLEAPYEAR(), DAYSINMONTH(), EOM(), BOM(),
      WOM(), WOY(), EOY() and BOY() functions


   The following contrib files should be updated but I'm leaving it to
   other developers who want to keep this code alive:
         contrib/hbtip/utils.c
         contrib/hbvpdf/hbvpdf.prg
         contrib/hbvpdf/hbvpdft.prg
         contrib/hbwin/win_dll.c
            some minor cleanups if necessary and authors will find
            timestamp values usable

         contrib/hbole/ole2.c
         contrib/hbwin/win_ole.c
            add support for timestamp values in OLE

         contrib/rddado/adordd.prg
         contrib/rddads/ads1.c
         contrib/rddads/adsx.c
         contrib/rddsql/...
            add support for timestamp values in RDDs

   Warning: this implementation is not xHarbour compatible. There are many
   important differences. Mostly on C level and used API. Please be careful
   in porting xHarbour source code to Harbour.
2009-03-21 15:04:43 +00:00

923 lines
28 KiB
C

/*
* $Id$
*/
/*
* Harbour Project source code:
* The Hash tables API (C level)
*
* Copyright 2007 Przemyslaw Czerpak <druzus / at / priv.onet.pl>
* www - http://www.harbour-project.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/).
*
* As a special exception, the Harbour Project gives permission for
* additional uses of the text contained in its release of Harbour.
*
* The exception is that, if you link the Harbour libraries with other
* files to produce an executable, this does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* Your use of that executable is in no way restricted on account of
* linking the Harbour library code into it.
*
* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*
* This exception applies only to the code released by the Harbour
* Project under the name Harbour. If you copy code from other
* Harbour Project or Free Software Foundation releases into a copy of
* Harbour, as the General Public License permits, the exception does
* not apply to the code that you add in this way. To avoid misleading
* anyone as to the status of such modified files, you must delete
* this exception notice from them.
*
* If you write modifications of your own for Harbour, it is your choice
* whether to permit this exception to apply to your modifications.
* If you do not wish that, delete this exception notice.
*
*/
#ifndef _HB_HASH_INTERNAL_
# define _HB_HASH_INTERNAL_
#endif
#include "hbvmopt.h"
#include "hbapi.h"
#include "hbapiitm.h"
#include "hbapierr.h"
#include "hbapilng.h"
#include "hbvm.h"
#include "hbxvm.h"
#include "hbstack.h"
#define HB_HASH_ITEM_ALLOC 16
/* internal structures for hashes */
typedef struct _HB_HASHPAIR
{
HB_ITEM key;
HB_ITEM value;
} HB_HASHPAIR, * PHB_HASHPAIR;
typedef struct _HB_BASEHASH
{
PHB_HASHPAIR pPairs; /* pointer to the array of key/value pairs */
PHB_ITEM pDefault; /* default autoadd value */
ULONG ulSize; /* size of allocated pair array */
ULONG ulLen; /* number of used items in pair array */
int iFlags; /* hash item flags */
} HB_BASEHASH, * PHB_BASEHASH, * HB_BASEHASH_PTR;
void hb_hashRefGrabage( PHB_ITEM pHash )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashRefGrabage(%p)", pHash));
if( HB_IS_HASH( pHash ) && pHash->item.asHash.value->ulLen > 0 )
{
PHB_HASHPAIR pPairs = pHash->item.asHash.value->pPairs;
ULONG ulLen = pHash->item.asHash.value->ulLen;
while( ulLen-- )
{
if( HB_IS_GCITEM( &pPairs[ ulLen ].key ) )
hb_gcItemRef( &pPairs[ ulLen ].key );
if( HB_IS_GCITEM( &pPairs[ ulLen ].value ) )
hb_gcItemRef( &pPairs[ ulLen ].value );
}
}
}
/* This releases hash when called from the garbage collector */
static HB_GARBAGE_FUNC( hb_hashReleaseGarbage )
{
PHB_BASEHASH pBaseHash = ( PHB_BASEHASH ) Cargo;
HB_TRACE(HB_TR_INFO, ("hb_hashReleaseGarbage(%p)", pBaseHash ));
if( pBaseHash->ulSize > 0 )
{
PHB_HASHPAIR pPairs = pBaseHash->pPairs;
ULONG ulLen = pBaseHash->ulLen;
/*
* clear the pBaseHash->pPairs to avoid infinite loop in cross
* referenced items when pBaseArray is not freed due to buggy
* object destructor [druzus]
*/
pBaseHash->pPairs = NULL;
pBaseHash->ulLen = 0;
while( ulLen-- )
{
if( HB_IS_COMPLEX( &pPairs[ ulLen ].key ) )
hb_itemClear( &pPairs[ ulLen ].key );
if( HB_IS_COMPLEX( &pPairs[ ulLen ].value ) )
hb_itemClear( &pPairs[ ulLen ].value );
}
hb_xfree( pPairs );
}
if( pBaseHash->pDefault )
{
PHB_ITEM pDefault = pBaseHash->pDefault;
pBaseHash->pDefault = NULL;
hb_itemRelease( pDefault );
}
}
static int hb_hashItemCmp( PHB_ITEM pKey1, PHB_ITEM pKey2, int iFlags )
{
if( HB_IS_STRING( pKey1 ) )
{
if( HB_IS_STRING( pKey2 ) )
{
if( iFlags & HB_HASH_BINARY )
return pKey1->item.asString.length < pKey2->item.asString.length ? -1 :
( pKey1->item.asString.length > pKey2->item.asString.length ? 1 :
memcmp( pKey1->item.asString.value,
pKey2->item.asString.value,
pKey1->item.asString.length ) );
else if( iFlags & HB_HASH_IGNORECASE )
return hb_itemStrICmp( pKey1, pKey2, TRUE );
else
return hb_itemStrCmp( pKey1, pKey2, TRUE );
}
else
return 1;
}
else if( HB_IS_DATETIME( pKey1 ) )
{
if( HB_IS_DATETIME( pKey2 ) )
return pKey1->item.asDateTime.julian < pKey2->item.asDateTime.julian ? -1 :
( pKey1->item.asDateTime.julian > pKey2->item.asDateTime.julian ? 1 :
( pKey1->item.asDateTime.time < pKey2->item.asDateTime.time ? -1 :
( pKey1->item.asDateTime.time < pKey2->item.asDateTime.time ? 1 : 0 ) ) );
else if( HB_IS_STRING( pKey2 ) )
return -1;
else
return 1;
}
else if( HB_IS_POINTER( pKey1 ) )
{
if( HB_IS_POINTER( pKey2 ) )
return pKey1->item.asPointer.value < pKey2->item.asPointer.value ? -1 :
( pKey1->item.asPointer.value > pKey2->item.asPointer.value ? 1 : 0 );
else if( HB_IS_STRING( pKey2 ) || HB_IS_DATETIME( pKey2 ) )
return -1;
else
return 1;
}
else if( HB_IS_NUMINT( pKey1 ) && HB_IS_NUMINT( pKey2 ) )
{
HB_LONG l1 = HB_ITEM_GET_NUMINTRAW( pKey1 ),
l2 = HB_ITEM_GET_NUMINTRAW( pKey2 );
return l1 < l2 ? -1 : ( l1 > l2 ? 1 : 0 );
}
else if( HB_IS_NUMERIC( pKey2 ) )
{
double d1 = hb_itemGetND( pKey1 ), d2 = hb_itemGetND( pKey2 );
return d1 < d2 ? -1 : ( d1 > d2 ? 1 : 0 );
}
return -1;
}
static void hb_hashResort( PHB_BASEHASH pBaseHash )
{
ULONG ulPos, ulFrom;
int iFlags = pBaseHash->iFlags;
/* The hash array is probably quite well sorted so this trivial
* algorithm is the most efficient one [druzus]
*/
for( ulFrom = 1; ulFrom < pBaseHash->ulLen; ++ulFrom )
{
ulPos = ulFrom;
while( ulPos > 0 && hb_hashItemCmp( &pBaseHash->pPairs[ ulPos - 1 ].key,
&pBaseHash->pPairs[ ulPos ].key,
iFlags ) > 0 )
{
HB_HASHPAIR pair;
memcpy( &pair, pBaseHash->pPairs + ulPos - 1, sizeof( HB_HASHPAIR ) );
memcpy( pBaseHash->pPairs + ulPos - 1, pBaseHash->pPairs + ulPos, sizeof( HB_HASHPAIR ) );
memcpy( pBaseHash->pPairs + ulPos, &pair, sizeof( HB_HASHPAIR ) );
--ulPos;
}
}
pBaseHash->iFlags &= ~HB_HASH_RESORT;
}
static BOOL hb_hashFind( PHB_BASEHASH pBaseHash, PHB_ITEM pKey, ULONG * pulPos )
{
ULONG ulLeft, ulRight, ulMiddle;
int iFlags = pBaseHash->iFlags;
int i;
if( iFlags & HB_HASH_RESORT )
hb_hashResort( pBaseHash );
ulLeft = 0;
ulRight = pBaseHash->ulLen;
while( ulLeft < ulRight )
{
ulMiddle = ( ulLeft + ulRight ) >> 1;
i = hb_hashItemCmp( &pBaseHash->pPairs[ ulMiddle ].key, pKey, iFlags );
if( i == 0 )
{
if( pulPos )
*pulPos = ulMiddle;
return TRUE;
}
else if( i < 0 )
ulLeft = ulMiddle + 1;
else
ulRight = ulMiddle;
}
if( pulPos )
*pulPos = ulLeft;
return FALSE;
}
static void hb_hashResize( PHB_BASEHASH pBaseHash, ULONG ulNewSize )
{
if( pBaseHash->ulSize < ulNewSize )
{
if( pBaseHash->ulSize )
pBaseHash->pPairs = ( PHB_HASHPAIR ) hb_xrealloc( pBaseHash->pPairs,
ulNewSize * sizeof( HB_HASHPAIR ) );
else
pBaseHash->pPairs = ( PHB_HASHPAIR ) hb_xgrab( ulNewSize * sizeof( HB_HASHPAIR ) );
do
{
pBaseHash->pPairs[ pBaseHash->ulSize ].key.type = HB_IT_NIL;
pBaseHash->pPairs[ pBaseHash->ulSize ].value.type = HB_IT_NIL;
}
while( ++pBaseHash->ulSize < ulNewSize );
}
else if( pBaseHash->ulSize > ulNewSize && pBaseHash->ulLen <= ulNewSize )
{
pBaseHash->ulSize = ulNewSize;
if( ulNewSize )
pBaseHash->pPairs = ( PHB_HASHPAIR ) hb_xrealloc( pBaseHash->pPairs,
ulNewSize * sizeof( HB_HASHPAIR ) );
else
{
hb_xfree( pBaseHash->pPairs );
pBaseHash->pPairs = NULL;
}
}
}
static PHB_ITEM hb_hashValuePtr( PHB_BASEHASH pBaseHash, PHB_ITEM pKey, BOOL fAdd )
{
ULONG ulPos;
if( !hb_hashFind( pBaseHash, pKey, &ulPos ) )
{
if( !fAdd )
return NULL;
if( pBaseHash->ulSize == pBaseHash->ulLen )
hb_hashResize( pBaseHash, pBaseHash->ulSize + HB_HASH_ITEM_ALLOC );
if( ulPos < pBaseHash->ulLen )
{
memmove( pBaseHash->pPairs + ulPos + 1, pBaseHash->pPairs + ulPos,
( pBaseHash->ulLen - ulPos ) * sizeof( HB_HASHPAIR ) );
pBaseHash->pPairs[ ulPos ].key.type = HB_IT_NIL;
pBaseHash->pPairs[ ulPos ].value.type = HB_IT_NIL;
}
hb_itemCopy( &pBaseHash->pPairs[ ulPos ].key, pKey );
pBaseHash->ulLen++;
if( pBaseHash->pDefault )
{
PHB_ITEM pDefault = hb_itemClone( pBaseHash->pDefault );
hb_itemMove( &pBaseHash->pPairs[ ulPos ].value, pDefault );
hb_itemRelease( pDefault );
}
}
return &pBaseHash->pPairs[ ulPos ].value;
}
static BOOL hb_hashNewValue( PHB_BASEHASH pBaseHash, PHB_ITEM pKey, PHB_ITEM pValue )
{
ULONG ulPos;
if( !hb_hashFind( pBaseHash, pKey, &ulPos ) )
{
if( pBaseHash->ulSize == pBaseHash->ulLen )
hb_hashResize( pBaseHash, pBaseHash->ulSize + HB_HASH_ITEM_ALLOC );
if( ulPos < pBaseHash->ulLen )
{
memmove( pBaseHash->pPairs + ulPos + 1, pBaseHash->pPairs + ulPos,
( pBaseHash->ulLen - ulPos ) * sizeof( HB_HASHPAIR ) );
pBaseHash->pPairs[ ulPos ].key.type = HB_IT_NIL;
pBaseHash->pPairs[ ulPos ].value.type = HB_IT_NIL;
}
hb_itemCopy( &pBaseHash->pPairs[ ulPos ].key, pKey );
hb_itemCopyFromRef( &pBaseHash->pPairs[ ulPos ].value, pValue );
pBaseHash->ulLen++;
return TRUE;
}
return FALSE;
}
static void hb_hashNewPair( PHB_BASEHASH pBaseHash, PHB_ITEM * pKeyPtr, PHB_ITEM * pValPtr )
{
if( pBaseHash->ulSize == pBaseHash->ulLen )
hb_hashResize( pBaseHash, pBaseHash->ulSize + HB_HASH_ITEM_ALLOC );
* pKeyPtr = &pBaseHash->pPairs[ pBaseHash->ulLen ].key;
* pValPtr = &pBaseHash->pPairs[ pBaseHash->ulLen ].value;
pBaseHash->ulLen++;
}
static void hb_hashDelPair( PHB_BASEHASH pBaseHash, ULONG ulPos )
{
if( --pBaseHash->ulLen == 0 )
{
PHB_HASHPAIR pPairs = pBaseHash->pPairs;
pBaseHash->pPairs = NULL;
pBaseHash->ulSize = 0;
if( HB_IS_COMPLEX( &pPairs->key ) )
hb_itemClear( &pPairs->key );
if( HB_IS_COMPLEX( &pPairs->value ) )
hb_itemClear( &pPairs->value );
hb_xfree( pPairs );
}
else
{
if( ulPos != pBaseHash->ulLen )
{
HB_HASHPAIR pair;
memcpy( &pair, pBaseHash->pPairs + ulPos, sizeof( HB_HASHPAIR ) );
memmove( pBaseHash->pPairs + ulPos, pBaseHash->pPairs + ulPos + 1,
( pBaseHash->ulLen - ulPos ) * sizeof( HB_HASHPAIR ) );
ulPos = pBaseHash->ulLen;
memcpy( pBaseHash->pPairs + ulPos, &pair, sizeof( HB_HASHPAIR ) );
}
hb_itemSetNil( &pBaseHash->pPairs[ ulPos ].key );
hb_itemSetNil( &pBaseHash->pPairs[ ulPos ].value );
if( pBaseHash->ulSize - pBaseHash->ulLen > ( HB_HASH_ITEM_ALLOC << 1 ) )
{
pBaseHash->ulSize -= HB_HASH_ITEM_ALLOC;
pBaseHash->pPairs = ( PHB_HASHPAIR ) hb_xrealloc( pBaseHash->pPairs,
pBaseHash->ulSize * sizeof( HB_HASHPAIR ) );
}
}
}
PHB_ITEM hb_hashNew( PHB_ITEM pItem )
{
PHB_BASEHASH pBaseHash;
HB_TRACE(HB_TR_DEBUG, ("hb_hashNew(%p)", pItem));
if( pItem == NULL )
pItem = hb_itemNew( NULL );
else if( HB_IS_COMPLEX( pItem ) )
hb_itemClear( pItem );
pBaseHash = ( PHB_BASEHASH ) hb_gcAlloc( sizeof( HB_BASEHASH ), hb_hashReleaseGarbage );
pBaseHash->pPairs = NULL;
pBaseHash->ulSize = 0;
pBaseHash->ulLen = 0;
pBaseHash->iFlags = HB_HASH_FLAG_DEFAULT;
pBaseHash->pDefault = NULL;
pItem->type = HB_IT_HASH;
pItem->item.asHash.value = pBaseHash;
return pItem;
}
ULONG hb_hashLen( PHB_ITEM pHash )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashLen(%p)", pHash));
if( HB_IS_HASH( pHash ) )
return pHash->item.asHash.value->ulLen;
else
return 0;
}
void hb_hashPreallocate( PHB_ITEM pHash, ULONG ulNewSize )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashPreallocate(%p,%lu)", pHash, ulNewSize));
if( HB_IS_HASH( pHash ) )
hb_hashResize( pHash->item.asHash.value, ulNewSize );
}
BOOL hb_hashAllocNewPair( PHB_ITEM pHash, PHB_ITEM * pKeyPtr, PHB_ITEM * pValPtr )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashAllocNewPair(%p,%p,%p)", pHash, pKeyPtr, pValPtr));
if( HB_IS_HASH( pHash ) )
{
hb_hashNewPair( pHash->item.asHash.value, pKeyPtr, pValPtr );
return TRUE;
}
else
return FALSE;
}
void hb_hashSort( PHB_ITEM pHash )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashSort(%p)", pHash));
if( HB_IS_HASH( pHash ) )
{
if( pHash->item.asHash.value->iFlags & HB_HASH_RESORT )
hb_hashResort( pHash->item.asHash.value );
}
}
PHB_ITEM hb_hashGetItemPtr( PHB_ITEM pHash, PHB_ITEM pKey, int iFlags )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashGetItemPtr(%p,%p,%d)", pHash, pKey, iFlags));
if( HB_IS_HASH( pHash ) && HB_IS_HASHKEY( pKey ) )
{
PHB_ITEM pDest = hb_hashValuePtr( pHash->item.asHash.value, pKey,
iFlags && ( pHash->item.asHash.value->iFlags & iFlags ) == iFlags );
if( pDest )
return HB_IS_BYREF( pDest ) ? hb_itemUnRef( pDest ) : pDest;
}
return NULL;
}
PHB_ITEM hb_hashGetItemRefPtr( PHB_ITEM pHash, PHB_ITEM pKey )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashGetItemRefPtr(%p,%p)", pHash, pKey));
if( HB_IS_HASH( pHash ) && HB_IS_HASHKEY( pKey ) )
{
PHB_ITEM pDest = hb_hashValuePtr( pHash->item.asHash.value, pKey,
( pHash->item.asHash.value->iFlags & HB_HASH_AUTOADD_REFERENCE ) ==
HB_HASH_AUTOADD_REFERENCE );
if( pDest )
{
if( !HB_IS_BYREF( pDest ) )
pDest = hb_memvarDetachLocal( pDest );
return pDest;
}
}
return NULL;
}
BOOL hb_hashScan( PHB_ITEM pHash, PHB_ITEM pKey, ULONG * pulPos )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashScan(%p,%p,%p)", pHash, pKey, pulPos));
if( HB_IS_HASH( pHash ) )
{
ULONG ulPos;
if( HB_IS_HASHKEY( pKey ) )
{
if( hb_hashFind( pHash->item.asHash.value, pKey, &ulPos ) )
{
if( pulPos )
*pulPos = ulPos + 1;
return TRUE;
}
}
else if( HB_IS_HASH( pKey ) && pKey->item.asHash.value->ulLen == 1 )
{
if( hb_hashFind( pHash->item.asHash.value, &pKey->item.asHash.value->pPairs[ 0 ].key, &ulPos ) )
{
PHB_ITEM pVal1 = &pHash->item.asHash.value->pPairs[ ulPos ].value;
PHB_ITEM pVal2 = &pKey->item.asHash.value->pPairs[ 0 ].value;
BOOL fResult = FALSE;
if( HB_IS_STRING( pVal1 ) && HB_IS_STRING( pVal2 ) )
fResult = hb_itemStrCmp( pVal1, pVal2, TRUE ) == 0;
else if( HB_IS_NUMINT( pVal1 ) && HB_IS_NUMINT( pVal2 ) )
fResult = HB_ITEM_GET_NUMINTRAW( pVal1 ) == HB_ITEM_GET_NUMINTRAW( pVal2 );
else if( HB_IS_NUMERIC( pVal1 ) && HB_IS_NUMERIC( pVal2 ) )
fResult = hb_itemGetND( pVal1 ) == hb_itemGetND( pVal2 );
else if( HB_IS_NIL( pVal1 ) && HB_IS_NIL( pVal2 ) )
fResult = TRUE;
else if( hb_itemType( pVal1 ) & hb_itemType( pVal2 ) )
{
hb_vmPush( pVal1 );
hb_vmPush( pVal2 );
if( !hb_xvmExactlyEqual() )
{
fResult = hb_itemGetL( hb_stackItemFromTop( -1 ) );
hb_stackPop();
}
}
if( fResult )
{
if( pulPos )
*pulPos = ulPos + 1;
return TRUE;
}
}
}
}
if( pulPos )
*pulPos = 0;
return FALSE;
}
static BOOL hb_hashClear( PHB_ITEM pHash )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashClear(%p)", pHash));
if( HB_IS_HASH( pHash ) )
{
if( pHash->item.asHash.value->ulSize )
{
while( pHash->item.asHash.value->ulLen )
{
pHash->item.asHash.value->ulLen--;
if( HB_IS_COMPLEX( &pHash->item.asHash.value->pPairs[ pHash->item.asHash.value->ulLen ].key ) )
hb_itemClear( &pHash->item.asHash.value->pPairs[ pHash->item.asHash.value->ulLen ].key );
if( HB_IS_COMPLEX( &pHash->item.asHash.value->pPairs[ pHash->item.asHash.value->ulLen ].value ) )
hb_itemClear( &pHash->item.asHash.value->pPairs[ pHash->item.asHash.value->ulLen ].value );
}
/*
* This condition is a protection against recursive call
* from .prg object destructor [druzus]
*/
if( pHash->item.asHash.value->ulSize )
{
hb_xfree( pHash->item.asHash.value->pPairs );
pHash->item.asHash.value->pPairs = NULL;
pHash->item.asHash.value->ulSize = 0;
}
}
return TRUE;
}
return FALSE;
}
BOOL hb_hashDel( PHB_ITEM pHash, PHB_ITEM pKey )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashDel(%p,%p)", pHash, pKey));
if( HB_IS_HASH( pHash ) && HB_IS_HASHKEY( pKey ) )
{
PHB_BASEHASH pBaseHash = pHash->item.asHash.value;
ULONG ulPos;
if( hb_hashFind( pBaseHash, pKey, &ulPos ) )
{
hb_hashDelPair( pBaseHash, ulPos );
return TRUE;
}
}
return FALSE;
}
BOOL hb_hashRemove( PHB_ITEM pHash, PHB_ITEM pItem )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashRemove(%p,%p)", pHash, pItem));
if( HB_IS_HASH( pHash ) )
{
if( HB_IS_HASHKEY( pItem ) )
{
hb_hashDel( pHash, pItem );
return TRUE;
}
else if( HB_IS_ARRAY( pItem ) )
{
ULONG ul = 0;
PHB_ITEM pKey;
while( ( pKey = hb_arrayGetItemPtr( pItem, ++ul ) ) != NULL )
hb_hashDel( pHash, pKey );
return TRUE;
}
else if( HB_IS_HASH( pItem ) )
{
if( pHash->item.asHash.value == pItem->item.asHash.value )
hb_hashClear( pHash );
else
{
ULONG ulLen = 0;
while( ulLen < pItem->item.asHash.value->ulLen )
hb_hashDel( pHash, &pItem->item.asHash.value->pPairs[ ulLen++ ].key );
}
return TRUE;
}
}
return FALSE;
}
BOOL hb_hashAdd( PHB_ITEM pHash, PHB_ITEM pKey, PHB_ITEM pValue )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashAdd(%p,%p,%p)", pHash, pKey, pValue));
if( HB_IS_HASH( pHash ) && HB_IS_HASHKEY( pKey ) )
{
PHB_ITEM pDest = hb_hashValuePtr( pHash->item.asHash.value, pKey, TRUE );
if( pDest )
{
if( HB_IS_BYREF( pDest ) )
pDest = hb_itemUnRef( pDest );
if( pValue )
hb_itemCopyFromRef( pDest, pValue );
else
hb_itemSetNil( pDest );
return TRUE;
}
}
return FALSE;
}
BOOL hb_hashAddNew( PHB_ITEM pHash, PHB_ITEM pKey, PHB_ITEM pValue )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashAddNew(%p,%p,%p)", pHash, pKey, pValue));
if( HB_IS_HASH( pHash ) && HB_IS_HASHKEY( pKey ) )
return hb_hashNewValue( pHash->item.asHash.value, pKey, pValue );
else
return FALSE;
}
PHB_ITEM hb_hashGetKeyAt( PHB_ITEM pHash, ULONG ulPos )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashGetKeyAt(%p,%lu)", pHash, ulPos));
if( HB_IS_HASH( pHash ) && ulPos > 0 && ulPos <= pHash->item.asHash.value->ulLen )
return &pHash->item.asHash.value->pPairs[ ulPos - 1 ].key;
else
return NULL;
}
PHB_ITEM hb_hashGetValueAt( PHB_ITEM pHash, ULONG ulPos )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashGetValueAt(%p,%lu)", pHash, ulPos));
if( HB_IS_HASH( pHash ) && ulPos > 0 && ulPos <= pHash->item.asHash.value->ulLen )
return HB_IS_BYREF( &pHash->item.asHash.value->pPairs[ ulPos - 1 ].value ) ?
hb_itemUnRef( &pHash->item.asHash.value->pPairs[ ulPos - 1 ].value ) :
&pHash->item.asHash.value->pPairs[ ulPos - 1 ].value;
else
return NULL;
}
BOOL hb_hashDelAt( PHB_ITEM pHash, ULONG ulPos )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashDelAt(%p,%lu)", pHash, ulPos));
if( HB_IS_HASH( pHash ) && ulPos > 0 && ulPos <= pHash->item.asHash.value->ulLen )
{
hb_hashDelPair( pHash->item.asHash.value, ulPos - 1 );
return TRUE;
}
else
return FALSE;
}
/* retrives the hash unique ID */
void * hb_hashId( PHB_ITEM pHash )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashId(%p)", pHash));
if( HB_IS_HASH( pHash ) )
return ( void * ) pHash->item.asHash.value;
else
return NULL;
}
void hb_hashCloneBody( PHB_ITEM pHash, PHB_ITEM pDest, PHB_NESTED_CLONED pClonedList )
{
ULONG ulPos;
HB_TRACE(HB_TR_DEBUG, ("hb_hashCloneBody(%p,%p,%p)", pHash, pDest, pClonedList));
hb_hashNew( pDest );
hb_hashResize( pDest->item.asHash.value, pHash->item.asHash.value->ulSize );
pDest->item.asHash.value->iFlags = pHash->item.asHash.value->iFlags;
if( pHash->item.asHash.value->pDefault )
pDest->item.asHash.value->pDefault =
hb_itemNew( pHash->item.asHash.value->pDefault );
for( ulPos = 0; ulPos < pHash->item.asHash.value->ulLen; ++ulPos )
{
PHB_ITEM pValue = &pHash->item.asHash.value->pPairs[ ulPos ].value;
if( HB_IS_BYREF( pValue ) )
pValue = hb_itemUnRef( pValue );
hb_itemCopy( &pDest->item.asHash.value->pPairs[ ulPos ].key,
&pHash->item.asHash.value->pPairs[ ulPos ].key );
pDest->item.asHash.value->ulLen++;
hb_cloneNested( &pDest->item.asHash.value->pPairs[ ulPos ].value, pValue, pClonedList );
}
}
PHB_ITEM hb_hashClone( PHB_ITEM pHash )
{
PHB_ITEM pDest;
HB_TRACE(HB_TR_DEBUG, ("hb_hashClone(%p)", pHash));
pDest = hb_itemNew( NULL );
if( HB_IS_HASH( pHash ) )
{
PHB_NESTED_CLONED pClonedList, pCloned;
pClonedList = ( PHB_NESTED_CLONED ) hb_xgrab( sizeof( HB_NESTED_CLONED ) );
pClonedList->value = ( void * ) pHash->item.asHash.value;
pClonedList->pDest = pDest;
pClonedList->pNext = NULL;
hb_hashCloneBody( pHash, pDest, pClonedList );
do
{
pCloned = pClonedList;
pClonedList = pClonedList->pNext;
hb_xfree( pCloned );
}
while( pClonedList );
}
return pDest;
}
void hb_hashJoin( PHB_ITEM pDest, PHB_ITEM pSource, int iType )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashJoin(%p,%p,%d)", pDest, pSource, iType));
if( HB_IS_HASH( pDest ) && HB_IS_HASH( pSource ) )
{
PHB_BASEHASH pBaseHash;
ULONG ulPos;
switch( iType )
{
case HB_HASH_UNION: /* OR */
pBaseHash = pSource->item.asHash.value;
for( ulPos = 0; ulPos < pBaseHash->ulLen; ++ulPos )
{
PHB_ITEM pVal = &pBaseHash->pPairs[ ulPos ].value;
if( HB_IS_BYREF( pVal ) )
pVal = hb_itemUnRef( pVal );
hb_hashAdd( pDest, &pBaseHash->pPairs[ ulPos ].key, pVal );
}
break;
case HB_HASH_INTERSECT: /* AND */
pBaseHash = pDest->item.asHash.value;
for( ulPos = 0; ulPos < pBaseHash->ulLen; ++ulPos )
{
if( !hb_hashFind( pSource->item.asHash.value,
&pBaseHash->pPairs[ ulPos ].key, NULL ) )
hb_hashDel( pDest, &pBaseHash->pPairs[ ulPos ].key );
}
break;
case HB_HASH_DIFFERENCE: /* XOR */
pBaseHash = pSource->item.asHash.value;
for( ulPos = 0; ulPos < pBaseHash->ulLen; ++ulPos )
{
if( !hb_hashDel( pDest, &pBaseHash->pPairs[ ulPos ].key ) )
{
PHB_ITEM pVal = &pBaseHash->pPairs[ ulPos ].value;
if( HB_IS_BYREF( pVal ) )
pVal = hb_itemUnRef( pVal );
hb_hashAdd( pDest, &pBaseHash->pPairs[ ulPos ].key, pVal );
}
}
break;
case HB_HASH_REMOVE: /* NOT -> h1 & ( h1 ^ h2 ) */
pBaseHash = pSource->item.asHash.value;
if( pDest->item.asHash.value == pBaseHash )
hb_hashClear( pDest );
else
{
for( ulPos = 0; ulPos < pBaseHash->ulLen; ++ulPos )
hb_hashDel( pDest, &pBaseHash->pPairs[ ulPos ].key );
}
break;
}
}
}
PHB_ITEM hb_hashGetKeys( PHB_ITEM pHash )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashGetKeys(%p)", pHash));
if( HB_IS_HASH( pHash ) )
{
PHB_ITEM pKeys = hb_itemArrayNew( hb_hashLen( pHash ) ), pKey;
ULONG ulPos = 0;
while( ( pKey = hb_hashGetKeyAt( pHash, ++ulPos ) ) != NULL )
{
PHB_ITEM pDest = hb_arrayGetItemPtr( pKeys, ulPos );
if( !pDest )
break;
hb_itemCopy( pDest, pKey );
}
return pKeys;
}
return NULL;
}
PHB_ITEM hb_hashGetValues( PHB_ITEM pHash )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashGetValues(%p)", pHash));
if( HB_IS_HASH( pHash ) )
{
PHB_ITEM pValues = hb_itemArrayNew( hb_hashLen( pHash ) ), pVal;
ULONG ulPos = 0;
while( ( pVal = hb_hashGetValueAt( pHash, ++ulPos ) ) != NULL )
{
PHB_ITEM pDest = hb_arrayGetItemPtr( pValues, ulPos );
if( !pDest )
break;
hb_itemCopy( pDest, pVal );
}
return pValues;
}
return NULL;
}
void hb_hashSetDefault( PHB_ITEM pHash, PHB_ITEM pValue )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashSetDefault(%p,%p)", pHash, pValue));
if( HB_IS_HASH( pHash ) )
{
if( pHash->item.asHash.value->pDefault )
{
hb_itemRelease( pHash->item.asHash.value->pDefault );
pHash->item.asHash.value->pDefault = NULL;
}
if( pValue && !HB_IS_NIL( pValue ) &&
( !HB_IS_HASH( pValue ) || pHash->item.asHash.value !=
pValue->item.asHash.value ) )
pHash->item.asHash.value->pDefault = hb_itemClone( pValue );
}
}
PHB_ITEM hb_hashGetDefault( PHB_ITEM pHash )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashGetDefault(%p)", pHash));
if( HB_IS_HASH( pHash ) )
return pHash->item.asHash.value->pDefault;
else
return NULL;
}
void hb_hashSetFlags( PHB_ITEM pHash, int iFlags )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashSetFlags(%p,%d)", pHash, iFlags));
if( HB_IS_HASH( pHash ) )
pHash->item.asHash.value->iFlags |= iFlags;
}
void hb_hashClearFlags( PHB_ITEM pHash, int iFlags )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashClearFlags(%p,%d)", pHash, iFlags));
if( HB_IS_HASH( pHash ) )
pHash->item.asHash.value->iFlags &= ~iFlags;
}
int hb_hashGetFlags( PHB_ITEM pHash )
{
HB_TRACE(HB_TR_DEBUG, ("hb_hashGetFlags(%p)", pHash));
if( HB_IS_HASH( pHash ) )
return pHash->item.asHash.value->iFlags;
else
return 0;
}