Topic:   programming TI-BASIC   (Read 26522 times)


0 Members and 1 Guest are viewing this topic.

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: programming TI-BASIC
« Reply #45 on: November 05, 2011, 11:09:47 PM »
Mmm hmm. I remember a programmer friend tellin me some memory command was unreliable. Coulda swore it was realloc.
Anywho I'll post up some of my C code soonish(when I get back to my Mac).

x


  • GMG-er

  • **


  • Posts: 247
Re: programming TI-BASIC
« Reply #46 on: November 06, 2011, 02:04:50 AM »
Quote
Mmm hmm. I remember a programmer friend tellin me some memory command was unreliable. Coulda swore it was realloc.
Anywho I'll post up some of my C code soonish(when I get back to my Mac).

Either he was wrong, or you misinterpreted him. It wouldn't be part of the standard library if it were unreliable.

The built in C++ implementation of a dynamic array (vector) uses realloc -- I have seen hundreds of projects that use vectors, and I am sure there are hundreds of thousands of C++ programs that have used it, as its part of the standard library. I highly doubt realloc is unreliable.

Besides if you really don't trust it look at the source code for it, its all open source. In fact you could even write your own realloc if you want. All you have to do is malloc the new size then copy all the elements over -- its really very simple.
« Last Edit: November 06, 2011, 02:12:38 AM by x »

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: programming TI-BASIC
« Reply #47 on: November 06, 2011, 12:50:19 PM »
thats what I did ;D

made a varient of realloc using pointers, calloc, and malloc.
It'd return a character pointer one slot bigger than the latter one.
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: programming TI-BASIC
« Reply #48 on: November 06, 2011, 03:07:08 PM »
That's what I did, malloc a new array, copy stuff.

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: programming TI-BASIC
« Reply #49 on: November 07, 2011, 02:20:47 PM »
Here's a mutable array I made in C.
Code: [Select]
//
//  MutableArray.c
//  Avisaria Collision Demo
//
//  Created by Matthew French on 9/24/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <stdio.h>
#import <stdlib.h>

typedef struct MutableArray MutableArray;

static inline MutableArray* MutableArrayMake();
static inline void MutableArrayAdd(MutableArray* m,void* object);
static inline void MutableArrayRemove(MutableArray* m,void* object);
static inline void MutableArrayRemoveAllObjects(MutableArray* m);
static inline int MutableArrayIndexOfObject(MutableArray* m,void* object);
static inline void* MutableArrayObjectAtIndex(MutableArray* m,int index);
static inline void MutableArrayFree(MutableArray* m);

struct MutableArray {
    int count;
    void** objects;
};

static inline MutableArray* MutableArrayMake() {
    MutableArray* m = (MutableArray*)malloc(sizeof(MutableArray));
    
    m->count = 0;
    m->objects = NULL;
    
    return m;
}

static inline void MutableArrayAdd(MutableArray* m,void* object) {
    m->count += 1;
    void** new = (void**)malloc(m->count*sizeof(void*));
    if (m->objects!=NULL) {
        for (int i = 0; i < m->count-1; i++) {
            new[i] = m->objects[i];
        }
        free(m->objects);
        m->objects = NULL;
    }
    new[m->count-1] = object;
    
    m->objects = new;
}

static inline void MutableArrayRemove(MutableArray* m,void* object) {
    int n = 0;
    for (int i = 0; i < m->count; i++) {
        if (m->objects[i] == object) {
            n++;
        }
    }
    int r = 0;
    void** new = (void**)malloc((m->count - n)*sizeof(void*));
    for (int i = 0; i < m->count; i++) {
        if (m->objects[i] != object) {
            new[i-r] = m->objects[i];
        } else {
            r ++;
        }
    }
    free(m->objects);
    m->objects = new;
    m->count -= n;
}

static inline void MutableArrayRemoveAllObjects(MutableArray* m) {
    m->count = 0;
    free(m->objects);
    m->objects = NULL;
}

static inline int MutableArrayIndexOfObject(MutableArray* m,void* object) {
    for (int i = 0; i < m->count; i++) {
        if (m->objects[i] == object) {
            return i;
        }
    }
    return -1;
}

static inline void* MutableArrayObjectAtIndex(MutableArray* m,int index) {
    return m->objects[index];
}

static inline void MutableArrayFree(MutableArray* m) {
    if (m->count > 0) {
        free(m->objects);
    }
    free(m);
}

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: programming TI-BASIC
« Reply #50 on: November 07, 2011, 08:07:36 PM »
Awesome, I'll have to look at it more in depth in the morning.
It almost looks C++ish.
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

x


  • GMG-er

  • **


  • Posts: 247
Re: programming TI-BASIC
« Reply #51 on: November 07, 2011, 09:46:41 PM »
Quote
Awesome, I'll have to look at it more in depth in the morning.
It almost looks C++ish.

No, this would be C++ish:
Code: [Select]
vector<int> v;

Also @Gan: Defining your functions as static is redundant since you have not static variables in them. The only thing its going to do its make it harder for a compiler to optimize your code. In addition your free function and the remove all function have a memory leak. Also you should never use "import", its depreciated. Not to be rude, but what is the point of this? All the operations are O(n) because you re-create the array every time. This means your data structure is extremely slow and thus useless compared to a linked list, or a normal vector, (both of which have add and remove as O(1)) for example.


Example of memory leak:
Code: [Select]
//
//  MutableArray.c
//  Avisaria Collision Demo
//
//  Created by Matthew French on 9/24/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>

typedef struct MutableArray MutableArray;

static inline MutableArray* MutableArrayMake();
static inline void MutableArrayAdd(MutableArray* m,void* object);
static inline void MutableArrayRemove(MutableArray* m,void* object);
static inline void MutableArrayRemoveAllObjects(MutableArray* m);
static inline int MutableArrayIndexOfObject(MutableArray* m,void* object);
static inline void* MutableArrayObjectAtIndex(MutableArray* m,int index);
static inline void MutableArrayFree(MutableArray* m);

struct MutableArray {
 Â   int count;
 Â   void** objects;
};

static inline MutableArray* MutableArrayMake() {
 Â   MutableArray* m = (MutableArray*)malloc(sizeof(MutableArray));

 Â   m->count = 0;
 Â   m->objects = NULL;

 Â   return m;
}

static inline void MutableArrayAdd(MutableArray* m,void* object) {
 Â   m->count += 1;
 Â   void** new = (void**)malloc(m->count*sizeof(void*));
 Â   if (m->objects!=NULL) {
 Â       int i;
 Â  for ( i = 0; i < m->count-1; i++) {
 Â new[i] = m->objects[i];
 Â  }
 Â  free(m->objects);
 Â  m->objects = NULL;
 Â   }
 Â   new[m->count-1] = object;

 Â   m->objects = new;
}

static inline void MutableArrayRemove(MutableArray* m,void* object) {
 Â   int n = 0;
 Â   int i;
 Â   for (i = 0; i < m->count; i++) {
 Â  if (m->objects[i] == object) {
 Â n++;
 Â  }
 Â   }
 Â   int r = 0;
 Â   void** new = (void**)malloc((m->count - n)*sizeof(void*));
 Â   for (i = 0; i < m->count; i++) {
 Â  if (m->objects[i] != object) {
 Â new[i-r] = m->objects[i];
 Â  } else {
 Â r ++;
 Â  }
 Â   }
 Â   free(m->objects);
 Â   m->objects = new;
 Â   m->count -= n;
}

static inline void MutableArrayRemoveAllObjects(MutableArray* m) {
 Â   m->count = 0;
 Â   free(m->objects);
 Â   m->objects = NULL;
}

static inline int MutableArrayIndexOfObject(MutableArray* m,void* object) {
 Â   int i;
 Â   for (i = 0; i < m->count; i++) {
 Â  if (m->objects[i] == object) {
 Â return i;
 Â  }
 Â   }
 Â   return -1;
}

static inline void* MutableArrayObjectAtIndex(MutableArray* m,int index) {
 Â   return m->objects[index];
}

static inline void MutableArrayFree(MutableArray* m) {
 Â   if (m->count > 0) {
 Â  free(m->objects);
 Â   }
 Â   free(m);
}

void print(MutableArray* m)
{
 Â   int i;
 Â   for (i = 0; i < m->count; i++)
 Â       puts((char*)m->objects[i]);
}

int main()
{
 Â   MutableArray* m;
 Â   while (getchar() != 'z') {
 Â       m = MutableArrayMake();
 Â       int i;
 Â       for(i = 0; i < 10000; i++) {
 Â           MutableArrayAdd(m, malloc(100));
 Â           MutableArrayAdd(m, malloc(100));
 Â           MutableArrayAdd(m, malloc(100));

 Â       }
 Â       MutableArrayRemoveAllObjects(m);
 Â       MutableArrayFree(m);
 Â   }
 Â   return 0;
}


Run it, and keep pressing return. Watch the memory usage go up and up. This is probably because you are freeing the objects pointer, but not everything it points to (since its basically an array of other pointers). You could fix this with a simple:
Code: [Select]
int i;
for (i = 0; i < m->count; i++)
 Â           free(m->objects[i]);
« Last Edit: November 07, 2011, 10:45:36 PM by x »

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: programming TI-BASIC
« Reply #52 on: November 07, 2011, 10:58:56 PM »
Nah, it's up to the user to free the objects they put in it. Cause as far as I know they could be referenced elsewhere.

As for the static... for some reason the compiler gets really angry when I un-static them.

As for the import, I got that in there cause I'm mincing C and Obj-C. Even though it probably makes no difference...

As for it being slow... well it seems to be much faster than an NSMutableArray. I also don't like linked lists, just preference.

Yeah, only reason I've taken the initiative to learn and write some plain C code is to speed up some algorithms and stuff in a project.
So far it works. Use to run 4,000 sheep at 100% CPU. Now it handles 17,000 sheep at 80-90% CPU.
I've turned a ton of my Obj-C code into just C. Runs a lot faster and it's nice how both can run side by side.

x


  • GMG-er

  • **


  • Posts: 247
Re: programming TI-BASIC
« Reply #53 on: November 08, 2011, 12:15:55 AM »
Quote
Nah, it's up to the user to free the objects they put in it. Cause as far as I know they could be referenced elsewhere.

As for the static... for some reason the compiler gets really angry when I un-static them.

As for the import, I got that in there cause I'm mincing C and Obj-C. Even though it probably makes no difference...

As for it being slow... well it seems to be much faster than an NSMutableArray. I also don't like linked lists, just preference.

Yeah, only reason I've taken the initiative to learn and write some plain C code is to speed up some algorithms and stuff in a project.
So far it works. Use to run 4,000 sheep at 100% CPU. Now it handles 17,000 sheep at 80-90% CPU.
I've turned a ton of my Obj-C code into just C. Runs a lot faster and it's nice how both can run side by side.

You do realize it would be much faster if you didn't have to iterate over the entire array whenever you added or removed something right?

Also I don't know how you are getting 100% cpu usage without multi-threading (assuming you have a multicore CPU).

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: programming TI-BASIC
« Reply #54 on: November 08, 2011, 07:29:54 AM »
Code: [Select]
You do realize it would be much faster if you didn't have to iterate over the entire array whenever you added or removed something right? 
Yeah probs, though a linked list would up the complexity. It'd hafta be wrapped to work just like a mutable array if I were to use one.
Imagine tryin to get a single object at some index, it'd have to iterate through quite a few. I'm not sure it'd be faster when I have a ton of collision tests, targeting and AI behaviors. Though adding a object certainly would be faster.
Then again so far I only add objects at the beginning, haven't tested removing and adding sheep during the game.
It'd take some testing to see if it's worth it.

As for the CPU usage, that's a good point. I'm gonna have to test it again to figure out why it does that.

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: programming TI-BASIC
« Reply #55 on: November 08, 2011, 06:18:50 PM »
Quote

As for the import, I got that in there cause I'm mincing C and Obj-C. Even though it probably makes no difference...

Aha! I knew it looked a bit more object-oriented than the C I'm used too!

Quote
As for it being slow... well it seems to be much faster than an NSMutableArray. I also don't like linked lists, just preference.

I like linked lists myself, but I can't find a practical usage for them something else doesn't already do faster :/

So not to change the subject or anything, but I 1/2 finished The function that I've been working on forever. It works fine except it's only accurate to a tenth, and freaks out, if it's given a string without a decimal in it. (which is why I made my own realloc commmand... still working on that)

Code: [Select]

#define DIGIT (c[array] - 48)

#define SLOTS (numberofsections(string))

#define AZERO (numberofsectionsabovezero(string))

#define BZERO (numberofsectionsbelowzero(string))

#define RESET place = -array + AZERO

float stringtonumber(char string[]) {
      
      float      f = 0;
      int            array, place;
      float      exponent = 1;
                  
      for (array = 0; array >= -BZERO && array < SLOTS ; array++) {

            place = RESET;

            if (string[array] == '.')
                  {continue;}
                  
            if (place > 0)
            {
                  for (place = RESET, exponent = 1; place != 1; --place)
                        {exponent *= 10;}
            }
            else if (place < 0)
            {
                  for (place = RESET, exponent = 1; place != 0; ++place)//else is important
                        {exponent /= 10;}
            }
            
            f = f + DIGIT * exponent; //add to final
            printf("f evaluated to: %f\\n\\n",f);
      }
      // divide to move up decimal point by # of sections under 0

return(f);
}
I'm quite proud of it ;D
I've spent half a month working on it.
« Last Edit: November 08, 2011, 06:20:06 PM by KurtManion »
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

x


  • GMG-er

  • **


  • Posts: 247
Re: programming TI-BASIC
« Reply #56 on: November 08, 2011, 06:55:47 PM »
I think you guys have some misinterpretations ranging from small to moderate.

But firstly, I hate to do this but... http://www.cplusplus.com/reference/clibrary/cstdlib/atof/
Take a look at that kurt.

@Gan - Linked lists allow everything in O(1) time but indexed access. The point is that you use them for anything but indexed access. The power of a list is that it can grow indefinitely without having to be reallocated memory (which is EXTREMELY slow), and removing items from anywhere is fast. The trick to removing the item is storing a reference to the link. For example in linked lists I have coded (below is pseudocode) adding a new item returns a reference (or pointer) to the link.
Link l = list.addFirst(object);
list.remove(l);
That operation is O(1).

You said you are hesitant to use a linked list since remove has to iterate over many of the items in the list. Thats true, but your mutable array already has to iterate over EVERY item in the array, so I don't see the problem. The only difference is that adding in the linked list will be fast...

Also why dont you implement a vector. Which is like a mutable array except much faster, albeit slightly limited. It allows O(1) indexed access, you can add and remove from the ends of the vector (front and back) in amortized O(1) time, and it can grow to any size. Its built into the C++ library. Removing from the middle is O(n) time (like everything in yours) since you have to shuffle the array items along to fill the hole the removed item makes.

I think a mutable array may not be the best option for storing lots of entities anyway. Explain exactly what you are doing with it, and I might be able to point you in the direction of a better data structure.

@Kurt.
I am hard pressed not to find a use for lists. I have used them countless times for game development. This is because they give me O(1) times for pretty much everything I need, and they never need to be re-sized.

I usually use them to store an entity list of game objects.

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: programming TI-BASIC
« Reply #57 on: November 09, 2011, 05:44:41 PM »
... :'(
damn.

oh well it was quite an experience making it from scratch at least ;D

I've never thought of using doubly linked lists that way. Cool.
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: programming TI-BASIC
« Reply #58 on: November 17, 2011, 07:22:04 PM »
where is the stdlib.h file I want to read the atof() documentation. I want to see how similar mine, and Next's functions are.
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

GabrielCA


  • GMG-er

  • **

  • no avatar

  • Posts: 224
Re: programming TI-BASIC
« Reply #59 on: November 18, 2011, 04:05:19 PM »
Quote
where is the stdlib.h file I want to read the atof() documentation. I want to see how similar mine, and Next's functions are.
Apple Documentation: http://developer.apple.com/library/IOs/#documentation/System/Conceptual/ManPages_iPhoneOS/man3/atof.3.html
Implementation: http://minnie.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/libc/gen/atof.c
The implementation given above is from Unix V7, written in 1979; it is the "original" one, so to speak.

This is the atof implementation used in the latest Mac OS X release -- it just calls strtod: http://opensource.apple.com/source/Libc/Libc-763.12/stdlib/atof-fbsd.c

This is the GNU implementation used by Linux. It also just calls strtod:
http://sourceware.org/git/?p=glibc.git;a=blob_plain;f=stdlib/atof.c;hb=master

I guess you'll have to read the strtod source code  ;D

Here is very annotated version of that, used by Apple: http://opensource.apple.com/source/tcl/tcl-10/tcl/compat/strtod.c It is very neat and legible; I think this is what you're looking for.

Also I think you probably don't want to read an stdlib.h file because they tend to full of garbage.
« Last Edit: November 18, 2011, 04:35:35 PM by GabrielCA »
Creator of the deprecated plugin KeyDetect (2005)