示例#1
0
void TestAppendItem( void )
{
    UInt16* item1;
    UInt16* item2;
    UInt16* data1;
    UInt16* data2;

    ASSERT( ListIsEmpty( list ));

    data1 = MemPtrNew( sizeof *data1 );
    *data1 = 6;
    ListAppend( list, data1 );
    ASSERT( ListSize( list ) == 1 );
    ASSERT( ! ListIsEmpty( list ));

    data2 = MemPtrNew( sizeof *data2 );
    *data2 = 10;
    ListAppend( list, data2 );
    ASSERT( ListSize( list ) == 2 );

    item1 = ListGet( list, 1 );
    item2 = ListGet( list, 2 );
    ASSERT_UINT16_EQUAL_MSG( "First item: ", 6, *item1 );
    ASSERT_UINT16_EQUAL_MSG( "Second item: ", 10, *item2 );
}
示例#2
0
/* Parse a parameter. Currently only supports normal ones. */
Param ParseParam(VyObj param, bool opt, bool rest){
    VyObj default_val = None();
    if(opt){
        if(IsType(param, TypeCons)){
            VyObj name = ListGet(UNWRAP(param, VyCons), 0);

            default_val = ListGet(UNWRAP(param, VyCons), 0);
            param = name;
        }
    }

    Param p = {optional: opt,
        rest: rest,
        default_value: default_val,
        name: UNWRAP(param, VySymbol)};
    return p;
}

/* Find how many actual parameters there are */
int CountParams(VyObj list){
    int count = 0;
    while(!IsNil(list)){
        if(!ObjEq(Car(list), CreateSymbol("?")) && 
           !ObjEq(Car(list), CreateSymbol("..")))
                count++;

        list = Cdr(list);
    }

    return count;
}
示例#3
0
/* Given the argument list, parse it into ArgList format. */
ArgList ParseArgList(VyObj list){
    int num = CountParams(list);
    Param* params = VyMalloc(sizeof(Param) * num);
    int i;

    /* Each element of the list is either a parameter or an argument mode setting symbol...
     * Check if it sets the argument type (?, ~, or ..), and if it doesn't, parse it as a parameter 
     */
    VyObj opt_arg_set = CreateSymbol("?");
    VyObj rest_arg_set = CreateSymbol("..");

    bool opt, rest;
    opt = rest = false;

    int param_num = 0;
    int arg_list_len = ListLen(list);
    for(i = 0; i < arg_list_len; i++){
        VyObj next = ListGet(UNWRAP(list, VyCons), i);

        if(ObjEq(next, opt_arg_set)){
            opt = true;
        }
        else if(ObjEq(next, rest_arg_set)){
            opt = rest = true;
        }
        else {
            params[param_num] = ParseParam(next, opt, rest);
            param_num++;
        }

    }

    ArgList args = {num_params: num, params: params};
    return args;
}
示例#4
0
void TestGetItem( void )
{
    UInt16* item1;
    UInt16* item2;

    ASSERT( ListIsEmpty( list ));

    AddTwoItems();

    item1 = ListGet( list, 3 );
    ASSERT_MSG( "Item shouldn't be in list: ", item1 == NULL );

    item1 = ListGet( list, 1 );
    item2 = ListGet( list, 2 );
    ASSERT_UINT16_EQUAL_MSG( "First item: ", 6, *item1 );
    ASSERT_UINT16_EQUAL_MSG( "Second item: ", 10, *item2 );

    item1 = ListGet( list, 0 );
    ASSERT_MSG( "Item shouldn't be in list: ", item1 == NULL );
}
static int exec_GetAlpha( filter_t *p_filter,
                          const commandparams_t *p_params,
                          commandparams_t *p_results )
{
    filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
    overlay_t *p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
    if( p_ovl == NULL )
        return VLC_EGENERIC;

    p_results->i_alpha = p_ovl->i_alpha;
    return VLC_SUCCESS;
}
static int exec_GetTextSize( filter_t *p_filter,
                             const commandparams_t *p_params,
                             commandparams_t *p_results )
{
    filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
    overlay_t *p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
    if( p_ovl == NULL )
        return VLC_EGENERIC;

    p_results->fontstyle.i_font_size = p_ovl->p_fontstyle->i_font_size;
    return VLC_SUCCESS;
}
static int exec_GetVisibility( filter_t *p_filter,
                               const commandparams_t *p_params,
                               commandparams_t *p_results )
{
    filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;

    overlay_t *p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
    if( p_ovl == NULL )
        return VLC_EGENERIC;

    p_results->b_visible = p_ovl->b_active ? 1 : 0;
    return VLC_SUCCESS;
}
示例#8
0
void HandleSmoke() {
	for (int i = 0; i < ListSize(&smokeGens); i++) {
		smokeGen* gen = ListGet(&smokeGens, i);

		while (SYS_TimeMillis() - gen->lastSpawn > gen->interval) {
			gen->lastSpawn = SYS_TimeMillis();
			G_AddSmoke(gen->pos, (vec3) {0, 0, 0}, 0.4, 5000);
		}
	}

	for (int i = 0; i < ListSize(&smokeParts); i++) {
		smoke* s = (smoke*) ListGet(&smokeParts, i);
		
		s->timeLeft -= SYS_deltaMillis;
		if (s->timeLeft <= 0) {
			ListRemove(&smokeParts, i);
			i--;
			continue;
		}
		
		G_TickPointPhysics(&s->p, smokeAcc);
		s->radius *= pow(1.2, SYS_deltaMillis / 1000.0);
	}
}
static int exec_SetVisibility( filter_t *p_filter,
                               const commandparams_t *p_params,
                               commandparams_t *p_results )
{
    VLC_UNUSED(p_results);
    filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;

    overlay_t *p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
    if( p_ovl == NULL )
        return VLC_EGENERIC;

    p_ovl->b_active = p_params->b_visible;// ? false : true;
    p_sys->b_updated = true;
    return VLC_SUCCESS;
}
示例#10
0
文件: List.cpp 项目: FangXiaobao/-
int main()
{
    int e;
    MyList l;
    ListCreate(&l);
    ListShow(&l);
    ListGet(&l, 3, &e);
    printf("第三个元素是%d\n",e);
    ListInsert(&l,1, 6);
    printf("在第一个位置插入6后:\n");
    ListShow(&l);
    return 0;
    
    
}
示例#11
0
static int exec_SetTextColor( filter_t *p_filter,
                              const commandparams_t *p_params,
                              commandparams_t *p_results )
{
    VLC_UNUSED(p_results);
    filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;

    overlay_t *p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
    if( p_ovl == NULL )
        return VLC_EGENERIC;

    p_ovl->p_fontstyle->i_font_color = p_params->fontstyle.i_font_color;
    p_sys->b_updated = p_ovl->b_active;
    return VLC_SUCCESS;
}
示例#12
0
static int exec_SetPosition( filter_t *p_filter,
                             const commandparams_t *p_params,
                             commandparams_t *p_results )
{
    VLC_UNUSED(p_results);
    filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;

    overlay_t *p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
    if( p_ovl == NULL )
        return VLC_EGENERIC;

    p_ovl->i_x = p_params->i_x;
    p_ovl->i_y = p_params->i_y;

    p_sys->b_updated = p_ovl->b_active;
    return VLC_SUCCESS;
}
示例#13
0
void LoadScripting() {
	// Initialize the interpreter
	G_luaState = luaL_newstate();
	luaL_openlibs(G_luaState);

	// Get the path
	char* path = malloc(PATH_LENGTH);
	memset(path, 0, PATH_LENGTH);
	SYS_GetResourcePath("/scripts/main.lua", path);

	// Run the file
	if (luaL_dofile(G_luaState, path));

	// Load the engines interface
	G_LoadLuaFunctions();

	// Call the main script's 'init' function
	lua_getglobal(G_luaState, "init");
	char levelPath[PATH_LENGTH];
	SYS_GetLevelPath(L_current.name, levelPath);
	lua_pushstring(G_luaState, levelPath);
	int err = lua_pcall(G_luaState, 1, 0, 0);

	// Add all prop scripts
	for (int i = 0; i < L_current.props.size; i++) {
		prop* p = ListGet(&L_current.props, i);
		G_currentProp = p;

		lua_getglobal(G_luaState, "addProp");
		char propPath[PATH_LENGTH];
		SYS_GetLevelPath(L_current.name, propPath);
		strcat(propPath, "/resources/");
		strcat(propPath, p->res->name);
		strcat(propPath, "/script.lua");
		lua_pushstring(G_luaState, propPath);
		lua_pcall(G_luaState, 1, 0, 0);
	}

	// Check for errors
	if (err != 0) SYS_Warning("A lua error occured!");
}
示例#14
0
// Read input into 'text'
int IN_ReadTextInput(char* text, int length) {
    SDL_Event event;
    if (!IN_readingText) {
        while (SDL_PollEvent(&event));
        return 0;
    }

    int curLength = 0;
    for (int i = 0; text[i];)
        curLength = ++i;

    while (SDL_PollEvent(&event)) {
        switch (event.type) {
        case SDL_TEXTINPUT: {
            char* input = event.text.text;
            for (int i = 0; input[i] && curLength + i < length - 1; i++) {
                text[curLength++ + i] = input[i];
                text[curLength + i] = 0;
            }

            // Make cursor blinking stop when typing
            C_cursorBlinkTimer = SYS_TimeMillis() + C_CONSOLE_BLINKMS;

            break;
        }
        case SDL_KEYDOWN:
            if (event.key.keysym.sym == SDLK_BACKSPACE && curLength >= 0)
                text[--curLength] = 0;
            if (event.key.keysym.sym == SDLK_RETURN && curLength < length)
                text[curLength++] = '\n';
            if (event.key.keysym.sym == SDLK_TAB && curLength < length)
                text[curLength++] = '\t';
            if (event.key.keysym.sym == SDLK_ESCAPE && curLength < length)
                return -1;
            if (event.key.keysym.sym == SDLK_UP) {
                int listSize = ListSize(&C_console.commandHistory);
                if (C_console.selectedRow < listSize - 1) {
                    C_console.selectedRow++;

                    char* line = ListGet(&C_console.commandHistory,
                                         listSize - C_console.selectedRow - 1);

                    C_console.text[0] = 0;
                    for (int i = 0; line[i]; i++) {
                        C_console.text[i] = line[i];
                        C_console.text[i + 1] = 0;
                    }
                }
            }
            if (event.key.keysym.sym == SDLK_DOWN) {
                int listSize = ListSize(&C_console.commandHistory);
                if (C_console.selectedRow > -1) {
                    C_console.selectedRow--;

                    C_console.text[0] = 0;
                    if (C_console.selectedRow > -1) {
                        char* line = ListGet(&C_console.commandHistory,
                                             listSize - C_console.selectedRow - 1);

                        for (int i = 0; line[i]; i++) {
                            C_console.text[i] = line[i];
                            C_console.text[i + 1] = 0;
                        }
                    }
                }
            }

            C_cursorBlinkTimer = SYS_TimeMillis() + C_CONSOLE_BLINKMS;

            break;
        }
    }

    return 0;
}
示例#15
0
int main(int argc, char** argv) {
    SYS_argc = argc;
    SYS_argv = argv;

    // Check command line options
    if (SYS_HasParam("-l")) {
        if (argc != 3) {
            PrintUsage();
            return 0;
        }

        // Load the level and print its data
        L_LoadLevel(argv[argc - 1]);
        printf("Name: %s\n", L_current.name);
        printf("Contains %d resources and %d props\n",
               L_current.res.size, L_current.props.size);

        // Print name of every resource
        printf("\nResources\n");
        for (int i = 0; i < L_current.res.size; i++)
            printf("\t%d:\t%s\n", i, ((resource*)ListGet(&L_current.res, i))->name);

        // Print data of every prop
        printf("\nProps\n");
        for (int i = 0; i < L_current.props.size; i++) {
            prop* p = ListGet(&L_current.props, i);
            printf(
                "\t%d:\tResource: %s\n\t\tPos: %f, %f, %f\n\t\tRot: %f, %f, %f\n",
                i, p->res->name, p->pos[0], p->pos[1], p->pos[2],
                p->rot[0], p->rot[1], p->rot[2]
            );
        }

        return 0;
    }

    // Initialize console variables
    C_Init();

    // Try to open a window with rendering capabilities
    if (!SYS_OpenWindow()) SYS_Error("Failed to open a window with OpenGL Core 3.3");

    // Initialize OpenGL
    V_InitOpenGL();
    fflush(stdout);

    // Initialize main engine
    G_Init();
    fflush(stdout);

    // Initialize rendering system
    V_Init();
    fflush(stdout);

    // Main game loop
    uint32_t current = SYS_TimeMillis();
    uint32_t last = current;
    uint32_t secondTimer = 0, frames = 0;
    while (!SYS_WindowClosed()) {
        last = current;
        current = SYS_TimeMillis();
        SYS_deltaMillis = current - last;
        secondTimer += SYS_deltaMillis;
        frames++;

        if (secondTimer >= 1000) {
            secondTimer -= 1000;

            printf("\rFPS: %d", frames);
            fflush(stdout);
            frames = 0;
        }

        // Update game, then render to screen
        G_Tick();
        V_Tick();
        SYS_UpdateWindow();
    }
    printf("\n");

    G_Quit();
    V_Quit();
    return 0;
}
示例#16
0
/* Find pattern in text string */
static Boolean DoSearch
    (
    Char*   text,           /* text string */
    UInt16  size,           /* size of text */
    Char*   pattern,        /* pattern to search for */
    UInt8   matchMode,      /* indicates if the search should be
                               case-sensitive or not */
    UInt16  xlitMode,       /* how should we transliterate? */
    UInt16* pos,            /* start position, will contain the position
                               of the pattern if found */
    UInt16* endFound,       /* end of position where pattern is found */
    Int16   depth           /* depth of this page */
    )
{
    Int16   startPos;
    Int16   charsize;
    Int16   topcharsize;
    UInt16  patternLen;
    UInt16  wplen;
    Int16   i;
    Int16   j;
    Int16   top;
    WChar   ch;
    WChar   wpattern[ 2 * ( MAX_PATTERN_LEN+1 ) ];
    Boolean guaranteed8Bits;
#ifdef BUILD_ARMLETS
    Boolean        useArmlet;
    void*          DoSearchArmlet;
    ArmSearchType  armData;
    MemHandle      armChunkH = NULL;
#endif
    static UInt8   lastMatchMode = SEARCH_UNINITIALIZED;
    static UInt16  lastXlitMode  = 0;
    static Char    xlat[ 256 ];
    static Boolean multibyteCharFix;
    static WChar   multibyteCharFixTo;
#ifdef SUPPORT_TRANSLITERATION
    static Boolean symmetricXlit;
#endif

    multibyteCharFix = false;
    if ( lastMatchMode != matchMode || xlitMode != lastXlitMode ) {
#ifdef SUPPORT_TRANSLITERATION
        symmetricXlit = SetTransliteration( xlitMode, xlat, &multibyteCharFix, 
                            &multibyteCharFixTo );
#else
        for ( i = 0 ; i < 256 ; i++ ) {
            xlat[ i ] = i;
        }
#endif
        if ( ! ( matchMode & SEARCH_CASESENSITIVE ) ) {
            for ( i = 0; i < 256; i++ )
                xlat[ i ] = TxtGlueLowerChar( ( UInt8 )xlat[ i ] );
        }
        lastMatchMode = matchMode;
    }

    startPos    = *pos;
    charsize    = 1;
    patternLen  = StrLen( pattern );

    /* expand wchar string in advance*/
    i = wplen = 0;
    while ( i < patternLen ) {
        charsize = TxtGlueGetNextChar( pattern, i, &ch );
        if ( charsize == 1 ) {
#ifdef SUPPORT_TRANSLITERATION
            if ( symmetricXlit ) {
#endif
                ch = xlat[ (UInt8) ch ];
#ifdef SUPPORT_TRANSLITERATION
            }
            else if ( ! ( matchMode & SEARCH_CASESENSITIVE ) ) {
                ch = TxtGlueLowerChar( (UInt8) ch );
            }
#endif
        }
        else if ( multibyteCharFix )
            ch = multibyteCharFixTo;

        wpattern[ wplen ] = ch;

        i       += charsize;
        wplen   += 1;
    }

    guaranteed8Bits = DeviceUses8BitChars();
#ifdef BUILD_ARMLETS
    useArmlet       = false;
    DoSearchArmlet  = NULL;

    if ( SupportArmlets() && guaranteed8Bits ) {
        armChunkH = DmGetResource( ArmletResourceType, armDoSearch );
        if ( armChunkH != NULL ) {
            DoSearchArmlet = MemHandleLock( armChunkH );

            armData.size     = size;
            armData.depth    = depth;
            armData.text     = text;
            armData.wpattern = wpattern;
            armData.wplen    = wplen;
            armData.xlat     = xlat;

            useArmlet = true;
        }
    }
#endif

    i           = top = *pos;
    j           = 0;
    topcharsize = 0;
    while ( j < wplen && i < size ) {
#ifdef BUILD_ARMLETS
        if ( useArmlet ) {
            armData.i     = i;
            armData.j     = j;
            armData.top   = top;

            ch    = (WChar) PceNativeCall( DoSearchArmlet, &armData );

            i     = armData.i;
            j     = armData.j;
            top   = armData.top;

            if ( wplen <= j || size <= i )
                break;
            charsize = topcharsize = 1;
        }
        else
#endif
        if ( guaranteed8Bits ) {
            ch = DoSearch8BitText( text, size, wpattern, wplen, xlat,
                     &i, &j, &top, 0 < depth );
            if ( wplen <= j || size <= i )
                break;
            charsize = topcharsize = 1;
        }
        else
            charsize = TxtGlueGetNextChar( text, i, &ch );

        if ( j == 0 )
            topcharsize = charsize;

        if ( ch == '\0' ) {
            if ( 0 < depth ) {
                /* if function is anchor, store it to queue */
                if ( text[ i + 1 ] == 0x0A /* ANCHOR_START */ ){
                    UInt16 reference;
                    Int16  q;
                    Int16  listSize;

                    reference = 256 * (UInt8)text[ i + 2 ] +
                        (UInt8)text[ i + 3 ];

                    listSize = ListSize( searchQueue );
                    for ( q = 0; q < listSize; q ++ ){
                        /*look up in queue, append if not exist*/
                        QueueNode *node;
                        node = ListGet( searchQueue, q + 1 );
                        if ( node->reference == reference )
                            break;
                    }
                    if ( q == listSize )
                        AddReference( reference, depth );

                }
            }
            i  += 2 + ( text[ i + 1 ] & 0x07 );
            top = i;
            j   = 0;
            continue;
        }
        if ( charsize == 1 )
            ch = xlat[ (UInt8) ch ];
        else if ( multibyteCharFix )
            ch = multibyteCharFixTo;
        if ( ch != wpattern[ j ] ) {
            top += topcharsize;
            i    = top;
            j    = 0;
            continue;
        }
        i += charsize;
        j += 1;
    }
#ifdef BUILD_ARMLETS
    if ( armChunkH != NULL ) {
        MemHandleUnlock( armChunkH );
        DmReleaseResource( armChunkH );
    }
#endif
    if ( j == wplen ) {
        *pos      = top;
        *endFound = i;
        return true;
    }
    else {
        return false;
    }
}
示例#17
0
/*****************************************************************************
 * Command functions
 *****************************************************************************/
static int exec_DataSharedMem( filter_t *p_filter,
                               const commandparams_t *p_params,
                               commandparams_t *p_results )
{
#if defined(HAVE_SYS_SHM_H)
    filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
    struct shmid_ds shminfo;
    overlay_t *p_ovl;
    size_t i_size;

    VLC_UNUSED(p_results);

    p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
    if( p_ovl == NULL )
    {
        msg_Err( p_filter, "Invalid overlay: %d", p_params->i_id );
        return VLC_EGENERIC;
    }

    if( shmctl( p_params->i_shmid, IPC_STAT, &shminfo ) == -1 )
    {
        msg_Err( p_filter, "Unable to access shared memory" );
        return VLC_EGENERIC;
    }
    i_size = shminfo.shm_segsz;

    if( p_params->fourcc == VLC_CODEC_TEXT )
    {
        char *p_data;

        if( (p_params->i_height != 1) || (p_params->i_width < 1) )
        {
            msg_Err( p_filter,
                     "Invalid width and/or height. when specifying text height "
                     "must be 1 and width the number of bytes in the string, "
                     "including the null terminator" );
            return VLC_EGENERIC;
        }

        if( (size_t)p_params->i_width > i_size )
        {
            msg_Err( p_filter,
                     "Insufficient data in shared memory. need %d, got %zu",
                     p_params->i_width, i_size );
            return VLC_EGENERIC;
        }

        p_ovl->data.p_text = malloc( p_params->i_width );
        if( p_ovl->data.p_text == NULL )
        {
            msg_Err( p_filter, "Unable to allocate string storage" );
            return VLC_ENOMEM;
        }

        video_format_Setup( &p_ovl->format, VLC_CODEC_TEXT,
                            0, 0, 0, 1 );

        p_data = shmat( p_params->i_shmid, NULL, SHM_RDONLY );
        if( p_data == NULL )
        {
            msg_Err( p_filter, "Unable to attach to shared memory" );
            free( p_ovl->data.p_text );
            p_ovl->data.p_text = NULL;
            return VLC_ENOMEM;
        }
        memcpy( p_ovl->data.p_text, p_data, p_params->i_width );

        shmdt( p_data );
    }
    else
    {
        uint8_t *p_data, *p_in;
        size_t i_neededsize = 0;

        p_ovl->data.p_pic = picture_New( p_params->fourcc,
                                         p_params->i_width, p_params->i_height,
                                         1, 1 );
        if( p_ovl->data.p_pic == NULL )
            return VLC_ENOMEM;

        p_ovl->format = p_ovl->data.p_pic->format;

        for( size_t i_plane = 0; i_plane < (size_t)p_ovl->data.p_pic->i_planes;
             ++i_plane )
        {
            i_neededsize += p_ovl->data.p_pic->p[i_plane].i_visible_lines *
                            p_ovl->data.p_pic->p[i_plane].i_visible_pitch;
        }

        if( i_neededsize > i_size )
        {
            msg_Err( p_filter,
                     "Insufficient data in shared memory. need %zu, got %zu",
                     i_neededsize, i_size );
            picture_Release( p_ovl->data.p_pic );
            p_ovl->data.p_pic = NULL;
            return VLC_EGENERIC;
        }

        p_data = shmat( p_params->i_shmid, NULL, SHM_RDONLY );
        if( p_data == NULL )
        {
            msg_Err( p_filter, "Unable to attach to shared memory" );
            picture_Release( p_ovl->data.p_pic );
            p_ovl->data.p_pic = NULL;
            return VLC_ENOMEM;
        }

        p_in = p_data;
        for( size_t i_plane = 0; i_plane < (size_t)p_ovl->data.p_pic->i_planes;
             ++i_plane )
        {
            uint8_t *p_out = p_ovl->data.p_pic->p[i_plane].p_pixels;
            for( size_t i_line = 0;
                 i_line < (size_t)p_ovl->data.p_pic->p[i_plane].i_visible_lines;
                 ++i_line )
            {
                memcpy( p_out, p_in,
                            p_ovl->data.p_pic->p[i_plane].i_visible_pitch );
                p_out += p_ovl->data.p_pic->p[i_plane].i_pitch;
                p_in += p_ovl->data.p_pic->p[i_plane].i_visible_pitch;
            }
        }
        shmdt( p_data );
    }
    p_sys->b_updated = p_ovl->b_active;

    return VLC_SUCCESS;
#else
    VLC_UNUSED(p_params);
    VLC_UNUSED(p_results);

    msg_Err( p_filter, "system doesn't support shared memory" );
    return VLC_EGENERIC;
#endif
}
示例#18
0
static void TestList()
{
	uint i, a;
	Thing S, T, U, W, X;
	Thing things[MAXCOUNT];
	List L;
	a=-93;
	W = Word(a);
	
	CommentLine("Test List");

	L = NewList(); /* insert and empty list */
	DelList(L); /* delete an empty list */

	/* insert into a list, delete a list make sure thing is ok */
	L = NewList();
	ListIns(L, W);
	DelList(L);
	assert(a == IntWord(W));

	L = NewList();
	for (i=0; i<MAXCOUNT; i++)
	{
		T = Word(i);
		things[i]=T;
		//printf("made: >>%s<<\n", ThingToString(T));
		ListIns(L, T);
	}
	//asm("int3");
	String s1, s2;
	for (i=0; i<MAXCOUNT; i++)
	{
		// seperate assignments are NECESSARY because if i assign
		// inline with strcmp(ThingToString ... ) they will never 
		// get deleted and it will be a memory leak!!
		s1 = ThingToString(things[i]);
		s2 = ThingToString(ListGet(L, things[i]));
		assert(strcmp(s1, s2)==0);
		/*
		if (strcmp(s1, s2) != 0)
		{
			
			printf("*** no match\n");
			printf("%s ",ThingToString(things[i]));
			printf("!= %s\n", ThingToString(ListGet(L, things[i])));
		}
		*/
		DelStr(s1); // sigh, there is no real way around this.
		DelStr(s2); 
	}
	DelList(L);
	fflush(stdout);

	/* S, T, X are unused */
	L = NewList();
	S = Word(-64);
	U = Word(64);
	ListIns(L, S);  // memory leak but these are just tests.
	T = Word(-64);
	assert(SameThing(S, T));
	assert(SameThing(S, U) == false);

	X = ListRm(L, T);
	DelList(L);

	/* X is unused */
	L = NewList();
	for (i = 0; i < MAXCOUNT; i++) 
	{
		W = Word(i);
		ListIns(L, W);
	}
	assert(GetListSize(L) == MAXCOUNT);
	 
	a = GetListSize(L);

	for (i = 0; i < MAXCOUNT; i++) 
	{
		a -= 1;
		W = Word(i);
		assert(i == IntWord(ListRm(L, W)));
		assert(GetListSize(L) == a);
		//printf("a: %d, size: %d\n", a, GetListSize(L));
	}
	DelList(L);

	L = NewList();
	assert(SameThing(S, W) == false);
	ListIns(L, T);
	i = GetListSize(L);
	X = ListRm(L, T);
	assert(GetListSize(L) == (i - 1));
	assert(SameThing(X, T) == true);
	/* check to make sure it's not in there anymore */
	assert(ListRm(L, T) == NULL);


	for (i = 0; i < MAXCOUNT; i++) { ListIns(L, T); }

	assert(GetListSize(L) == MAXCOUNT);

	for (i = 0; i < MAXCOUNT; i++) 
	{ 
		X = ListRm (L, T); 
		assert(X != NULL);
	}
	assert(GetListSize(L) == 0);
	assert(ListRm(L, T) == NULL);

	DelList(L);
	/* these are the only ones we actually made new */
	/* W is a, S is -64, T= -64 */
	DelThing(S);
	DelThing(T);
	DelThing(U);
	DelThing(W);

	printf("Done TestList()\n");
}
示例#19
0
d
int main() {
    int n = 10;

    cout << "<1>求顺序表中第i个元素(函数),若不存在,报错。" << endl;
    // 顺序表长度n≥10,i分别为5,n,0,n+1,n+2
    SeqList L1_1 = GenerateSeqList(1, 10, n);
    cout << "第一组数据:";
    DisplayArray(L1_1);
    cout << "i=5  :  " << ListGet(&L1_1, 5) << endl;
    cout << "i=n  :  " << ListGet(&L1_1, n) << endl;
    cout << "i=0  :  " << ListGet(&L1_1, 0) << endl;
    cout << "i=n+1:  " << ListGet(&L1_1, n + 1) << endl;
    cout << "i=n+2:  " << ListGet(&L1_1, n + 2) << endl;

    // 顺序表长度n=0,i分别为0,2
    SeqList L1_2 = {{}, 0};
    cout << "第二组数据:";
    DisplayArray(L1_2);
    cout << "i=0:  " << ListGet(&L1_2, 0) << endl;
    cout << "i=2:  " << ListGet(&L1_2, 2) << endl;

    cout << "\n<2>在第i个结点前插入值为x的结点。" << endl;
    // 顺序表长度n≥10,i分别为5,n,0,n+1,n+2
    cout << "第一组数据:";
    DisplayArray(L1_1);

    int x = 100;
    cout << "i=5  :  ";
    DisplayArray(ListInsert(L1_1, x, 5));
    cout << "i=n  :  ";
    DisplayArray(ListInsert(L1_1, x, n));
    cout << "i=n+1:  ";
    DisplayArray(ListInsert(L1_1, x, n + 1));
    cout << "i=0  :  ";
    DisplayArray(ListInsert(L1_1, x, 0));
    cout << "i=1  :  ";
    DisplayArray(ListInsert(L1_1, x, 1));
    cout << "i=n+2:  ";
    DisplayArray(ListInsert(L1_1, x, n + 2));

    // 顺序表长度n=0,i=5
    cout << "第二组数据:";
    DisplayArray(L1_2);
    cout << "i=5:  ";
    DisplayArray(ListInsert(L1_2, x, 5));

    cout << "\n<3>删除顺序表中第i个元素结点。" << endl;
    // 顺序表长度n≥10,i分别为5,n,0,1,n+1,0
    SeqList L3_1 = GenerateSeqList(1, 10, n);
    cout << "第一组数据:";
    DisplayArray(L3_1);
    cout << "i=5  :  ";
    DisplayArray(ListDelete(L3_1, 5));
    cout << "i=n  :  ";
    DisplayArray(ListDelete(L3_1, n));
    cout << "i=1  :  ";
    DisplayArray(ListDelete(L3_1, 1));
    cout << "i=n+1:  ";
    DisplayArray(ListDelete(L3_1, n + 1));
    cout << "i=0  :  ";
    DisplayArray(ListDelete(L3_1, 0));

    // 顺序表长度n=0,i=5
    cout << "第二组数据:";
    DisplayArray(L1_2);
    cout << "i=5:  ";
    DisplayArray(ListDelete(L1_2, 5));

    cout << "\n<4>在一个递增有序的顺序表L中插入一个值为x的元素,并保持其递增有序特性。" << endl;
    SeqList L4_1 = {{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}, 10};
    cout << "x=25 :  ";
    DisplayArray(IncreaseListInsert(L4_1, 25));
    cout << "x=85 :  ";
    DisplayArray(IncreaseListInsert(L4_1, 85));
    cout << "x=110:  ";
    DisplayArray(IncreaseListInsert(L4_1, 110));
    cout << "x=8  :  ";
    DisplayArray(IncreaseListInsert(L4_1, 8));

    cout << "\n<5>将顺序表L中的奇数项和偶数项结点分解开(元素值为奇数、偶数),分别放入新的顺序表中,\n然后原表和新表元素同时输出到屏幕上,以便对照求解结果。" << endl;
    SeqList OddL, EvenL;
    cout << "第一组数据:";
    SeqList L5_1 = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60}, 15};
    DisplayArray(L5_1);
    ClassifyOddEven(&L5_1, &OddL, &EvenL);
    cout << "奇数: ";
    DisplayArray(OddL);
    cout << "偶数: ";
    DisplayArray(EvenL);
    cout << "第二组数据:";
    SeqList L5_2 = {{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}, 10};
    DisplayArray(L5_2);
    ClassifyOddEven(&L5_2, &OddL, &EvenL);
    cout << "奇数: ";
    DisplayArray(OddL);
    cout << "偶数: ";
    DisplayArray(EvenL);

    cout << "\n<6>求两个递增有序顺序表L1和L2中的公共元素,放入新的顺序表L3中。" << endl;
    SeqList SameL;
    cout << "第一组数据:" << endl;
    SeqList L6_1_1 = {{1, 3, 6, 10, 15, 16, 17, 18, 19, 20}, 10};
    SeqList L6_1_2 = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 18, 20, 30}, 13};
    DisplayArray(L6_1_1);
    DisplayArray(L6_1_2);
    GetSameElements(&L6_1_1, &L6_1_2, &SameL);
    cout << "合并结果: ";
    DisplayArray(SameL);

    cout << "第二组数据:" << endl;
    SeqList L6_2_1 = {{1, 3, 6, 10, 15, 16, 17, 18, 19, 20}, 10};
    SeqList L6_2_2 = {{2, 4, 5, 7, 8, 9, 12, 22}, 8};
    DisplayArray(L6_2_1);
    DisplayArray(L6_2_2);
    GetSameElements(&L6_2_1, &L6_2_2, &SameL);
    cout << "合并结果: ";
    DisplayArray(SameL);

    cout << "第三组数据:" << endl;
    SeqList L6_3_1 = {{}, 0};
    SeqList L6_3_2 = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 10};
    DisplayArray(L6_3_1);
    DisplayArray(L6_3_2);
    GetSameElements(&L6_3_1, &L6_3_2, &SameL);
    cout << "合并结果: ";
    DisplayArray(SameL);

    cout << "\n<7>删除递增有序顺序表中的重复元素,并统计移动元素次数,要求时间性能最好。" << endl;
    unsigned step = 0;
    cout << "第一组数据: ";
    SeqList L7_1 = {{1, 2, 3, 4, 5, 6, 7, 8, 9}, 9};
    DisplayArray(L7_1);
    cout << "删除结果: ";
    DisplayArray(DeleteSame(&L7_1, step), true, false);
    cout << " step: " << step << endl;

    cout << "第二组数据: ";
    SeqList L7_2 = {{1, 1, 2, 2, 2, 3, 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9}, 17};
    DisplayArray(L7_2);
    cout << "删除结果: ";
    DisplayArray(DeleteSame(&L7_2, step), true, false);
    cout << " step: " << step << endl;

    cout << "第三组数据: ";
    SeqList L7_3 = {{1, 2, 3, 4, 5, 5, 6, 7, 8, 8, 9, 9, 9, 9, 9}, 15};
    DisplayArray(L7_3);
    cout << "删除结果: ";
    DisplayArray(DeleteSame(&L7_3, step), true, false);
    cout << " step: " << step << endl;
    return 0;
}