about summary refs log tree commit diff stats
path: root/screen.mu
Commit message (Expand)AuthorAgeFilesLines
* 4262 - literal 'null'Kartik Agaram2018-06-171-19/+19
* 3861 - screen untouched when entering console modeKartik K. Agaram2017-05-181-0/+1
* 3851Kartik K. Agaram2017-05-101-1/+1
* 3380Kartik K. Agaram2016-09-171-1/+1
* 3379Kartik K. Agaram2016-09-171-4/+4
* 3189Kartik K. Agaram2016-08-141-2/+1
* 2735 - define recipes using 'def'Kartik K. Agaram2016-03-081-1/+1
* 2576 - distinguish allocated addresses from othersKartik K. Agaram2016-01-191-1/+2
* 2548 - teach 'print' to print integersKartik K. Agaram2015-12-281-1/+2
* 2468 - overload print-character as just 'print'Kartik K. Agaram2015-11-211-2/+2
* 1868 - start using naked literals everywhereKartik K. Agaram2015-07-281-18/+18
* 1618Kartik K. Agaram2015-06-211-2/+2
* 1617Kartik K. Agaram2015-06-211-7/+7
* 1476 - fake screens support colorKartik K. Agaram2015-05-261-1/+1
* 1363 - rename 'integer' to 'number'Kartik K. Agaram2015-05-131-1/+1
* 1345Kartik K. Agaram2015-05-111-1/+5
* 1276 - make C++ version the defaultKartik K. Agaram2015-05-051-0/+23
} /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#include "hashtable.h"
#include "alloc.h"

typedef struct DataItem {
   uint32 data;
   uint32 key;
   uint8 used;
} DataItem;

typedef struct HashTable {
   DataItem* items;
   uint32 capacity;
} HashTable;

static uint32 hashCode(HashTable* hashTable, uint32 key) {
   return key % hashTable->capacity;
}

HashTable* HashTable_create(uint32 capacity) {
    HashTable* hashTable = kmalloc(sizeof(HashTable));
    memset((uint8*)hashTable, 0, sizeof(HashTable));
    hashTable->capacity = capacity;
    hashTable->items = kmalloc(sizeof(DataItem) * capacity);

    return hashTable;
}

void HashTable_destroy(HashTable* hashTable) {
    kfree(hashTable->items);
    kfree(hashTable);
}

DataItem* HashTable_search_internal(HashTable* hashTable, uint32 key) {
   //get the hash
   uint32 hashIndex = hashCode(hashTable, key);

   uint32 counter = 0;
   while(counter < hashTable->capacity) {
      if(hashTable->items[hashIndex].key == key) {
          if(hashTable->items[hashIndex].used == TRUE) {
              return &(hashTable->items[hashIndex]);
          }
      }

      //go to next cell
      ++hashIndex;

      //wrap around the table
      hashIndex %= hashTable->capacity;

      ++counter;
   }

   return NULL;
}

BOOL HashTable_search(HashTable* hashTable, uint32 key, uint32* value) {
    DataItem* existing = HashTable_search_internal(hashTable, key);

    if (existing) {
        *value = existing->data;

        return TRUE;
    }

    return FALSE;
}

BOOL HashTable_insert(HashTable* hashTable, uint32 key, uint32 data) {
    DataItem* existing = HashTable_search_internal(hashTable, key);

    if (existing) {
        existing->data = data;

        return TRUE;
    }

    //get the hash
    uint32 hashIndex = hashCode(hashTable, key);

    uint32 counter = 0;
    //move in array until an empty or deleted cell
    while(counter < hashTable->capacity) {
        if (hashTable->items[hashIndex].used == FALSE) {
            hashTable->items[hashIndex].key = key;
            hashTable->items[hashIndex].data = data;
            hashTable->items[hashIndex].used = TRUE;

            return TRUE;
        }


        //go to next cell
        ++hashIndex;

        //wrap around the table
        hashIndex %= hashTable->capacity;

        ++counter;
    }

    return FALSE;
}

BOOL HashTable_remove(HashTable* hashTable, uint32 key) {
    DataItem* existing = HashTable_search_internal(hashTable, key);

    if (existing) {
        existing->used = FALSE;

        return TRUE;
    }

    return FALSE;
}