text:
 # pragma once
#endif

#include "metamodel.h"                  // Base classes for the MetaModel.
#include "metadatahash.h"
#include "rwutil.h"
#include "shash.h"

#include "../heaps/export.h"
#include "../hotdata/export.h"
#include "../tables/export.h"

struct HENUMInternal;
#ifdef FEATURE_METADATA_CUSTOM_DATA_SOURCE
struct IMDCustomDataSource;
#endif

// ENUM for marking bit
enum
{
    InvalidMarkedBit            = 0x00000000,
    ModuleMarkedBit             = 0x00000001,
    TypeRefMarkedBit            = 0x00000002,
    TypeDefMarkedBit            = 0x00000004,
    FieldMarkedBit              = 0x00000008,
    MethodMarkedBit             = 0x00000010,
    ParamMarkedBit              = 0x00000020,
    MemberRefMarkedBit          = 0x00000040,
    CustomAttributeMarkedBit    = 0x00000080,
    DeclSecurityMarkedBit       = 0x00000100,
    SignatureMarkedBit          = 0x00000200,
    EventMarkedBit              = 0x00000400,
    PropertyMarkedBit           = 0x00000800,
    MethodImplMarkedBit         = 0x00001000,
    ModuleRefMarkedBit          = 0x00002000,
    TypeSpecMarkedBit           = 0x00004000,
    InterfaceImplMarkedBit      = 0x00008000,
    AssemblyRefMarkedBit        = 0x00010000,
    MethodSpecMarkedBit         = 0x00020000,

};

// entry for marking UserString
struct FilterUserStringEntry
{
    DWORD       m_tkString;
    bool        m_fMarked;
};

class FilterTable : public CDynArray<DWORD>
{
public:
    FilterTable() { m_daUserStringMarker = NULL; }
    ~FilterTable();

    __checkReturn FORCEINLINE HRESULT MarkTypeRef(mdToken tk) { return MarkToken(tk, TypeRefMarkedBit); }
    __checkReturn FORCEINLINE HRESULT MarkTypeDef(mdToken tk) { return MarkToken(tk, TypeDefMarkedBit); }
    __checkReturn FORCEINLINE HRESULT MarkField(mdToken tk) { return MarkToken(tk, FieldMarkedBit); }
    __checkReturn FORCEINLINE HRESULT MarkMethod(mdToken tk) { return MarkToken(tk, MethodMarkedBit); }
    __checkReturn FORCEINLINE HRESULT MarkParam(mdToken tk) { return MarkToken(tk, ParamMarkedBit); }
    __checkReturn FORCEINLINE HRESULT MarkMemberRef(mdToken tk) { return MarkToken(tk, MemberRefMarkedBit); }
    __checkReturn FORCEINLINE HRESULT MarkCustomAttribute(mdToken tk) { return MarkToken(tk, CustomAttributeMarkedBit); }
    __checkReturn FORCEINLINE HRESULT MarkDeclSecurity(mdToken tk) { return MarkToken(tk, DeclSecurityMarkedBit); }
    __checkReturn FORCEINLINE HRESULT MarkSignature(mdToken tk) { return MarkToken(tk, SignatureMarkedBit); }
    __checkReturn FORCEINLINE HRESULT MarkEvent(mdToken tk) { return MarkToken(tk, EventMarkedBit); }
    __checkReturn FORCEINLINE HRESULT MarkProperty(mdToken tk) { return MarkToken(tk, PropertyMarkedBit); }
    __checkReturn FORCEINLINE HRESULT MarkMethodImpl(RID rid)
    {
        return MarkToken(TokenFromRid(rid, TBL_MethodImpl << 24), MethodImplMarkedBit);
    }
    __checkReturn FORCEINLINE HRESULT MarkModuleRef(mdToken tk) { return MarkToken(tk, ModuleRefMarkedBit); }
    __checkReturn FORCEINLINE HRESULT MarkTypeSpec(mdToken tk) { return MarkToken(tk, TypeSpecMarkedBit); }
    __checkReturn FORCEINLINE HRESULT MarkInterfaceImpl(mdToken tk) { return MarkToken(tk, InterfaceImplMarkedBit); }
    __checkReturn FORCEINLINE HRESULT MarkAssemblyRef(mdToken tk) { return MarkToken(tk, AssemblyRefMarkedBit); }
    __checkReturn FORCEINLINE HRESULT MarkMethodSpec(mdToken tk) { return MarkToken(tk, MethodSpecMarkedBit); }

    // It may look inconsistent but it is because taht UserString an offset to the heap.
    // We don't want to grow the FilterTable to the size of the UserString heap.
    // So we use the heap's marking system instead...
    //
    __checkReturn HRESULT MarkUserString(mdString str);

    __checkReturn HRESULT MarkNewUserString(mdString str);

    FORCEINLINE bool IsTypeRefMarked(mdToken tk)    { return IsTokenMarked(tk, TypeRefMarkedBit); }
    FORCEINLINE bool IsTypeDefMarked(mdToken tk) { return IsTokenMarked(tk, TypeDefMarkedBit); }
    FORCEINLINE bool IsFieldMarked(mdToken tk) { return IsTokenMarked(tk, FieldMarkedBit); }
    FORCEINLINE bool IsMethodMarked(mdToken tk) { return IsTokenMarked(tk, MethodMarkedBit); }
    FORCEINLINE bool IsParamMarked(mdToken tk) { return IsTokenMarked(tk, ParamMarkedBit); }
    FORCEINLINE bool IsMemberRefMarked(mdToken tk) { return IsTokenMarked(tk, MemberRefMarkedBit); }
    FORCEINLINE bool IsCustomAttributeMarked(mdToken tk) { return IsTokenMarked(tk, CustomAttributeMarkedBit); }
    FORCEINLINE bool IsDeclSecurityMarked(mdToken tk) { return IsTokenMarked(tk, DeclSecurityMarkedBit); }
    FORCEINLINE bool IsSignatureMarked(mdToken tk) { return IsTokenMarked(tk, SignatureMarkedBit); }
    FORCEINLINE bool IsEventMarked(mdToken tk) { return IsTokenMarked(tk, EventMarkedBit); }
    FORCEINLINE bool IsPropertyMarked(mdToken tk) { return IsTokenMarked(tk, PropertyMarkedBit); }
    FORCEINLINE bool IsMethodImplMarked(RID rid)
    {
        return IsTokenMarked(TokenFromRid(rid, TBL_MethodImpl << 24), MethodImplMarkedBit);
    }
    FORCEINLINE bool IsModuleRefMarked(mdToken tk) { return IsTokenMarked(tk, ModuleRefMarkedBit); }
    FORCEINLINE bool IsTypeSpecMarked(mdToken tk) { return IsTokenMarked(tk, TypeSpecMarkedBit); }
    FORCEINLINE bool IsInterfaceImplMarked(mdToken tk){ return IsTokenMarked(tk, InterfaceImplMarkedBit); }
    FORCEINLINE bool IsAssemblyRefMarked(mdToken tk){ return IsTokenMarked(tk, AssemblyRefMarkedBit); }
    FORCEINLINE bool IsMethodSpecMarked(mdToken tk){ return IsTokenMarked(tk, MethodSpecMarkedBit); }

    bool IsUserStringMarked(mdString str);

    __checkReturn HRESULT UnmarkAll(CMiniMdRW *pMiniMd, ULONG ulSize);
    __checkReturn HRESULT MarkAll(CMiniMdRW *pMiniMd, ULONG ulSize);
    bool IsTokenMarked(mdToken);

    __checkReturn FORCEINLINE HRESULT UnmarkTypeDef(mdToken tk) { return UnmarkToken(tk, TypeDefMarkedBit); }
    __checkReturn FORCEINLINE HRESULT UnmarkField(mdToken tk) { return UnmarkToken(tk, FieldMarkedBit); }
    __checkReturn FORCEINLINE HRESULT UnmarkMethod(mdToken tk) { return UnmarkToken(tk, MethodMarkedBit); }
    __checkReturn FORCEINLINE HRESULT UnmarkCustomAttribute(mdToken tk) { return UnmarkToken(tk, CustomAttributeMarkedBit); }

private:
    CDynArray<FilterUserStringEntry> *m_daUserStringMarker;
    bool            IsTokenMarked(mdToken tk, DWORD bitMarked);
    __checkReturn HRESULT         MarkToken(mdToken tk, DWORD bit);
    __checkReturn HRESULT         UnmarkToken(mdToken tk, DWORD bit);
}; // class FilterTable : public CDynArray<DWORD>

class CMiniMdRW;

//*****************************************************************************
// This class is used to keep a list of RID. This list of RID can be sorted
// base on the m_ixCol's value of the m_ixTbl table.
//*****************************************************************************
class VirtualSort
{
public:
    void Init(ULONG ixTbl, ULONG ixCol, CMiniMdRW *pMiniMd);
    void Uninit();
    TOKENMAP    *m_pMap;                // RID for m_ixTbl table. Sorted by on the ixCol
    bool        m_isMapValid;
    ULONG       m_ixTbl;                // Table this is a sorter for.
    ULONG       m_ixCol;                // Key column in the table.
    CMiniMdRW   *m_pMiniMd;             // The MiniMd with the data.
    __checkReturn
    HRESULT Sort();
private:
    mdToken     m_tkBuf;
    __checkReturn
    HRESULT SortRange(int iLeft, int iRight);
public:
    __checkReturn
    HRESULT Compare(
        RID  iLeft,         // First item to compare.
        RID  iRight,        // Second item to compare.
        int *pnResult);     // -1, 0, or 1

private:
    FORCEINLINE void Swap(
        RID         iFirst,
        RID         iSecond)
    {
        if ( iFirst == iSecond ) return;
        m_tkBuf = *(m_pMap->Get(iFirst));
        *(m_pMap->Get(iFirst)) = *(m_pMap->Get(iSecond));
        *(m_pMap->Get(iSecond)) = m_tkBuf;
    }

}; // class VirtualSort

class ReorderData
{
public:
    typedef enum
    {
        MinReorderBucketType=0,  // bucket# shouldn't be less than this value
        Undefined=0,             // use this for initialization
        Duplicate=1,             // duplicate string
        ProfileData=2,           // bucket# for IBC data
        PublicData=3,            // bucket# for public data
        OtherData=4,             // bucket# for other data
        NonPublicData=5,         // bucket# for non-public data
        MaxReorderBucketType=255 // bucket# shouldn't exceeed this value
    } ReorderBucketType;
};

typedef CMetaDataHashBase CMemberRefHash;
typedef CMetaDataHashBase CLookUpHash;

class MDTOKENMAP;
class MDInternalRW;
class CorProfileData;
class UTSemReadWrite;

enum MetaDataReorderingOptions {
    NoReordering=0x0,
    ReArrangeStringPool=0x1
};

#ifdef FEATURE_PREJIT

// {0702E333-8D64-4ca7-B564-4AA56B1FCEA3}
EXTERN_GUID(IID_IMetaDataCorProfileData, 0x702e333, 0x8d64, 0x4ca7, 0xb5, 0x64, 0x4a, 0xa5, 0x6b, 0x1f, 0xce, 0xa3 );

#undef  INTERFACE
#define INTERFACE IMetaDataCorProfileData
DECLARE_INTERFACE_(IMetaDataCorProfileData, IUnknown)
{
    STDMETHOD(SetCorProfileData)(
        CorProfileData *pProfileData) PURE;         // [IN] Pointer to profile data
};

// {2B464817-C0F6-454e-99E7-C352D8384D7B}
EXTERN_GUID(IID_IMDInternalMetadataReorderingOptions, 0x2B464817, 0xC0F6, 0x454e, 0x99, 0xE7, 0xC3, 0x52, 0xD8, 0x38, 0x4D, 0x7B );

#undef  INTERFACE
#define INTERFACE IMDInternalMetadataReorderingOptions
DECLARE_INTERFACE_(IMDInternalMetadataReorderingOptions, IUnknown)
{
    STDMETHOD(SetMetaDataReorderingOptions)(
        MetaDataReorderingOptions options) PURE;         // [IN] metadata reordering options
};

#endif //FEATURE_PREJIT

template <class MiniMd> class CLiteWeightStgdb;
//*****************************************************************************
// Read/Write MiniMd.
//*****************************************************************************
class CMiniMdRW : public CMiniMdTemplate<CMiniMdRW>
{
public:
    friend class CLiteWeightStgdb<CMiniMdRW>;
    friend class CLiteWeightStgdbRW;
    friend class CMiniMdTemplate<CMiniMdRW>;
    friend class CQuickSortMiniMdRW;
    friend class VirtualSort;
    friend class MDInternalRW;
    friend class RegMeta;
    friend class FilterTable;
    friend class ImportHelper;
    friend class VerifyLayoutsMD;

    CMiniMdRW();
    ~CMiniMdRW();

    __checkReturn
    HRESULT InitNew();
    __checkReturn
    HRESULT InitOnMem(const void *pBuf, ULONG ulBufLen, int bReadOnly);
    __checkReturn
    HRESULT PostInit(int iLevel);
    __checkReturn
    HRESULT InitPoolOnMem(int iPool, void *pbData, ULONG cbData, int bReadOnly);
    __checkReturn
    HRESULT InitOnRO(CMiniMd *pMd, int bReadOnly);
#ifdef FEATURE_METADATA_CUSTOM_DATA_SOURCE
    __checkReturn
    HRESULT InitOnCustomDataSource(IMDCustomDataSource* pDataSouce);
#endif
    __checkReturn
    HRESULT ConvertToRW();

    __checkReturn
    HRESULT GetSaveSize(
        CorSaveSize               fSave,
        UINT32                   *pcbSize,
        DWORD                    *pbCompressed,
        MetaDataReorderingOptions reorderingOptions = NoReordering,
        CorProfileData           *pProfileData = NULL);
    int IsPoolEmpty(int iPool);
    __checkReturn
    HRESULT GetPoolSaveSize(int iPool, UINT32 *pcbSize);

    __checkReturn
    HRESULT SaveTablesToStream(IStream *pIStream, MetaDataReorderingOptions reorderingOptions, CorProfileData *pProfileData);
    __checkReturn
    HRESULT SavePoolToStream(int iPool, IStream *pIStream);
    __checkReturn
    HRESULT SaveDone();

    __checkReturn
    HRESULT SetHandler(IUnknown *pIUnk);

    __checkReturn
    HRESULT SetOption(OptionValue *pOptionValue);
    __checkReturn
    HRESULT GetOption(OptionValue *pOptionValue);

    static ULONG GetTableForToken(mdToken tkn);
    static mdToken GetTokenForTable(ULONG ixTbl);

    FORCEINLINE static ULONG TblFromRecId(ULONG ul) { return (ul >> 24)&0x7f; }
    FORCEINLINE static ULONG RidFromRecId(ULONG ul) { return ul & 0xffffff; }
    FORCEINLINE static ULONG RecIdFromRid(ULONG rid, ULONG ixTbl) { return rid | ((ixTbl|0x80) << 24); }
    FORCEINLINE static int IsRecId(ULONG ul) { return (ul & 0x80000000) != 0;}

    // Place in every API function before doing any allocations.
    __checkReturn
    FORCEINLINE HRESULT PreUpdate()
    {
        if (m_eGrow == eg_grow)
        {
            return ExpandTables();
        }
        return S_OK;
    }

    __checkReturn
    HRESULT AddRecord(
        UINT32 nTableIndex,
        void **ppRow,
        RID   *pRid);

    __checkReturn
    FORCEINLINE HRESULT PutCol(ULONG ixTbl, ULONG ixCol, void *pRecord, ULONG uVal)
    {   _ASSERTE(ixTbl < TBL_COUNT); _ASSERTE(ixCol < m_TableDefs[ixTbl].m_cCols);
        return PutCol(m_TableDefs[ixTbl].m_pColDefs[ixCol], pRecord, uVal);
    } // HRESULT CMiniMdRW::PutCol()
    __checkReturn
    HRESULT PutString(ULONG ixTbl, ULONG ixCol, void *pRecord, LPCSTR szString);
    __checkReturn
    HRESULT PutStringW(ULONG ixTbl, ULONG ixCol, void *pRecord, LPCWSTR wszString);
    __checkReturn
    HRESULT PutGuid(ULONG ixTbl, ULONG ixCol, void *pRecord, REFGUID guid);
    __checkReturn
    HRESULT ChangeMvid(REFGUID newMvid);
    __checkReturn
    HRESULT PutToken(ULONG ixTbl, ULONG ixCol, void *pRecord, mdToken tk);
    __checkReturn
    HRESULT PutBlob(ULONG ixTbl, ULONG ixCol, void *pRecord, const void *pvData, ULONG cbData);

    __checkReturn
    HRESULT PutUserString(MetaData::DataBlob data, UINT32 *pnIndex)
    { return m_UserStringHeap.AddBlob(data, pnIndex); }

    ULONG GetCol(ULONG ixTbl, ULONG ixCol, void *pRecord);
    mdToken GetToken(ULONG ixTbl, ULONG ixCol, void *pRecord);

    // Add a record to a table, and return a typed XXXRec *.
//  #undef AddTblRecord
    #define AddTblRecord(tbl) \
        __checkReturn HRESULT Add##tbl##Record(tbl##Rec **ppRow, RID *pnRowIndex)   \
        {   return AddRecord(TBL_##tbl, reinterpret_cast<void **>(ppRow), pnRowIndex); }

    AddTblRecord(Module)
    AddTblRecord(TypeRef)
    __checkReturn HRESULT AddTypeDefRecord( // Specialized implementation.
        TypeDefRec **ppRow,
        RID         *pnRowIndex);
    AddTblRecord(Field)
    __checkReturn HRESULT AddMethodRecord(  // Specialized implementation.
        MethodRec **ppRow,
        RID        *pnRowIndex);
    AddTblRecord(Param)
    AddTblRecord(InterfaceImpl)
    AddTblRecord(MemberRef)
    AddTblRecord(Constant)
    AddTblRecord(CustomAttribute)
    AddTblRecord(FieldMarshal)
    AddTblRecord(DeclSecurity)
    AddTblRecord(ClassLayout)
    AddTblRecord(FieldLayout)
    AddTblRecord(StandAloneSig)
    __checkReturn HRESULT AddEventMapRecord(    // Specialized implementation.
        EventMapRec **ppRow,
        RID          *pnRowIndex);
    AddTblRecord(Event)
    __checkReturn HRESULT AddPropertyMapRecord( // Specialized implementation.
        PropertyMapRec **ppRow,
        RID             *pnRowIndex);
    AddTblRecord(Property)
    AddTblRecord(MethodSemantics)
    AddTblRecord(MethodImpl)
    AddTblRecord(ModuleRef)
    AddTblRecord(FieldPtr)
    AddTblRecord(MethodPtr)
    AddTblRecord(ParamPtr)
    AddTblRecord(PropertyPtr)
    AddTblRecord(EventPtr)

    AddTblRecord(ENCLog)
    AddTblRecord(TypeSpec)
    AddTblRecord(ImplMap)
    AddTblRecord(ENCMap)
    AddTblRecord(FieldRVA)

    // Assembly Tables.
    AddTblRecord(Assembly)
    AddTblRecord(AssemblyProcessor)
    AddTblRecord(AssemblyOS)
    AddTblRecord(AssemblyRef)
    AddTblRecord(AssemblyRefProcessor)
    AddTblRecord(AssemblyRefOS)
    AddTblRecord(File)
    AddTblRecord(ExportedType)
    AddTblRecord(ManifestResource)

    AddTblRecord(NestedClass)
    AddTblRecord(GenericParam)
    AddTblRecord(MethodSpec)
    AddTblRecord(GenericParamConstraint)

    // Specialized AddXxxToYyy() functions.
    __checkReturn HRESULT AddMethodToTypeDef(RID td, RID md);
    __checkReturn HRESULT AddFieldToTypeDef(RID td, RID md);
    __checkReturn HRESULT AddParamToMethod(RID md, RID pd);
    __checkReturn HRESULT AddPropertyToPropertyMap(RID pmd, RID pd);
    __checkReturn HRESULT AddEventToEventMap(ULONG emd, RID ed);

    // does the MiniMdRW has the indirect tables, such as FieldPtr, MethodPtr
    FORCEINLINE int HasIndirectTable(ULONG ix)
    { if (g_PtrTableIxs[ix].m_ixtbl < TBL_COUNT) return GetCountRecs(g_PtrTableIxs[ix].m_ixtbl); return 0;}

    FORCEINLINE int IsVsMapValid(ULONG ixTbl)
    { _ASSERTE(ixTbl<TBL_COUNT); return (m_pVS[ixTbl] && m_pVS[ixTbl]->m_isMapValid); }

    // translate index returned by getMethodListOfTypeDef to a rid into Method table
    __checkReturn
    FORCEINLINE HRESULT GetMethodRid(ULONG index, ULONG *pRid)
    {
        HRESULT hr;
        if (HasIndirectTable(TBL_Method))
        {
            MethodPtrRec *pMethodPtrRecord;
            IfFailGo(GetMethodPtrRecord(index, &pMethodPtrRecord));
            *pRid = getMethodOfMethodPtr(pMethodPtrRecord);
        }
        else
        {
            *pRid = index;
        }
        return S_OK;
    ErrExit:
        *pRid = 0;
        return hr;
    }

    // translate index returned by getFieldListOfTypeDef to a rid into Field table
    __checkReturn
    FORCEINLINE HRESULT GetFieldRid(ULONG index, ULONG *pRid)
    {
        HRESULT hr;
        if (HasIndirectTable(TBL_Field))
        {
            FieldPtrRec *pFieldPtrRecord;
            IfFailGo(GetFieldPtrRecord(index, &pFieldPtrRecord));
            *pRid = getFieldOfFieldPtr(pFieldPtrRecord);
        }
        else
        {
            *pRid = index;
        }
        return S_OK;
    ErrExit:
        *pRid = 0;
        return hr;
    }

    // translate index returned by getParamListOfMethod to a rid into Param table
    __checkReturn
    FORCEINLINE HRESULT GetParamRid(ULONG index, ULONG *pRid)
    {
        HRESULT hr;
        if (HasIndirectTable(TBL_Param))
        {
            ParamPtrRec *pParamPtrRecord;
            IfFailGo(GetParamPtrRecord(index, &pParamPtrRecord));
            *pRid = getParamOfParamPtr(pParamPtrRecord);
        }
        else
        {
            *pRid = index;
        }
        return S_OK;
    ErrExit:
        *pRid = 0;
        return hr;
    }

    // translate index returned by getEventListOfEventMap to a rid into Event table
    __checkReturn
    FORCEINLINE HRESULT GetEventRid(ULONG index, ULONG *pRid)
    {
        HRESULT hr;
        if (HasIndirectTable(TBL_Event))
        {
            EventPtrRec *pEventPtrRecord;
            IfFailGo(GetEventPtrRecord(index, &pEventPtrRecord));
            *pRid = getEventOfEventPtr(pEventPtrRecord);
        }
        else
        {
            *pRid = index;
        }
        return S_OK;
    ErrExit:
        *pRid = 0;
        return hr;
    }

    // translate index returned by getPropertyListOfPropertyMap to a rid into Property table
    __checkReturn
    FORCEINLINE HRESULT GetPropertyRid(ULONG index, ULONG *pRid)
    {
        HRESULT hr;
        if (HasIndirectTable(TBL_Property))
        {
            PropertyPtrRec *pPropertyPtrRecord;
            IfFailGo(GetPropertyPtrRecord(index, &pPropertyPtrRecord));
            *pRid = getPropertyOfPropertyPtr(pPropertyPtrRecord);
        }
        else
        {
            *pRid = index;
        }
        return S_OK;
    ErrExit:
        *pRid = 0;
        return hr;
    }

    // Convert a pseudo-RID from a Virtual Sort into a real RID.
    FORCEINLINE ULONG GetRidFromVirtualSort(ULONG ixTbl, ULONG index)
    { return IsVsMapValid(ixTbl) ? *(m_pVS[ixTbl]->m_pMap->Get(index)) : index; }

    // Index returned by GetInterfaceImplForTypeDef. It could be index to VirtualSort table
    // or directly to InterfaceImpl
    FORCEINLINE ULONG GetInterfaceImplRid(ULONG index)
    { return GetRidFromVirtualSort(TBL_InterfaceImpl, index); }

    // Index returned by GetGenericParamForToken. It could be index to VirtualSort table
    // or directly to GenericParam
    FORCEINLINE ULONG GetGenericParamRid(ULONG index)
    { return GetRidFromVirtualSort(TBL_GenericParam, index); }

    // Index returned by GetGenericParamConstraintForToken. It could be index to VirtualSort table
    // or directly to GenericParamConstraint
    FORCEINLINE ULONG GetGenericParamConstraintRid(ULONG index)
    { return GetRidFromVirtualSort(TBL_GenericParamConstraint, index); }

    // Index returned by GetDeclSecurityForToken. It could be index to VirtualSort table
    // or directly to DeclSecurity
    FORCEINLINE ULONG GetDeclSecurityRid(ULONG index)
    { return GetRidFromVirtualSort(TBL_DeclSecurity, index); }

    // Index returned by GetCustomAttributeForToken. It could be index to VirtualSort table
    // or directly to CustomAttribute
    FORCEINLINE ULONG GetCustomAttributeRid(ULONG index)
    { return GetRidFromVirtualSort(TBL_CustomAttribute, index); }

    // add method, field, property, event, param to the map table
    __checkReturn HRESULT AddMethodToLookUpTable(mdMethodDef md, mdTypeDef td);
    __checkReturn HRESULT AddFieldToLookUpTable(mdFieldDef fd, mdTypeDef td);
    __checkReturn HRESULT AddPropertyToLookUpTable(mdProperty pr, mdTypeDef td);
    __checkReturn HRESULT AddEventToLookUpTable(mdEvent ev, mdTypeDef td);
    __checkReturn HRESULT AddParamToLookUpTable(mdParamDef pd, mdMethodDef md);

    // look up the parent of method, field, property, event, or param
    __checkReturn HRESULT FindParentOfMethodHelper(mdMethodDef md, mdTypeDef *ptd);
    __checkReturn HRESULT FindParentOfFieldHelper(mdFieldDef fd, mdTypeDef *ptd);
    __checkReturn HRESULT FindParentOfPropertyHelper(mdProperty pr, mdTypeDef *ptd);
    __checkReturn HRESULT FindParentOfEventHelper(mdEvent ev, mdTypeDef *ptd);
    __checkReturn HRESULT FindParentOfParamHelper(mdParamDef pd, mdMethodDef *pmd);

    bool IsMemberDefHashPresent() { return m_pMemberDefHash != NULL; }

    // Function to reorganize the string pool based on IBC profile data (if available) and static analysis.
    // Throws on error.
    VOID OrganizeStringPool(CorProfileData *pProfileData);

    // Result of hash search
    enum HashSearchResult
    {
        Found,      // Item was found.
        NotFound,   // Item not found.
        NoTable     // Table hasn't been built.
    };

    // Create MemberRef hash table.
    __checkReturn
    HRESULT CreateMemberRefHash();

    // Add a new MemberRef to the hash table.
    __checkReturn
    HRESULT AddMemberRefToHash(             // Return code.
        mdMemberRef mr);                    // Token of new guy.

    // If the hash is built, search for the item. Ignore token *ptkMemberRef.
    HashSearchResult FindMemberRefFromHash(
        mdToken         tkParent,       // Parent token.
        LPCUTF8         szName,         // Name of item.
        PCCOR_SIGNATURE pvSigBlob,      // Signature.
        ULONG           cbSigBlob,      // Size of signature.
        mdMemberRef *   ptkMemberRef);  // IN: Ignored token. OUT: Return if found.

    //*************************************************************************
    // Check a given mr token to see if this one is a match.
    //*************************************************************************
    __checkReturn
    HRESULT CompareMemberRefs(              // S_OK match, S_FALSE no match.
        mdMemberRef mr,                     // Token to check.
        mdToken     tkPar,                  // Parent token.
        LPCUTF8     szNameUtf8,             // Name of item.
        PCCOR_SIGNATURE pvSigBlob,          // Signature.
        ULONG       cbSigBlob);             // Size of signature.

    // Add a new MemberDef to the hash table.
    __checkReturn
    HRESULT AddMemberDefToHash(
        mdToken tkMember,   // Token of new guy. It can be MethodDef or FieldDef
        mdToken tkParent);  // Parent token.

    // Create MemberDef Hash
    __checkReturn
    HRESULT CreateMemberDefHash();

    // If the hash is built, search for the item. Ignore token *ptkMember.
    HashSearchResult FindMemberDefFromHash(
        mdToken         tkParent,   // Parent token.
        LPCUTF8         szName,     // Name of item.
        PCCOR_SIGNATURE pvSigBlob,  // Signature.
        ULONG           cbSigBlob,  // Size of signature.
        mdToken *       ptkMember); // IN: Ignored token. OUT: Return if found. It can be MethodDef or FieldDef

    //*************************************************************************
    // Check a given Method/Field token to see if this one is a match.
    //*************************************************************************
    __checkReturn
    HRESULT CompareMemberDefs(              // S_OK match, S_FALSE no match.
        mdToken     tkMember,               // Token to check. It can be MethodDef or FieldDef
        mdToken     tkParent,               // Parent token recorded in the hash entry
        mdToken     tkPar,                  // Parent token.
        LPCUTF8     szNameUtf8,             // Name of item.
        PCCOR_SIGNATURE pvSigBlob,          // Signature.
        ULONG       cbSigBlob);             // Size of signature.

    //*************************************************************************
    // Add a new CustomAttributes to the hash table.
    //*************************************************************************
    __checkReturn
    HRESULT AddCustomAttributesToHash(      // Return code.
        mdCustomAttribute     cv)           // Token of new guy.
    { return GenericAddToHash(TBL_CustomAttribute, CustomAttributeRec::COL_Parent, RidFromToken(cv)); }

    inline ULONG HashMemberRef(mdToken tkPar, LPCUTF8 szName)
    {
        ULONG l = HashBytes((const BYTE *) &tkPar, sizeof(mdToken)) + HashStringA(szName);
        return (l);
    }

    inline ULONG HashMemberDef(mdToken tkPar, LPCUTF8 szName)
    {
        return HashMemberRef(tkPar, szName);
    }

    // helper to calculate the hash value given a token
    inline ULONG HashCustomAttribute(mdToken tkObject)
    {
        return HashToken(tkObject);
    }

    CMemberRefHash *m_pMemberRefHash;

    // Hash table for Methods and Fields
    CMemberDefHash *m_pMemberDefHash;

    // helper to calculate the hash value given a pair of tokens
    inline ULONG HashToken(mdToken tkObject)
    {
        ULONG l = HashBytes((const BYTE *) &tkObject, sizeof(mdToken));
        return (l);
    }

    //*************************************************************************
    // Add a new FieldMarhsal Rid to the hash table.
    //*************************************************************************
    __checkReturn
    HRESULT AddFieldMarshalToHash(          // Return code.
        RID         rid)                    // Token of new guy.
    { return GenericAddToHash(TBL_FieldMarshal, FieldMarshalRec::COL_Parent, rid); }

    //*************************************************************************
    // Add a new Constant Rid to the hash table.
    //*************************************************************************
    __checkReturn
    HRESULT AddConstantToHash(              // Return code.
        RID         rid)                    // Token of new guy.
    { return GenericAddToHash(TBL_Constant, ConstantRec::COL_Parent, rid); }

    //*************************************************************************
    // Add a new MethodSemantics Rid to the hash table.
    //*************************************************************************
    __checkReturn
    HRESULT AddMethodSemanticsToHash(       // Return code.
        RID         rid)                    // Token of new guy.
    { return GenericAddToHash(TBL_MethodSemantics, MethodSemanticsRec::COL_Association, rid); }

    //*************************************************************************
    // Add a new ClassLayout Rid to the hash table.
    //*************************************************************************
    __checkReturn
    HRESULT AddClassLayoutToHash(           // Return code.
        RID         rid)                    // Token of new guy.
    { return GenericAddToHash(TBL_ClassLayout, ClassLayoutRec::COL_Parent, rid); }

    //*************************************************************************
    // Add a new FieldLayout Rid to the hash table.
    //*************************************************************************
    __checkReturn
    HRESULT AddFieldLayoutToHash(           // Return code.
        RID         rid)                    // Token of new guy.
    { return GenericAddToHash(TBL_FieldLayout, FieldLayoutRec::COL_Field, rid); }

    //*************************************************************************
    // Add a new ImplMap Rid to the hash table.
    //*************************************************************************
    __checkReturn
    HRESULT AddImplMapToHash(               // Return code.
        RID         rid)                    // Token of new guy.
    { return GenericAddToHash(TBL_ImplMap, ImplMapRec::COL_MemberForwarded, rid); }

    //*************************************************************************
    // Add a new FieldRVA Rid to the hash table.
    //*************************************************************************
    __checkReturn
    HRESULT AddFieldRVAToHash(              // Return code.
        RID         rid)                    // Token of new guy.
    { return GenericAddToHash(TBL_FieldRVA, FieldRVARec::COL_Field, rid); }

    //*************************************************************************
    // Add a new nested class Rid to the hash table.
    //*************************************************************************
    __checkReturn
    HRESULT AddNestedClassToHash(           // Return code.
        RID         rid)                    // Token of new guy.
    { return GenericAddToHash(TBL_NestedClass, NestedClassRec::COL_NestedClass, rid); }

    //*************************************************************************
    // Add a new MethodImpl Rid to the hash table.
    //*************************************************************************
    __checkReturn
    HRESULT AddMethodImplToHash(           // Return code.
        RID         rid)                    // Token of new guy.
    { return GenericAddToHash(TBL_MethodImpl, MethodImplRec::COL_Class, rid); }

    //*************************************************************************
    // Build a hash table for the specified table if the size exceed the thresholds.
    //*************************************************************************
    __checkReturn
    HRESULT GenericBuildHashTable(          // Return code.
        ULONG       ixTbl,                  // Table with hash
        ULONG       ixCol);                 // col that we hash.

    //*************************************************************************
    // Add a rid from a table into a hash
    //*************************************************************************
    __checkReturn
    HRESULT GenericAddToHash(               // Return code.
        ULONG       ixTbl,                  // Table with hash
        ULONG       ixCol,                  // col that we hash.
        RID         rid);                   // new row of the table.

    //*************************************************************************
    // Add a rid from a table into a hash
    //*************************************************************************
    __checkReturn
    HRESULT GenericFindWithHash(                // Return code.
        ULONG       ixTbl,                  // Table with hash
        ULONG       ixCol,                  // col that we hash.
        mdToken     tkTarget,               // token to be find in the hash
        RID        *pFoundRid);

    // look up hash table for tokenless tables.
    // They are constant, FieldMarshal, MethodSemantics, ClassLayout, FieldLayout, ImplMap, FieldRVA, NestedClass, and MethodImpl
    CLookUpHash * m_pLookUpHashs[TBL_COUNT];

#if !defined(DACCESS_COMPILE)
    MapSHash<UINT32, UINT32> m_StringPoolOffsetHash;
#endif

    //*************************************************************************
    // Hash for named items.
    //*************************************************************************
    __checkReturn
    HRESULT AddNamedItemToHash(             // Return code.
        ULONG       ixTbl,                  // Table with the new item.
        mdToken     tk,                     // Token of new guy.
        LPCUTF8     szName,                 // Name of item.
        mdToken     tkParent);              // Token of parent, if any.

    HashSearchResult FindNamedItemFromHash(
        ULONG     ixTbl,    // Table with the item.
        LPCUTF8   szName,   // Name of item.
        mdToken   tkParent, // Token of parent, if any.
        mdToken * ptk);     // Return if found.

    __checkReturn
    HRESULT CompareNamedItems(              // S_OK match, S_FALSE no match.
        ULONG       ixTbl,                  // Table with the item.
        mdToken     tk,                     // Token to check.
        LPCUTF8     szName,                 // Name of item.
        mdToken     tkParent);              // Token of parent, if any.

    FORCEINLINE ULONG HashNamedItem(mdToken tkPar, LPCUTF8 szName)
    {   return HashBytes((const BYTE *) &tkPar, sizeof(mdToken)) + HashStringA(szName); }

    CMetaDataHashBase *m_pNamedItemHash;

    //*****************************************************************************
    // IMetaModelCommon - RW specific versions for some of the functions.
    //*****************************************************************************
    __checkReturn
    virtual HRESULT CommonGetEnclosingClassOfTypeDef(
        mdTypeDef  td,
        mdTypeDef *ptkEnclosingTypeDef)
    {
        _ASSERTE(ptkEnclosingTypeDef != NULL);

        HRESULT hr;
        NestedClassRec *pRec;
        RID         iRec;

        IfFailRet(FindNestedClassHelper(td, &iRec));
        if (iRec == 0)
        {
            *ptkEnclosingTypeDef = mdTypeDefNil;
            return S_OK;
        }

        IfFailRet(GetNestedClassRecord(iRec, &pRec));
        *ptkEnclosingTypeDef = getEnclosingClassOfNestedClass(pRec);
        return S_OK;
    }

    __checkReturn
    HRESULT CommonEnumCustomAttributeByName( // S_OK or error.
        mdToken     tkObj,                  // [IN] Object with Custom Attribute.
        LPCUTF8     szName,                 // [IN] Name of desired Custom Attribute.
        bool        fStopAtFirstFind,       // [IN] just find the first one
        HENUMInternal* phEnum);             // enumerator to fill up

    __checkReturn
    HRESULT CommonGetCustomAttributeByNameEx( // S_OK or error.
        mdToken            tkObj,             // [IN] Object with Custom Attribute.
        LPCUTF8            szName,            // [IN] Name of desired Custom Attribute.
        mdCustomAttribute *ptkCA,             // [OUT] put custom attribute token here
        const void       **ppData,            // [OUT] Put pointer to data here.
        ULONG             *pcbData);          // [OUT] Put size of data here.

    //*****************************************************************************
    // Find helper for a constant.
    //*****************************************************************************
    __checkReturn
    HRESULT FindConstantHelper(         // return index to the constant table
        mdToken  tkParent,              // Parent token. Can be ParamDef, FieldDef, or Property.
        RID     *pFoundRid);

    //*****************************************************************************
    // Find helper for a FieldMarshal.
    //*****************************************************************************
    __checkReturn
    HRESULT FindFieldMarshalHelper(     // return index to the field marshal table
        mdToken tkParent,               // Parent token. Can be a FieldDef or ParamDef.
        RID    *pFoundRid);

    //*****************************************************************************
    // Find helper for a method semantics.
    //*****************************************************************************
    __checkReturn
    HRESULT FindMethodSemanticsHelper(      // return HRESULT
        mdToken     tkAssociate,            // Event or property token
        HENUMInternal *phEnum);             // fill in the enum

    //*****************************************************************************
    // Find helper for a method semantics given a associate and semantics.
    // This will look up methodsemantics based on its status!
    // Return CLDB_E_RECORD_NOTFOUND if cannot find the matching one
    //*****************************************************************************
    __checkReturn
    HRESULT FindAssociateHelper(// return HRESULT
        mdToken     tkAssociate,            // Event or property token
        DWORD       dwSemantics,            // [IN] given a associate semantics(setter, getter, testdefault, reset)
        RID         *pRid);                 // [OUT] return matching row index here

    //*****************************************************************************
    // Find helper for a MethodImpl.
    //*****************************************************************************
    __checkReturn
    HRESULT FindMethodImplHelper(// return HRESULT
        mdTypeDef   td,                     // TypeDef token for the Class.
        HENUMInternal *phEnum);             // fill in the enum

    //*****************************************************************************
    // Find helper for a GenericParams
    //*****************************************************************************
    __checkReturn
    HRESULT FindGenericParamHelper(         // Return HRESULT
        mdToken     tkOwner,                // Token for the GenericParams' owner
        HENUMInternal *phEnum);             // Fill in the enum.

    //*****************************************************************************
    // Find helper for a Generic Constraints
    //*****************************************************************************
    __checkReturn
    HRESULT FindGenericParamConstraintHelper(    // Return HRESULT
        mdGenericParam tkParam,             // Token for the GenericParam
        HENUMInternal *phEnum);             // Fill in the enum.

    //*****************************************************************************
    // Find helper for a ClassLayout.
    //*****************************************************************************
    __checkReturn
    HRESULT FindClassLayoutHelper(      // return index to the ClassLayout table
        mdTypeDef tkParent,             // Parent token.
        RID      *pFoundRid);

    //*****************************************************************************
    // Find helper for a FieldLayout.
    //*****************************************************************************
    __checkReturn
    HRESULT FindFieldLayoutHelper(  // return index to the FieldLayout table
        mdFieldDef tkField,         // Token for the field.
        RID       *pFoundRid);

    //*****************************************************************************
    // Find helper for a ImplMap.
    //*****************************************************************************
    __checkReturn
    HRESULT FindImplMapHelper(  // return index to the constant table
        mdToken tk,             // Member forwarded token.
        RID    *pFoundRid);

    //*****************************************************************************
    // Find helper for a FieldRVA.
    //*****************************************************************************
    __checkReturn
    HRESULT FindFieldRVAHelper(     // return index to the FieldRVA table
        mdFieldDef tkField,         // Token for the field.
        RID       *pFoundRid);

    //*****************************************************************************
    // Find helper for a NestedClass.
    //*****************************************************************************
    __checkReturn
    HRESULT FindNestedClassHelper(  // return index to the NestedClass table
        mdTypeDef tkClass,          // Token for the NestedClass.
        RID      *pFoundRid);

    //*****************************************************************************
    // IMPORTANT!!!!!!!! Use these set of functions if you are dealing with RW rather
    // getInterfaceImplsForTypeDef, getDeclSecurityForToken, etc.
    // The following functions can deal with these tables when they are not sorted and
    // build the VirtualSort tables for quick lookup.
    //*****************************************************************************
    __checkReturn
    HRESULT GetInterfaceImplsForTypeDef(mdTypeDef td, RID *pRidStart, RID *pRidEnd = 0)
    {
        return LookUpTableByCol( RidFromToken(td), m_pVS[TBL_InterfaceImpl], pRidStart, pRidEnd);
    }

    __checkReturn
    HRESULT GetGenericParamsForToken(mdToken tk, RID *pRidStart, RID *pRidEnd = 0)
    {
        return LookUpTableByCol(
            encodeToken(RidFromToken(tk), TypeFromToken(tk), mdtTypeOrMethodDef, lengthof(mdtTypeOrMethodDef)),
            m_pVS[TBL_GenericParam], pRidStart, pRidEnd);
    }

    __checkReturn
    HRESULT GetGenericParamConstraintsForToken(mdToken tk, RID *pRidStart, RID *pRidEnd = 0)
    {
        return LookUpTableByCol( RidFromToken(tk),
            m_pVS[TBL_GenericParamConstraint], pRidStart, pRidEnd);
    }

    __checkReturn
    HRESULT GetMethodSpecsForToken(mdToken tk, RID *pRidStart, RID *pRidEnd = 0)
    {
        return LookUpTableByCol(
            encodeToken(RidFromToken(tk), TypeFromToken(tk), mdtMethodDefOrRef, lengthof(mdtMethodDefOrRef)),
            m_pVS[TBL_MethodSpec], pRidStart, pRidEnd);
    }

    __checkReturn
    HRESULT GetDeclSecurityForToken(mdToken tk, RID *pRidStart, RID *pRidEnd = 0)
    {
        return LookUpTableByCol(
            encodeToken(RidFromToken(tk), TypeFromToken(tk), mdtHasDeclSecurity, lengthof(mdtHasDeclSecurity)),
            m_pVS[TBL_DeclSecurity],
            pRidStart,
            pRidEnd);
    }

    __checkReturn
    HRESULT GetCustomAttributeForToken(mdToken tk, RID *pRidStart, RID *pRidEnd = 0)
    {
        return LookUpTableByCol(
            encodeToken(RidFromToken(tk), TypeFromToken(tk), mdtHasCustomAttribute, lengthof(mdtHasCustomAttribute)),
            m_pVS[TBL_CustomAttribute],
            pRidStart,
            pRidEnd);
    }

    __checkReturn
    FORCEINLINE HRESULT GetUserString(ULONG nIndex, MetaData::DataBlob *pData)
    { return m_UserStringHeap.GetBlob(nIndex, pData); }
    // Gets user string (*Data) at index (nIndex) and fills the index (*pnNextIndex) of the next user string
    // in the heap.
    // Returns S_OK and fills the string (*pData) and the next index (*pnNextIndex).
    // Returns S_FALSE if the index (nIndex) is not valid user string index.
    // Returns error code otherwise.
    // Clears *pData and sets *pnNextIndex to 0 on error or S_FALSE.
    __checkReturn
    HRESULT GetUserStringAndNextIndex(
        UINT32              nIndex,
        MetaData::DataBlob *pData,
        UINT32             *pnNextIndex);

    FORCEINLINE int IsSorted(ULONG ixTbl) { return m_Schema.IsSorted(ixTbl);}
    FORCEINLINE int IsSortable(ULONG ixTbl) { return m_bSortable[ixTbl];}
    FORCEINLINE bool HasDelete() { return ((m_Schema.m_heaps & CMiniMdSchema::HAS_DELETE) ? true : false); }
    FORCEINLINE int IsPreSaveDone() { return m_bPreSaveDone; }

protected:
    __checkReturn HRESULT PreSave(MetaDataReorderingOptions reorderingOptions=NoReordering, CorProfileData *pProfileData=NULL);
    __checkReturn HRESULT PostSave();

    __checkReturn HRESULT PreSaveFull();
    __checkReturn HRESULT PreSaveEnc();

    __checkReturn HRESULT GetFullPoolSaveSize(int iPool, UINT32 *pcbSize);
    __checkReturn HRESULT GetENCPoolSaveSize(int iPool, UINT32 *pcbSize);

    __checkReturn HRESULT SaveFullPoolToStream(int iPool, IStream *pIStream);
    __checkReturn HRESULT SaveENCPoolToStream(int iPool, IStream *pIStream);

    __checkReturn
    HRESULT GetHotMetadataTokensSearchAware(
        CorProfileData *pProfileData,
        ULONG ixTbl,
        ULONG *pResultCount,
        mdToken *tokenBuffer,
        ULONG maxCount);

    __checkReturn
    HRESULT GetFullSaveSize(
        CorSaveSize               fSave,
        UINT32                   *pcbSize,
        DWORD                    *pbCompressed,
        MetaDataReorderingOptions reorderingOptions = NoReordering,
        CorProfileData           *pProfileData = NULL);
    __checkReturn
    HRESULT GetENCSaveSize(UINT32 *pcbSize);
    __checkReturn
    HRESULT GetHotPoolsSaveSize(
        UINT32                   *pcbSize,
        MetaDataReorderingOptions reorderingOptions,
        CorProfileData           *pProfileData);

    __checkReturn
    HRESULT SaveFullTablesToStream(IStream *pIStream, MetaDataReorderingOptions reorderingOptions=NoReordering, CorProfileData *pProfileData = NULL );
    __checkReturn
    HRESULT SaveENCTablesToStream(IStream *pIStream);
    __checkReturn
    HRESULT SaveHotPoolsToStream(
        IStream                  *pStream,
        MetaDataReorderingOptions reorderingOptions,
        CorProfileData           *pProfileData,
        UINT32                   *pnPoolDirSize,
        UINT32                   *pnSavedPoolsSize);
    __checkReturn
    HRESULT SaveHotPoolToStream(
        IStream                 *pStream,
        CorProfileData          *pProfileData,
        MetaData::HotHeapWriter *pHotHeapWriter,
        UINT32                  *pnSavedSize);

    // TO ELIMINATE:
    __checkReturn
    HRESULT AddGuid(REFGUID pGuid, UINT32 *pnIndex)
    { return m_GuidHeap.AddGuid(&pGuid, pnIndex); }

    // Allows putting into tables outside this MiniMd, specifically the temporary
    //  table used on save.
    __checkReturn
    HRESULT PutCol(CMiniColDef ColDef, void *pRecord, ULONG uVal);

    // Returns TRUE if token (tk) is valid.
    // For user strings, consideres 0 as valid token.
    BOOL _IsValidToken(
        mdToken tk)         // [IN] token to be checked
    {
        if (TypeFromToken(tk) == mdtString)
        {
            // need to check the user string heap
            return m_UserStringHeap.IsValidIndex(RidFromToken(tk));
        }
        // Base type doesn't know about user string blob (yet)
        return _IsValidTokenBase(tk);
    } // CMiniMdRW::_IsValidToken

#ifdef _DEBUG
    bool CanHaveCustomAttribute(ULONG ixTbl);
#endif

    __checkReturn
    HRESULT ExpandTables();
    __checkReturn
    HRESULT ExpandTableColumns(CMiniMdSchema &Schema, ULONG ixTbl);

    __checkReturn
    HRESULT InitWithLargeTables();

    void ComputeGrowLimits(int bSmall=TRUE); // Set max, lim, based on param.
    ULONG       m_maxRid;               // Highest RID so far allocated.
    ULONG       m_limRid;               // Limit on RID before growing.
    ULONG       m_maxIx;                // Highest pool index so far.
    ULONG       m_limIx;                // Limit on pool index before growing.
    enum        {eg_ok, eg_grow, eg_grown} m_eGrow; // Is a grow required? done?
    #define AUTO_GROW_CODED_TOKEN_PADDING 5

    // fix up these tables after PreSave has move the tokens
    __checkReturn HRESULT FixUpTable(ULONG ixTbl);
    __checkReturn HRESULT FixUpRefToDef();

    // Table info.
    MetaData::TableRW m_Tables[TBL_COUNT];
    VirtualSort *m_pVS[TBL_COUNT];      // Virtual sorters, one per table, but sparse.

    //*****************************************************************************
    // look up a table by a col given col value is ulVal.
    //*****************************************************************************
    __checkReturn
    HRESULT LookUpTableByCol(
        ULONG       ulVal,
        VirtualSort *pVSTable,
        RID         *pRidStart,
        RID         *pRidEnd);

    __checkReturn
    HRESULT Impl_SearchTableRW(ULONG ixTbl, ULONG ixCol, ULONG ulTarget, RID *pFoundRid);
    __checkReturn
    virtual HRESULT vSearchTable(ULONG ixTbl, CMiniColDef sColumn, ULONG ulTarget, RID *pRid);
    __checkReturn
    virtual HRESULT vSearchTableNotGreater(ULONG ixTbl, CMiniColDef sColumn, ULONG ulTarget, RID *pRid);

    void SetSorted(ULONG ixTbl, int bSorted)
        { m_Schema.SetSorted(ixTbl, bSorted); }

    void SetPreSaveDone(int bPreSaveDone)
        { m_bPreSaveDone = bPreSaveDone; }

    // Heaps
    MetaData::StringHeapRW m_StringHeap;
    MetaData::BlobHeapRW   m_BlobHeap;
    MetaData::BlobHeapRW   m_UserStringHeap;
    MetaData::GuidHeapRW   m_GuidHeap;

    IMapToken  *m_pHandler;     // Remap handler.
    __checkReturn HRESULT MapToken(RID from, RID to, mdToken type);

    ULONG m_cbSaveSize;         // Estimate of save size.

    int m_fIsReadOnly : 1;      // Is this db read-only?
    int m_bPreSaveDone : 1;     // Has save optimization been done?
    int m_bSaveCompressed : 1;  // Can the data be saved as fully compressed?
    int m_bPostGSSMod : 1;      // true if a change was made post GetSaveSize.

    //*************************************************************************
    // Overridables -- must be provided in derived classes.
    __checkReturn
    FORCEINLINE HRESULT Impl_GetString(UINT32 nIndex, __out LPCSTR *pszString)
    { return m_StringHeap.GetString(nIndex, pszString); }
    __checkReturn
    HRESULT Impl_GetStringW(ULONG ix, __inout_ecount (cchBuffer) LPWSTR szOut, ULONG cchBuffer, ULONG *pcchBuffer);
    __checkReturn
    FORCEINLINE HRESULT Impl_GetGuid(UINT32 nIndex, GUID *pTargetGuid)
    {
        HRESULT         hr;
        GUID UNALIGNED *pSourceGuid;
        IfFailRet(m_GuidHeap.GetGuid(
            nIndex,
            &pSourceGuid));
        // Add void* casts so that the compiler can't make assumptions about alignment.
        CopyMemory((void *)pTargetGuid, (void *)pSourceGuid, sizeof(GUID));
        SwapGuid(pTargetGuid);
        return S_OK;
    }

    __checkReturn
    FORCEINLINE HRESULT Impl_GetBlob(ULONG nIndex, __out MetaData::DataBlob *pData)
    { return m_BlobHeap.GetBlob(nIndex, pData); }

    __checkReturn
    FORCEINLINE HRESULT Impl_GetRow(
                        UINT32 nTableIndex,
                        UINT32 nRowIndex,
        __deref_out_opt BYTE **ppRecord)
    {
        _ASSERTE(nTableIndex < TBL_COUNT);
        return m_Tables[nTableIndex].GetRecord(nRowIndex, ppRecord);
    }

    // Count of rows in tbl2, pointed to by the column in tbl.
    __checkReturn
    HRESULT Impl_GetEndRidForColumn(
        UINT32       nTableIndex,
        RID          nRowIndex,
        CMiniColDef &def,                   // Column containing the RID into other table.
        UINT32       nTargetTableIndex,     // The other table.
        RID         *pEndRid);

    __checkReturn
    FORCEINLINE HRESULT Impl_SearchTable(ULONG ixTbl, CMiniColDef sColumn, ULONG ixCol, ULONG ulTarget, RID *pFoundRid)
    { return Impl_SearchTableRW(ixTbl, ixCol, ulTarget, pFoundRid); }

    FORCEINLINE int Impl_IsRo()
    { return 0; }

    //*************************************************************************
    enum {END_OF_TABLE = 0};
    FORCEINLINE ULONG NewRecordPointerEndValue(ULONG ixTbl)
    { if (HasIndirectTable(ixTbl)) return m_Schema.m_cRecs[ixTbl]+1; else return END_OF_TABLE; }

    __checkReturn HRESULT ConvertMarkerToEndOfTable(ULONG tblParent, ULONG colParent, ULONG ridChild, RID ridParent);

    // Add a child row, adjust pointers in parent rows.
    __checkReturn
    HRESULT AddChildRowIndirectForParent(
        ULONG tblParent,
        ULONG colParent,
        ULONG tblChild,
        RID ridParent,
        void **ppRow);

    // Update pointers in the parent table to reflect the addition of a child, if required
    // create the indirect table in which case don't update pointers.
    __checkReturn
    HRESULT AddChildRowDirectForParent(ULONG tblParent, ULONG colParent, ULONG tblChild, RID ridParent);

    // Given a table id, create the corresponding indirect table.
    __checkReturn
    HRESULT CreateIndirectTable(ULONG ixtbl, BOOL bOneLess = true);

    // If the last param is not added in the right sequence, fix it up.
    __checkReturn
    HRESULT FixParamSequence(RID md);

    // these are the map tables to map a method, a field, a property, a event, or a param to its parent
    TOKENMAP    *m_pMethodMap;
    TOKENMAP    *m_pFieldMap;
    TOKENMAP    *m_pPropertyMap;
    TOKENMAP    *m_pEventMap;
    TOKENMAP    *m_pParamMap;

    // This table keep tracks tokens that are marked( or filtered)
    FilterTable *m_pFilterTable;
    IHostFilter *m_pHostFilter;

    // TOKENMAP *m_pTypeRefToTypeDefMap;
    TokenRemapManager *m_pTokenRemapManager;

    OptionValue m_OptionValue;

    CMiniMdSchema m_StartupSchema;      // Schema at start time.  Keep count of records.
    BYTE        m_bSortable[TBL_COUNT]; // Is a given table sortable?  (Can it be reorganized?)
#ifdef FEATURE_METADATA_CUSTOM_DATA_SOURCE
    ReleaseHolder<IMDCustomDataSource> m_pCustomDataSource;
#endif

#ifdef _DEBUG

protected:
    UTSemReadWrite * dbg_m_pLock;

public:
    // Checks that MetaData is locked for write operation (if thread-safety is enabled and the lock exists)
    void Debug_CheckIsLockedForWrite();

    void Debug_SetLock(UTSemReadWrite * pLock)
    {
        dbg_m_pLock = pLock;
    }

#endif //_DEBUG

public:

    FilterTable *GetFilterTable();
    __checkReturn HRESULT UnmarkAll();
    __checkReturn HRESULT MarkAll();

    FORCEINLINE IHostFilter *GetHostFilter() { return m_pHostFilter;}

    __checkReturn HRESULT CalculateTypeRefToTypeDefMap();

    FORCEINLINE TOKENMAP *GetTypeRefToTypeDefMap()
    { return m_pTokenRemapManager ? m_pTokenRemapManager->GetTypeRefToTypeDefMap() : NULL; };

    FORCEINLINE TOKENMAP *GetMemberRefToMemberDefMap()
    { return m_pTokenRemapManager ? m_pTokenRemapManager->GetMemberRefToMemberDefMap() : NULL; };

    FORCEINLINE MDTOKENMAP *GetTokenMovementMap()
    { return m_pTokenRemapManager ? m_pTokenRemapManager->GetTokenMovementMap() : NULL; };

    FORCEINLINE TokenRemapManager *GetTokenRemapManager() { return m_pTokenRemapManager; };

    __checkReturn HRESULT InitTokenRemapManager();

    virtual ULONG vGetCol(ULONG ixTbl, ULONG ixCol, void *pRecord)
    { return GetCol(ixTbl, ixCol, pRecord);}

public:
    virtual BOOL IsWritable()
    {
        return !m_fIsReadOnly;
    }

    //*************************************************************************
    // Delta MetaData (EditAndContinue) functions.
public:
    enum eDeltaFuncs{
        eDeltaFuncDefault = 0,
        eDeltaMethodCreate,
        eDeltaFieldCreate,
        eDeltaParamCreate,
        eDeltaPropertyCreate,
        eDeltaEventCreate,
    };

    __checkReturn HRESULT ApplyDelta(CMiniMdRW &mdDelta);

public:
    // Functions for updating ENC log tables ENC log.
    FORCEINLINE BOOL IsENCOn()
    {
        return (m_OptionValue.m_UpdateMode & MDUpdateMask) == MDUpdateENC;
    }

    __checkReturn
    FORCEINLINE HRESULT UpdateENCLog(mdToken tk, CMiniMdRW::eDeltaFuncs funccode = CMiniMdRW::eDeltaFuncDefault)
    {
        if (IsENCOn())
            return UpdateENCLogHelper(tk, funccode);
        else
            return S_OK;
    }

    __checkReturn
    FORCEINLINE HRESULT UpdateENCLog2(ULONG ixTbl, ULONG iRid, CMiniMdRW::eDeltaFuncs funccode = CMiniMdRW::eDeltaFuncDefault)
    {
        if (IsENCOn())
            return UpdateENCLogHelper2(ixTbl, iRid, funccode);
        else
            return S_OK;
    }

    __checkReturn HRESULT ResetENCLog();

private:
    BOOL m_fMinimalDelta;

    //
    // String heap reorganization
    //

    // Check to see if it is safe to reorder the string pool.
    BOOL IsSafeToReorderStringPool();
    // Function to mark hot strings in the marks array based on the token information in profile data.
    VOID MarkHotStrings(CorProfileData *pProfileData, BYTE * pMarks, ULONG poolSize);
    // Function to mark hot strings referenced by hot tables based on token information in profile data.
    VOID MarkStringsInHotTables(CorProfileData *pProfileData, BYTE * pMarks, ULONG poolSize);
    // Function to mark strings referenced by the different metadata tables.
    VOID MarkStringsInTables(BYTE * pMarks, ULONG poolSize);
    // Function to mark duplicate strings in the mark array.
    // Throws on error.
    VOID MarkDuplicateStrings(BYTE * pMarks, ULONG poolSize);
    // Function to update the tables with the modified string offsets.
    VOID FixStringsInTables();
    // Function to fill the given string pool with strings from the existing string pool using the mark array.
    // Throws on error.
    VOID CreateReorderedStringPool(
        MetaData::StringHeapRW *pStringHeap,
        BYTE                   *pMarks,
        ULONG                   cbHeapSize,
        CorProfileData         *pProfileData);

public:
    BOOL IsMinimalDelta()
    {
        return m_fMinimalDelta;
    }

    // Turns on/off  the ability to emit delta metadatas

    // Unfortunately, we can't allow this to be set via the SetOption method anymore. In v1.0 and v1.1, this flag
    // could be set but would still result in generating full metadatas. We can't automatically start generating
    // true deltas for people... it could break them.
    void EnableDeltaMetadataGeneration()
    {
        _ASSERTE(m_OptionValue.m_UpdateMode == MDUpdateENC);
    }
    void DisableDeltaMetadataGeneration() {m_OptionValue.m_UpdateMode = MDUpdateENC;}

protected:
    // Internal Helper functions for ENC log.
    __checkReturn
    HRESULT UpdateENCLogHelper(mdToken tk, CMiniMdRW::eDeltaFuncs funccode);
    __checkReturn
    HRESULT UpdateENCLogHelper2(ULONG ixTbl, ULONG iRid, CMiniMdRW::eDeltaFuncs funccode);

protected:
    static ULONG m_TruncatedEncTables[];
    static ULONG m_SuppressedDeltaColumns[TBL_COUNT];

    ULONGARRAY  *m_rENCRecs;    // Array of RIDs affected by ENC.

    __checkReturn
    HRESULT ApplyRecordDelta(CMiniMdRW &mdDelta, ULONG ixTbl, void *pDelta, void *pRecord);
    __checkReturn
    HRESULT ApplyTableDelta(CMiniMdRW &mdDelta, ULONG ixTbl, RID iRid, int fc);
    __checkReturn
    HRESULT GetDeltaRecord(ULONG ixTbl, ULONG iRid, void **ppRecord);
    __checkReturn
    HRESULT ApplyHeapDeltas(CMiniMdRW &mdDelta);
    __checkReturn
    HRESULT ApplyHeapDeltasWithMinimalDelta(CMiniMdRW &mdDelta);
    __checkReturn
    HRESULT ApplyHeapDeltasWithFullDelta(CMiniMdRW &mdDelta);
    __checkReturn
    HRESULT StartENCMap();              // Call, on a delta MD, to prepare to access sparse rows.
    __checkReturn
    HRESULT EndENCMap();                // Call, on a delta MD, when done with sparse rows.

public:
    // Workaround for compiler performance issue VSW 584653 for 2.0 RTM.
    // Get the table's VirtualSort validity state.
    bool IsTableVirtualSorted(ULONG ixTbl);
    // Workaround for compiler performance issue VSW 584653 for 2.0 RTM.
    // Validate table's VirtualSort after adding one record into the table.
    // Returns new VirtualSort validity state in *pfIsTableVirtualSortValid.
    // Assumptions:
    //    Table's VirtualSort was valid before adding the record to the table.
    //    The caller must ensure validity of VirtualSort by calling to
    //    IsTableVirtualSorted or by using the returned state from previous
    //    call to this method.
    __checkReturn
    HRESULT ValidateVirtualSortAfterAddRecord(
        ULONG  ixTbl,
        bool * pfIsTableVirtualSortValid);

}; // class CMiniMdRW : public CMiniMdTemplate<CMiniMdRW>

#endif // _METAMODELRW_H_


text:
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.vmwarecloudsimple.models;

import com.azure.core.annotation.Fluent;
import com.azure.core.util.logging.ClientLogger;
import com.azure.resourcemanager.vmwarecloudsimple.fluent.models.DedicatedCloudNodeInner;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

/** List of dedicated nodes response model. */
@Fluent
public final class DedicatedCloudNodeListResponse {
    @JsonIgnore private final ClientLogger logger = new ClientLogger(DedicatedCloudNodeListResponse.class);

    /*
     * Link for next list of DedicatedCloudNode
     */
    @JsonProperty(value = "nextLink")
    private String nextLink;

    /*
     * Results of the DedicatedCloudNode list
     */
    @JsonProperty(value = "value")
    private List<DedicatedCloudNodeInner> value;

    /**
     * Get the nextLink property: Link for next list of DedicatedCloudNode.
     *
     * @return the nextLink value.
     */
    public String nextLink() {
        return this.nextLink;
    }

    /**
     * Set the nextLink property: Link for next list of DedicatedCloudNode.
     *
     * @param nextLink the nextLink value to set.
     * @return the DedicatedCloudNodeListResponse object itself.
     */
    public DedicatedCloudNodeListResponse withNextLink(String nextLink) {
        this.nextLink = nextLink;
        return this;
    }

    /**
     * Get the value property: Results of the DedicatedCloudNode list.
     *
     * @return the value value.
     */
    public List<DedicatedCloudNodeInner> value() {
        return this.value;
    }

    /**
     * Set the value property: Results of the DedicatedCloudNode list.
     *
     * @param value the value value to set.
     * @return the DedicatedCloudNodeListResponse object itself.
     */
    public DedicatedCloudNodeListResponse withValue(List<DedicatedCloudNodeInner> value) {
        this.value = value;
        return this;
    }

    /**
     * Validates the instance.
     *
     * @throws IllegalArgumentException thrown if the instance is not valid.
     */
    public void validate() {
        if (value() != null) {
            value().forEach(e -> e.validate());
        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
use core::convert::From;
use x86_64::structures::paging::{
    mapper::{self, Mapper},
    FrameAllocator, Page, PageSize, PhysFrame, RecursivePageTable, Size4KiB,
};
use x86_64::{PhysAddr, VirtAddr};

pub const KERNEL_HEAP_SIZE: u64 = 0x4_000_000;
pub const KERNEL_HEAP_BASE: u64 = 0x1_000_000;

#[derive(Debug)]
pub enum KernelHeapError {
    NoneError,
    MapToError(mapper::MapToError),
    UnknownError,
}

impl From<core::option::NoneError> for KernelHeapError {
    fn from(_: core::option::NoneError) -> KernelHeapError {
        KernelHeapError::NoneError
    }
}

impl From<mapper::MapToError> for KernelHeapError {
    fn from(e: mapper::MapToError) -> KernelHeapError {
        KernelHeapError::MapToError(e)
    }
}

pub unsafe fn init_kernel_heap(
    memory_map: &MemoryMap,
    recursive_page_table: &mut RecursivePageTable,
    frame_allocator: &mut impl FrameAllocator<Size4KiB>,
) -> Result<(), KernelHeapError> {
    fn inner(
        memory_map: &MemoryMap,
        recursive_page_table: &mut RecursivePageTable,
        frame_allocator: &mut impl FrameAllocator<Size4KiB>,
    ) -> Result<(), KernelHeapError> {
        use x86_64::structures::paging::PageTableFlags as Flags;
        let heap_flags = Flags::PRESENT | Flags::WRITABLE;

        // find the first physical region big enough to fit our heap
        let region = memory_map
            .iter()
            .filter(|r| r.region_type == MemoryRegionType::Usable)
            .filter(|r| (r.range.start_addr() + KERNEL_HEAP_SIZE) < r.range.end_addr())
            .next()?;

        let start_addr = region.range.start_addr();

        // start mapping
        let mut current: u64 = 0;
        while current <= KERNEL_HEAP_SIZE {
            let page: Page = Page::containing_address(VirtAddr::new(KERNEL_HEAP_BASE + current));
            let frame = PhysFrame::containing_address(PhysAddr::new(start_addr + current));

            unsafe {
                recursive_page_table
                    .map_to(page, frame, heap_flags, frame_allocator)?
                    .flush();
            };

            current += Size4KiB::SIZE;
        }

        Ok(())
    }

    inner(memory_map, recursive_page_table, frame_allocator)
}


text:
"""
API for logging to a log database.
Kept for backwards-compatibility.
"""

import next.constants as constants
import next.utils as utils
import datetime
from next.database_client.DatabaseAPI import DatabaseAPI

class LoggerAPI(DatabaseAPI):
    def __init__(self, mongo_host=constants.MONGODB_HOST, mongo_port=constants.MONGODB_PORT,
                    database_name=constants.logs_database_id):
        super(LoggerAPI, self).__init__(mongo_host, mongo_port, database_name)

    def _normalize_logentry(self, log):
        if log.get('timestamp') and isinstance(log.get('timestamp'), datetime.datetime):
            log['timestamp'] = str(log['timestamp'])

        return log

    def log(self,bucket_id,log_dict):
        """
        Saves log_dict to PermStore as an individual document for later recall.

        Inputs:
            (string) bucket_id, (dict with string values) log_dict
        """
        self.set_doc(bucket_id, None, log_dict)

    def get_logs_with_filter(self,bucket_id,pattern_dict):
        """
        Retrieves all logs in bucket_id that match (i.e. contain) pattern_dict

        Inputs:
            (string) bucket_id, (dict of string values) pattern_dict
        """
        return [self._normalize_logentry(d)
            for d in self.get_docs_with_filter(bucket_id, pattern_dict)]

    def delete_logs_with_filter(self,bucket_id,pattern_dict):
        """
        Deletes all logs in bucket_id that match (i.e. contain) pattern_dict

        Inputs:
            (string) bucket_id, (dict of string values) pattern_dict
        """
        self.delete_docs_with_filter(bucket_id, pattern_dict)


text:
class ManageIQ::Providers::Amazon::NetworkManager::RefreshWorker < ::MiqEmsRefreshWorker
  require_nested :Runner

  def self.ems_class
    ManageIQ::Providers::Amazon::NetworkManager
  end

  def self.settings_name
    :ems_refresh_worker_amazon_network
  end
end


text:
NuiKinfuVolumeConfig::NuiKinfuVolumeConfig()
{
	dimensions = Vector3f::Constant(3.0f);
	resolution = Vector3i::Constant(256);
	tranc_dist = 0.03f;
	bHas_color_volume = true;
	max_color_weight = 128;
	bIsDynamic = false;
	voxel_shift = 15;
	translateBasis = Vector3f::Zero();
}

bool	NuiKinfuVolumeConfig::load(const std::string& fileName)
{
	std::ifstream fpin (fileName.c_str(), std::ios::in);
	if( !fpin.is_open() )
	{
		//throw std::ios::failure(__FUNCTION__ + std::string(": could not open file ") + fileName);
		return false;
	}

	std::string line;
	while( getline(fpin,line) )
	{
		size_t pos = line.find('=');
		if(pos == std::string::npos)
			continue;

		std::string tmpKey = line.substr(0, pos);
		if("VolumeSizeX" == tmpKey)
		{
			dimensions[0] = (float)atof(line.substr(pos+1).c_str());
		}
		else if("VolumeSizeY" == tmpKey)
		{
			dimensions[1] = (float)atof(line.substr(pos+1).c_str());
		}
		else if("VolumeSizeZ" == tmpKey)
		{
			dimensions[2] = (float)atof(line.substr(pos+1).c_str());
		}
		else if("VolumeResolutionX" == tmpKey)
		{
			resolution[0] = atoi(line.substr(pos+1).c_str());
		}
		else if("VolumeResolutionY" == tmpKey)
		{
			resolution[1] = atoi(line.substr(pos+1).c_str());
		}
		else if("VolumeResolutionZ" == tmpKey)
		{
			resolution[2] = atoi(line.substr(pos+1).c_str());
		}
		else if("VolumeTrancDist" == tmpKey)
		{
			tranc_dist = (float)atof(line.substr(pos+1).c_str());
		}
		else if("VolumeHadColor" == tmpKey)
		{
			bHas_color_volume = atoi(line.substr(pos+1).c_str()) ? true : false;
		}
		else if("MaxColorWeight" == tmpKey)
		{
			max_color_weight = atoi(line.substr(pos+1).c_str());
		}
		else if("IsDynamicVolume" == tmpKey)
		{
			bIsDynamic = atoi(line.substr(pos+1).c_str()) ? true : false;
		}
		else if("DynamicVoxelShift" == tmpKey)
		{
			voxel_shift = atoi(line.substr(pos+1).c_str());
		}
		else if("VolumeBasisX" == tmpKey)
		{
			translateBasis[0] = (float)atof(line.substr(pos+1).c_str());
		}
		else if("VolumeBasisY" == tmpKey)
		{
			translateBasis[1] = (float)atof(line.substr(pos+1).c_str());
		}
		else if("VolumeBasisZ" == tmpKey)
		{
			translateBasis[2] = (float)atof(line.substr(pos+1).c_str());
		}
	}

	// Close file
	fpin.close ();
	return true;
}

bool	NuiKinfuVolumeConfig::log(const std::string& fileName) const
{
	// Open file
	std::ofstream fpout (fileName.c_str(), std::ios::out | std::ios::app);
	if( !fpout.is_open() )
	{
		//throw std::ios::failure(__FUNCTION__ + std::string(": could not open file ") + fileName);
		return false;
	}

	fpout << "VolumeSizeX=" << dimensions[0] << std::endl;
	fpout << "VolumeSizeY=" << dimensions[1] << std::endl;
	fpout << "VolumeSizeZ=" << dimensions[2] << std::endl;
	fpout << "VolumeResolutionX=" << resolution[0] << std::endl;
	fpout << "VolumeResolutionY=" << resolution[1] << std::endl;
	fpout << "VolumeResolutionZ=" << resolution[2] << std::endl;
	fpout << "VolumeTrancDist=" << tranc_dist << std::endl;
	fpout << "VolumeHadColor=" << bHas_color_volume << std::endl;
	fpout << "MaxColorWeight=" << max_color_weight << std::endl;
	fpout << "IsDynamicVolume=" << bIsDynamic << std::endl;
	fpout << "DynamicVoxelShift=" << voxel_shift << std::endl;
	fpout << "VolumeBasisX=" << translateBasis[0] << std::endl;
	fpout << "VolumeBasisY=" << translateBasis[1] << std::endl;
	fpout << "VolumeBasisZ=" << translateBasis[2] << std::endl;

	// Close file
	fpout.close ();
	return true;
}


text:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using System.Reflection;

using Vila.Extensions;

namespace InControls.Common
{

	public class ConfigItemCatalogo
	{
		private static ConfigItemCatalogo _Instance = new ConfigItemCatalogo();

		public static ConfigItemCatalogo Instance
		{
			get { return ConfigItemCatalogo._Instance; }
		}

		private Dictionary<ConfigItemEnum, ConfigItem> _list;

		public ConfigItem this[ConfigItemEnum key]
		{
			get
			{
				if (_list.ContainsKey(key))
					return _list[key];
				else
					return _list[ConfigItemEnum.UnDefined];

			}
		}

		private ConfigItemCatalogo()
		{
			_list = new Dictionary<ConfigItemEnum, ConfigItem>(251);
		}

		/// <summary>
		/// 从指定的DataTable中读取数据并构建内部表
		/// tblConfig
		/// </summary>
		/// <param name="dtSource">tblConfig</param>
		public void LoadFrom(DataTable dtSource)
		{
			_list.Clear();

			// 总是有一个 "未定义"项
			ConfigItem item = new ConfigItem(ConfigItemEnum.UnDefined, "ConfigItemNoDefined", 0, "未定义项");
			_list.Add(ConfigItemEnum.UnDefined, item);

			// 主界面刷新周期间隔
			item = new ConfigItem(ConfigItemEnum.UI_ScanInterval, "UI_ScanInterval", 100, "UI_ScanInterval");
			_list.Add(item.Key, item);

			// 从数据库中取值
			for (int i = 0; i < dtSource.Rows.Count; i++) {
				DataRow r = dtSource.Rows[i];

				string keyName = r["Key"].ToDbString().Replace('.', '_');

				ConfigItemEnum key = ParseFrom(keyName);
				if (key != ConfigItemEnum.UnDefined) {
					item = new ConfigItem(key, keyName);

					item.KeyName = keyName;
					item.Comment = r["Comment"].ToDbString();
					item.Value = r["Value"].ToDbDouble();

					_list.Add(key, item);
				} else {
					System.Diagnostics.Debug.Print("配置表中的项目 {0} 没有对应的枚举值存在!请检查。", keyName);
				}
			}
		}

		private static ConfigItemEnum ParseFrom(string enumName)
		{
			ConfigItemEnum eResult;

			if (Enum.TryParse<ConfigItemEnum>(enumName, true, out eResult)) {
				return eResult;
			} else {
				return ConfigItemEnum.UnDefined;
			}
		}

	}
}


text:
"""Class that implement  ARNet."""
import torch
import torch.nn as nn

class  ARNet(nn.Module):

  """Auto Regressive model as described in https://arxiv.org/abs/1911.12436.
  """

  def __init__(self, n_forecasts, n_lags, device):
    """Initializes a  ARNet instance.

    Args:
      n_forecasts: Number of time steps to forecast
      n_lags: Lags (past time steps) used to make forecast
      device: Device used  by the model
    """
    super(ARNet, self).__init__()
    self.n_lags = n_lags
    self.device = device
    self.n_forecasts = n_forecasts
    self.fc = nn.Linear(n_lags, 1, bias=False)
    nn.init.kaiming_normal_(self.fc.weight, mode="fan_in")

  def forward(self, x, true_output):
    """Forward pass for  ARNet.

    Args:
      x: A tensor of shape `(batch_size, n_lags)
      true_output: Actual forecast this is used for teacher forcing during
        training

    Returns:
      output: Forecast a tensor of shape `(batch_size, n_forecasts)`
    """
    output = torch.zeros((x.shape[0], self.n_forecasts)).to(self.device)
    output[:, 0] = self.fc(x).squeeze()

    if self.n_forecasts > self.n_lags:
      # If the forecast larger the lags than use orignal input and shift untill
      # the orginal inputs are done than use true output (teacher forecing).
      for i in range(1, self.n_lags):
        output[:,
               i] = self.fc(torch.cat((x[:, i:], true_output[:, :i]),
                                      dim=1)).squeeze()
      for i in range(0, self.n_forecasts - self.n_lags):
        output[:, self.n_lags + i] = self.fc(
            true_output[:, i:i + self.n_lags]).squeeze()
    else:
      for i in range(1, self.n_forecasts):
        output[:,
               i] = self.fc(torch.cat((x[:, i:], true_output[:, :i]),
                                      dim=1)).squeeze()

    return output

  def predict(self, x):
    """Function used during testing to make predictions in an auto regressive style.

    Args:
      x : A tensor of shape `(batch_size, n_lags)
    Returns:
      output: Forecast a tensor of shape `(batch_size, n_forecasts)`
    """

    output = torch.zeros((x.shape[0], self.n_forecasts)).to(self.device)

    output[:, 0] = self.fc(x).squeeze()
    if self.n_forecasts > self.n_lags:
      # If the forecast larger the lags than use orignal input and shift untill
      # the orginal inputs are done than the input will only contain forecasted
      # values
      for i in range(1, self.n_lags):
        output[:, i] = self.fc(torch.cat((x[:, i:], output[:, :i]),
                                         dim=1)).squeeze()
      for i in range(0, self.n_forecasts - self.n_lags):
        output[:,
               self.n_lags + i] = self.fc(output[:,
                                                 i:i + self.n_lags]).squeeze()
    else:
      for i in range(1, self.n_forecasts):
        output[:, i] = self.fc(torch.cat((x[:, i:], output[:, :i]),
                                         dim=1)).squeeze()
    return output


text:
#include "ClientConductorFixture.h"

MockClientConductorHandlers::MockClientConductorHandlers() = default;

MockClientConductorHandlers::~MockClientConductorHandlers() = default;


text:
raise "Missing parameter 'CRED_PATH'. Please read docs at #{__FILE__}" \
  unless ENV.key?('CRED_PATH')
raise "Missing parameter 'PROJECT'. Please read docs at #{__FILE__}" \
  unless ENV.key?('PROJECT')

# For more information on the gauth_credential parameters and providers please
# refer to its detailed documentation at:
# https://github.com/GoogleCloudPlatform/chef-google-auth
gauth_credential 'mycred' do
  action :serviceaccount
  path ENV['CRED_PATH'] # e.g. '/path/to/my_account.json'
  scopes [
    'https://www.googleapis.com/auth/compute'
  ]
end

gcompute_instance_group 'chef-e2e-my-chef-servers' do
  action :create
  zone 'us-central1-a'
  project ENV['PROJECT'] # ex: 'my-test-project'
  credential 'mycred'
end

# Google::Functions must be included at runtime to ensure that the
# gcompute_health_check_ref function can be used in health_check blocks.
::Chef::Resource.send(:include, Google::Functions)

gcompute_backend_service 'chef-e2e-my-app-backend' do
  action :create
  backends [
    { group: 'chef-e2e-my-chef-servers' }
  ]
  enable_cdn true
  health_checks [
    gcompute_health_check_ref('another-hc', ENV['PROJECT']) # ex: 'my-test-project'
  ]
  project ENV['PROJECT'] # ex: 'my-test-project'
  credential 'mycred'
end

gcompute_url_map 'chef-e2e-my-url-map' do
  action :create
  default_service 'chef-e2e-my-app-backend'
  project ENV['PROJECT'] # ex: 'my-test-project'
  credential 'mycred'
end

gcompute_target_http_proxy 'chef-e2e-my-http-proxy' do
  action :delete
  url_map 'chef-e2e-my-url-map'
  project ENV['PROJECT'] # ex: 'my-test-project'
  credential 'mycred'
end


text:
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('topics', '0008_auto_20170203_0934'),
    ]

    operations = [
        migrations.AddField(
            model_name='topic',
            name='other_resources',
            field=models.TextField(default=''),
            preserve_default=False,
        ),
    ]


text:
package org.pentaho.big.data.kettle.plugins.mapreduce;

import org.junit.BeforeClass;
import org.junit.Test;
import org.pentaho.big.data.api.cluster.NamedCluster;
import org.pentaho.big.data.api.cluster.NamedClusterService;
import org.pentaho.big.data.api.cluster.service.locator.NamedClusterServiceLocator;
import org.pentaho.big.data.kettle.plugins.mapreduce.entry.pmr.JobEntryHadoopTransJobExecutor;
import org.pentaho.di.repository.ObjectId;
import org.pentaho.di.repository.Repository;
import org.pentaho.metastore.api.IMetaStore;
import org.pentaho.runtime.test.RuntimeTester;
import org.pentaho.runtime.test.action.RuntimeTestActionService;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class JobEntryHadoopTransJobExecutorTest {
  private static NamedClusterService namedClusterService;
  private static RuntimeTestActionService runtimeTestActionService;
  private static RuntimeTester runtimeTester;
  private static NamedClusterServiceLocator namedClusterServiceLocator;
  private static JobEntryHadoopTransJobExecutor exec;
  private static Repository rep;
  private static ObjectId oid;
  private static IMetaStore metaStore;

  @BeforeClass
  public static final void setup() throws Throwable {
    namedClusterService = mock( NamedClusterService.class );
    when( namedClusterService.getClusterTemplate() ).thenReturn( mock( NamedCluster.class ) );
    runtimeTestActionService = mock( RuntimeTestActionService.class );
    runtimeTester = mock(
      RuntimeTester.class );
    namedClusterServiceLocator = mock( NamedClusterServiceLocator.class );
    rep = mock( Repository.class );
    metaStore = mock( IMetaStore.class );
    when( rep.getMetaStore() ).thenReturn( metaStore );
    oid = mock( ObjectId.class );
    exec = new JobEntryHadoopTransJobExecutor( namedClusterService, runtimeTestActionService, runtimeTester,
      namedClusterServiceLocator );
  }

  @Test
  public void loadRep_num_map_tasks_null() throws Throwable {
    when( rep.getJobEntryAttributeString( oid, "num_map_tasks" ) ).thenReturn( null );
    exec.loadRep( rep, metaStore, oid, null, null );
    assertEquals( null, exec.getNumMapTasks() );
  }

  @Test
  public void loadRep_num_map_tasks_empty() throws Throwable {
    when( rep.getJobEntryAttributeString( oid, "num_map_tasks" ) ).thenReturn( "" );
    exec.loadRep( rep, metaStore, oid, null, null );
    assertEquals( "", exec.getNumMapTasks() );
  }

  @Test
  public void loadRep_num_map_tasks_variable() throws Throwable {
    when( rep.getJobEntryAttributeString( oid, "num_map_tasks" ) ).thenReturn( "${test}" );
    exec.loadRep( rep, metaStore, oid, null, null );
    assertEquals( "${test}", exec.getNumMapTasks() );
  }

  @Test
  public void loadRep_num_map_tasks_number() throws Throwable {
    when( rep.getJobEntryAttributeString( oid, "num_map_tasks" ) ).thenReturn( "5" );
    exec.loadRep( rep, metaStore, oid, null, null );
    assertEquals( "5", exec.getNumMapTasks() );
  }

  @Test
  public void loadRep_num_reduce_tasks_null() throws Throwable {
    when( rep.getJobEntryAttributeString( oid, "num_reduce_tasks" ) ).thenReturn( null );
    exec.loadRep( rep, metaStore, oid, null, null );
    assertEquals( null, exec.getNumReduceTasks() );
  }

  @Test
  public void loadRep_num_reduce_tasks_empty() throws Throwable {
    when( rep.getJobEntryAttributeString( oid, "num_reduce_tasks" ) ).thenReturn( "" );
    exec.loadRep( rep, metaStore, oid, null, null );
    assertEquals( "", exec.getNumReduceTasks() );
  }

  @Test
  public void loadRep_num_reduce_tasks_variable() throws Throwable {
    when( rep.getJobEntryAttributeString( oid, "num_reduce_tasks" ) ).thenReturn( "${test}" );
    exec.loadRep( rep, metaStore, oid, null, null );
    assertEquals( "${test}", exec.getNumReduceTasks() );
  }

  @Test
  public void loadRep_num_reduce_tasks_number() throws Throwable {
    when( rep.getJobEntryAttributeString( oid, "num_reduce_tasks" ) ).thenReturn( "5" );
    exec.loadRep( rep, metaStore, oid, null, null );
    assertEquals( "5", exec.getNumReduceTasks() );
  }
}


text:
<!DOCTYPE html>

  <meta name="google-site-verification" content="_bMOCDpkx9ZAzBwb2kF3PRHbfUUdFj2uO8Jd1AXArz4" />
    <title>ImageMagick: MagickCore, C API for ImageMagick: Image Features</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  <meta name="application-name" content="ImageMagick"/>
  <meta name="description" content="ImageMagick® is a software suite to create, edit, compose, or convert bitmap images. It can read and write images in a variety of formats (over 200) including PNG, JPEG, JPEG-2000, GIF, WebP, Postscript, PDF, and SVG. Use ImageMagick to resize, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves."/>
  <meta name="application-url" content="http://www.imagemagick.org"/>
  <meta name="generator" content="PHP"/>
  <meta name="keywords" content="magickcore, c, api, for, imagemagick:, image, features, ImageMagick, PerlMagick, image processing, image, photo, software, Magick++, OpenMP, convert"/>
  <meta name="rating" content="GENERAL"/>
  <meta name="robots" content="INDEX, FOLLOW"/>
  <meta name="generator" content="ImageMagick Studio LLC"/>
  <meta name="author" content="ImageMagick Studio LLC"/>
  <meta name="revisit-after" content="2 DAYS"/>
  <meta name="resource-type" content="document"/>
  <meta name="copyright" content="Copyright (c) 1999-2016 ImageMagick Studio LLC"/>
  <meta name="distribution" content="Global"/>
  <meta name="magick-serial" content="P131-S030410-R485315270133-P82224-A6668-G1245-1"/>
  <link rel="icon" href="../image/wand.png"/>
  <link rel="shortcut icon" href="../image/wand.ico"/>
  <link rel="stylesheet" href="../css/magick.php"/>

      Home
      Download
      Tools
      Options
      Resources
      Develop
      Search
      Community

CannyEdgeImage • GetImageFeatures • Use HoughLineImage • MeanShiftImage

CannyEdgeImage

CannyEdgeImage() uses a multi-stage algorithm to detect a wide range of edges in images.

The format of the CannyEdgeImage method is:

Image *CannyEdgeImage(const Image *image,const double radius,
  const double sigma,const double lower_percent,
  const double upper_percent,ExceptionInfo *exception)

A description of each parameter follows:

image

the image.

radius

the radius of the gaussian smoothing filter.

sigma

the sigma of the gaussian smoothing filter.

lower_precent

percentage of edge pixels in the lower threshold.

upper_percent

percentage of edge pixels in the upper threshold.

exception

return any errors or warnings in this structure.

GetImageFeatures

GetImageFeatures() returns features for each channel in the image in each of four directions (horizontal, vertical, left and right diagonals) for the specified distance.  The features include the angular second moment, contrast, correlation, sum of squares: variance, inverse difference moment, sum average, sum varience, sum entropy, entropy, difference variance,  difference entropy, information measures of correlation 1, information measures of correlation 2, and maximum correlation coefficient.  You can access the red channel contrast, for example, like this:

channel_features=GetImageFeatures(image,1,exception);
contrast=channel_features[RedPixelChannel].contrast[0];

Use MagickRelinquishMemory() to free the features buffer.

The format of the GetImageFeatures method is:

ChannelFeatures *GetImageFeatures(const Image *image,
  const size_t distance,ExceptionInfo *exception)

A description of each parameter follows:

image

the image.

distance

the distance.

exception

return any errors or warnings in this structure.

Use HoughLineImage

Use HoughLineImage() in conjunction with any binary edge extracted image (we recommand Canny) to identify lines in the image.  The algorithm accumulates counts for every white pixel for every possible orientation (for angles from 0 to 179 in 1 degree increments) and distance from the center of the image to the corner (in 1 px increments) and stores the counts in an accumulator matrix of angle vs distance. The size of the accumulator is 180x(diagonal/2). Next it searches this space for peaks in counts and converts the locations of the peaks to slope and intercept in the normal x,y input image space. Use the slope/intercepts to find the endpoints clipped to the bounds of the image. The lines are then drawn. The counts are a measure of the length of the lines

The format of the HoughLineImage method is:

Image *HoughLineImage(const Image *image,const size_t width,
  const size_t height,const size_t threshold,ExceptionInfo *exception)

A description of each parameter follows:

image

the image.

width, height

find line pairs as local maxima in this neighborhood.

threshold

the line count threshold.

exception

return any errors or warnings in this structure.

MeanShiftImage

MeanShiftImage() delineate arbitrarily shaped clusters in the image. For each pixel, it visits all the pixels in the neighborhood specified by the window centered at the pixel and excludes those that are outside the radius=(window-1)/2 surrounding the pixel. From those pixels, it finds those that are within the specified color distance from the current mean, and computes a new x,y centroid from those coordinates and a new mean. This new x,y centroid is used as the center for a new window. This process iterates until it converges and the final mean is replaces the (original window center) pixel value. It repeats this process for the next pixel, etc.,  until it processes all pixels in the image. Results are typically better with colorspaces other than sRGB. We recommend YIQ, YUV or YCbCr.

The format of the MeanShiftImage method is:

Image *MeanShiftImage(const Image *image,const size_t width,
  const size_t height,const double color_distance,
  ExceptionInfo *exception)

A description of each parameter follows:

image

the image.

width, height

find pixels in this neighborhood.

color_distance

the color distance.

exception

return any errors or warnings in this structure.

Donate •
     Sitemap •
    Related •
    Architecture

Back to top •
    Public Key •
    Contact Us

©  1999-2016 ImageMagick Studio LLC


text:
<!doctype html>

  <meta charset="utf-8">
  <title>Doubleclick Example with UseSameDomainRenderingUntilDeprecated</title>
  <link rel="canonical" href="http://nonblocking.io/" >
  <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">

Doubleclick examples should all use GPT.js

    Each one of these ad slots has the flag useSameDomainRenderingUntilDeprecated specified in a different way. Each way should work, and all should use GPT.js, not glade.

  <amp-ad width="320" height="50"
      type="doubleclick"
          data-slot="/4119129/mobile_ad_banner"
          json='{"useSameDomainRenderingUntilDeprecated":1}'>
  </amp-ad>

  <amp-ad width="320" height="50"
      type="doubleclick"
          data-slot="/4119129/mobile_ad_banner"
          data-use-same-domain-rendering-until-deprecated="1">
  </amp-ad>


text:
import React, { Component, PropTypes } from 'react';

export default class Scope extends Component {
  static propTypes = {
    field: PropTypes.object.isRequired,
    value: PropTypes.string.isRequired,
    text: PropTypes.string.isRequired
  }

  onChange = (event) => {
    const { value, field } = this.props;
    field.value = field.value || [ ];

    const index = field.value.indexOf(value);
    if (index < 0) {
      if (event.target.checked) {
        field.onChange(field.value.concat(value));
      }
    } else {
      if (!event.target.checked) {
        const copy = [ ...field.value ];
        copy.splice(index, 1);
        field.onChange(copy);
      }
    }
  }

  isChecked = (field, value) => field.value && field.value.indexOf(value) >= 0;

  render() {
    const { value, text, field } = this.props;
    return (

        <input
          id={value}
          type="checkbox"
          checked={this.isChecked(field, value)}
          onChange={this.onChange}
          defaultValue={value}
          className="scope-input"
        />
        {text}

    );
  }
}


text:
drone-io-experiment
===================
[![Build Status](https://drone.io/github.com/darnould/drone_io_experiment/status.png)](https://drone.io/github.com/darnould/drone_io_experiment/latest)

Messing around with drone.io..