Linked List

This project is abandoned and its default file will likely not work with the most recent version of StarCraft II. Whether this project is out of date or its author has marked it as abandoned, this project is no longer maintained.

//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  *************************************************************
//  *                                                           *
//  *                        Linked List                        *
//  *                                                           *
//  *                                           v1.0.0.1        *
//  *                                           ~Nestharus      *
//  *                                                           *
//  *************************************************************
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  Requires
//
//      paste.sc2mapster.com/assets/dynamic-memory-allocator/
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  Details
//
//      Circular Linked List of Pointers
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  List Constructor
//
//      int listCreate()
//      void listDestroy(int list)
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  Node Getters
//
//      int listGetNext(int node)
//      int listGetPrev(int node)
//      int listGetData(int node)
//
//                            Example
//      ---------------------------------------------------------
//      int node = listGetNext(list);
//      int data;
//
//      while (node != list) {
//          data = listGetData(node);
//          node = listGetNext(node);
//
//          //code
//      }
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  List Getters
//
//      int listGetCount(int list)
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  List Functions
//
//      int listGetCount(int list)
//      bool listHas(int list, int data)
//
//      void listAdd(int list, int data)
//      void listRemove(int list, int data)
//      void listClear(int list)
//
//------------------------------------------------------------------------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  - constants
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
static const int NEXT = 0;
static const int PREV = 1;
static const int DATA = 2;
static const int COUNT = 2;

//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  - getters
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
int listGetNext(int node)       { return memoryInt[node];           }
int listGetPrev(int node)       { return memoryInt[node + 1];       }
int listGetData(int node)       { return memoryInt[node + 2];       }
int listGetCount(int list)      { return memoryInt[list + COUNT];   }

//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  - functions
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
bool listHas(int list, int data) {
    int node = memoryInt[list + NEXT];

    while (node != list && memoryInt[node] != data) { node = memoryInt[node + NEXT]; }

    return memoryInt[node + DATA] == data;
} //listHas

int listCountData(int list, int data) {
    int node = memoryInt[list + NEXT];
    int count = 0;

    while (node != list) {
        if (memoryInt[node + DATA] == data) { count = count + 1; }

        node = memoryInt[node + NEXT];
    } //while

    return count;
} //listCountData

void listAdd(int list, int data) {
    int node    = getMemoryBlockAddress(allocate(3, MEMORY_TYPE_INT), MEMORY_TYPE_INT);
    int last    = memoryInt[list + PREV];

    memoryInt[node + DATA] = data;

    memoryInt[node + NEXT] = list;
    memoryInt[node + PREV] = last;
    memoryInt[last + NEXT] = node;
    memoryInt[list + PREV] = node;

    memoryInt[list + COUNT] = memoryInt[list + COUNT] + 1;
} //listAdd

void listRemove(int list, int data) {
    int node = memoryInt[list + NEXT];

    while (node != list && memoryInt[node + DATA] != data) { node = memoryInt[node + NEXT]; }

    if (memoryInt[node + DATA] != data) { return; }

    memoryInt[memoryInt[node + NEXT] + PREV] = memoryInt[node + PREV];
    memoryInt[memoryInt[node + PREV] + NEXT] = memoryInt[node + NEXT];

    memoryInt[list + COUNT] = memoryInt[list + COUNT] - 1;

    deallocate(getPointerAddress(node, MEMORY_TYPE_INT));
} //listRemove

void listClear(int list) {
    int node = memoryInt[list + NEXT];

    while (node != list) {
        deallocate(getPointerAddress(node, MEMORY_TYPE_INT));
        node = memoryInt[node + NEXT];
    } //while

    memoryInt[list + NEXT] = list;
    memoryInt[list + PREV] = list;

    memoryInt[list + COUNT] = 0;
} //listClear

//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  - constructor
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
int listCreate() {
    int list = getMemoryBlockAddress(allocate(3, MEMORY_TYPE_INT), MEMORY_TYPE_INT);

    memoryInt[list + NEXT] = list;
    memoryInt[list + PREV] = list;

    memoryInt[list + COUNT] = 0;

    return list;
} //listCreate

void listDestroy(int list) {
    listClear(list);

    deallocate(getPointerAddress(list, MEMORY_TYPE_INT));
} //listDestroy

Comments

Posts Quoted:
Reply
Clear All Quotes

About This Project

  • Project ID
    42804
  • Created
    Aug 3, 2012
  • Last Released File
    Aug 3, 2012
  • Total Downloads
    96
  • License

Categories

Members

Recent Files