Exemple #1
0
 private void lru_insert(head_t h) {
   // insert to last position
   h.next = lru_head;
   h.prev = lru_head.prev;
   h.prev.next = h;
   h.next.prev = h;
 }
Exemple #2
0
        public int GetData(int index, ref float[] data, int len)
        {
            head_t head_t = this.head[index];

            if (head_t.len > 0)
            {
                this.lru_delete(head_t);
            }
            int num = len - head_t.len;

            if (num > 0)
            {
                while (this._size < num)
                {
                    head_t next = this.lru_head.next;
                    this.lru_delete(next);
                    this._size += next.len;
                    next.data   = null;
                    next.len    = 0;
                }
                float[] array = new float[len];
                if (head_t.data != null)
                {
                    Array.Copy(head_t.data, 0, array, 0, head_t.len);
                }
                head_t.data = array;
                this._size -= num;
                Cache.swap <int>(ref head_t.len, ref len);
            }
            this.lru_insert(head_t);
            data = head_t.data;
            return(len);
        }
Exemple #3
0
 private void lru_insert(head_t h)
 {
     h.next      = this.lru_head;
     h.prev      = this.lru_head.prev;
     h.prev.next = h;
     h.next.prev = h;
 }
Exemple #4
0
 public Cache(int l_, long size_) {
   l = l_;
   size = size_;
   head = new head_t[l];
   for (int i = 0; i < l; i++) head[i] = new head_t();
   size /= 4;
   size -= l * (16 / 4);	// sizeof(head_t) == 16
   size = Math.Max(size, 2 * (long)l);  // cache must be large enough for two columns
   lru_head = new head_t();
   lru_head.next = lru_head.prev = lru_head;
 }
Exemple #5
0
		internal Cache(int l_, int size_)
		{
			l = l_;
			size = size_;
			head = new head_t[l];
			for (int i = 0; i < l; i++)
				head[i] = new head_t(this);
			size /= 4;
			size -= l * (16 / 4); // sizeof(head_t) == 16
			lru_head = new head_t(this);
			lru_head.next = lru_head.prev = lru_head;
		}
Exemple #6
0
 public Cache(int length, long size)
 {
     _length = length;
       _size = size;
       head = new head_t[_length];
       for (int i = 0; i < _length; i++) head[i] = new head_t();
       _size /= 4;
       _size -= _length * (16 / 4);  // sizeof(head_t) == 16
       _size = Math.Max(_size, 2 * (long)_length);  // cache must be large enough for two columns
       lru_head = new head_t();
       lru_head.next = lru_head.prev = lru_head;
 }
Exemple #7
0
        public void swap_index(int i, int j)
        {
            if (i == j)
            {
                return;
            }

            if (head[i].len > 0)
            {
                lru_delete(head[i]);
            }
            if (head[j].len > 0)
            {
                lru_delete(head[j]);
            }

            Common.Swap(ref head[i].data, ref head[j].data);
            Common.Swap(ref head[i].len, ref head[j].len);

            if (head[i].len > 0)
            {
                lru_insert(head[i]);
            }
            if (head[j].len > 0)
            {
                lru_insert(head[j]);
            }

            if (i > j)
            {
                Common.Swap(ref i, ref j);
            }

            for (head_t h = lru_head.next; h != lru_head; h = h.next)
            {
                if (h.len > i)
                {
                    if (h.len > j)
                    {
                        Common.Swap(ref h.data[i], ref h.data[j]);
                    }
                    else
                    {
                        // give up
                        lru_delete(h);
                        size  += h.len;
                        h.data = null;
                        h.len  = 0;
                    }
                }
            }
        }
Exemple #8
0
 public Cache(int count, long size)
 {
     this._count = count;
     this._size  = size;
     this.head   = new head_t[this._count];
     for (int i = 0; i < this._count; i++)
     {
         this.head[i] = new head_t(this);
     }
     this._size        /= 4L;
     this._size        -= this._count * 4;
     this.lru_head      = new head_t(this);
     this.lru_head.next = (this.lru_head.prev = this.lru_head);
 }
Exemple #9
0
        public virtual void swap_index(int i, int j)
        {
            if (i == j)
            {
                return;
            }

            if (head[i].len > 0)
            {
                lru_delete(head[i]);
            }
            if (head[j].len > 0)
            {
                lru_delete(head[j]);
            }
            { float[] _ = head[i].data; head[i].data = head[j].data; head[j].data = _; }
            { int _ = head[i].len; head[i].len = head[j].len; head[j].len = _; }
            if (head[i].len > 0)
            {
                lru_insert(head[i]);
            }
            if (head[j].len > 0)
            {
                lru_insert(head[j]);
            }

            if (i > j)
            {
                int _ = i; i = j; j = _;
            }
            for (head_t h = lru_head.next; h != lru_head; h = h.next)
            {
                if (h.len > i)
                {
                    if (h.len > j)
                    {
                        float _ = h.data[i]; h.data[i] = h.data[j]; h.data[j] = _;
                    }
                    else
                    {
                        // give up
                        lru_delete(h);
                        size  += h.len;
                        h.data = null;
                        h.len  = 0;
                    }
                }
            }
        }
Exemple #10
0
 public Cache(int l_, long size_)
 {
     l    = l_;
     size = size_;
     head = new head_t[l];
     for (int i = 0; i < l; i++)
     {
         head[i] = new head_t();
     }
     size         /= 4;
     size         -= l * (16 / 4);                       // sizeof(head_t) == 16
     size          = System.Math.Max(size, 2 * (long)l); // cache must be large enough for two columns
     lru_head      = new head_t();
     lru_head.next = lru_head.prev = lru_head;
 }
Exemple #11
0
 public void SwapIndex(int i, int j)
 {
     if (i != j)
     {
         if (this.head[i].len > 0)
         {
             this.lru_delete(this.head[i]);
         }
         if (this.head[j].len > 0)
         {
             this.lru_delete(this.head[j]);
         }
         Cache.swap <float[]>(ref this.head[i].data, ref this.head[j].data);
         Cache.swap <int>(ref this.head[i].len, ref this.head[j].len);
         if (this.head[i].len > 0)
         {
             this.lru_insert(this.head[i]);
         }
         if (this.head[j].len > 0)
         {
             this.lru_insert(this.head[j]);
         }
         if (i > j)
         {
             Cache.swap(ref i, ref j);
         }
         for (head_t next = this.lru_head.next; next != this.lru_head; next = next.next)
         {
             if (next.len > i)
             {
                 if (next.len > j)
                 {
                     Cache.swap <float>(ref next.data[i], ref next.data[j]);
                 }
                 else
                 {
                     this.lru_delete(next);
                     this._size += next.len;
                     next.data   = null;
                     next.len    = 0;
                 }
             }
         }
     }
 }
Exemple #12
0
        // request data [0,len)
        // return some position p where [p,len) need to be filled
        // (p >= len if nothing needs to be filled)
        // java: simulate pointer using single-element array
        public int get_data(int index, float[][] data, int len)
        {
            head_t h = head[index];

            if (h.len > 0)
            {
                lru_delete(h);
            }
            int more = len - h.len;

            if (more > 0)
            {
                // free old space
                while (size < more)
                {
                    head_t old = lru_head.next;
                    lru_delete(old);
                    size    += old.len;
                    old.data = null;
                    old.len  = 0;
                }

                // allocate new space
                float[] new_data = new float[len];
                if (h.data != null)
                {
                    for (int i = 0; i < h.len; ++i)
                    {
                        new_data[i] = h.data[i];
                    }
                }
                h.data = new_data;
                size  -= more;
                do
                {
                    int tmp = h.len; h.len = len; len = tmp;
                } while (false);
            }

            lru_insert(h);
            data[0] = h.data;
            return(len);
        }
Exemple #13
0
        // request data [0,len)
        // return some position p where [p,len) need to be filled
        // (p >= len if nothing needs to be filled)
        public int get_data(int index, out double[] data, int len)
        {
            head_t h = head[index];

            if (h.len > 0)
            {
                lru_delete(h);
            }
            int more = len - h.len;

            if (more > 0)
            {
                // free old space
                while (size < more)
                {
                    head_t old = lru_head.next;
                    lru_delete(old);
                    size    += old.len;
                    old.data = null;
                    old.len  = 0;
                }

                // allocate new space
                double[] new_data = new double[len];

                if (h.data != null)
                {
                    Array.Copy(h.data, 0, new_data, 0, h.len);
                }
                h.data = new_data;
                size  -= more;

                Common.Swap(ref h.len, ref len);
            }

            lru_insert(h);
            data = h.data;
            return(len);
        }
Exemple #14
0
        // request data [0,len)
        // return some position p where [p,len) need to be filled
        // (p >= len if nothing needs to be filled)
        // java: simulate pointer using single-element array
        public virtual int get_data(int index, float[][] data, int len)
        {
            head_t h = head[index];

            if (h.len > 0)
            {
                lru_delete(h);
            }
            int more = len - h.len;

            if (more > 0)
            {
                // free old space
                while (size < more)
                {
                    head_t old = lru_head.next;
                    lru_delete(old);
                    size    += old.len;
                    old.data = null;
                    old.len  = 0;
                }

                // allocate new space
                float[] new_data = new float[len];
                if (h.data != null)
                {
                    System.Array.Copy(h.data, 0, new_data, 0, h.len);
                }
                h.data = new_data;
                size  -= more;
                { int _ = h.len; h.len = len; len = _; }
            }

            lru_insert(h);
            data[0] = h.data;
            return(len);
        }
Exemple #15
0
        public void swap_index(int i, int j)
        {
            if (i == j)
            {
                return;
            }

            if (head[i].len > 0)
            {
                lru_delete(head[i]);
            }
            if (head[j].len > 0)
            {
                lru_delete(head[j]);
            }
            do
            {
                float[] tmp = head[i].data; head[i].data = head[j].data; head[j].data = tmp;
            } while (false);
            do
            {
                int tmp = head[i].len; head[i].len = head[j].len; head[j].len = tmp;
            } while (false);
            if (head[i].len > 0)
            {
                lru_insert(head[i]);
            }
            if (head[j].len > 0)
            {
                lru_insert(head[j]);
            }

            if (i > j)
            {
                do
                {
                    int tmp = i; i = j; j = tmp;
                } while (false);
            }
            for (head_t h = lru_head.next; h != lru_head; h = h.next)
            {
                if (h.len > i)
                {
                    if (h.len > j)
                    {
                        do
                        {
                            float tmp = h.data[i]; h.data[i] = h.data[j]; h.data[j] = tmp;
                        } while (false);
                    }
                    else
                    {
                        // give up
                        lru_delete(h);
                        size  += h.len;
                        h.data = null;
                        h.len  = 0;
                    }
                }
            }
        }
Exemple #16
0
    public static unsafe bool LoadSpr(string filename, byte[] filedata, ref List <Texture2D> SprResList)
    {
        //Debug.LogError("LoadSpr" + filename);
        //Head
        head_t head = (head_t)BytesToStruct(filedata, typeof(head_t));

        //Debug.Log(head.width);

        if (head.reserved[0] != 0)
        {
            Debug.LogError("NEW FRAME!!!" + filename);
        }

        //Palette Colors
        int colors = head.pal_entry_count;

        //Debug.Log("Color: " + colors);
        Color32[] PaletteColors = new Color32[colors];
        long      off           = 0;

        fixed(byte *data = &filedata[32])  // sizeof(head_t)
        {
            byte *c = data;
            byte  a = 0xff;

            for (int i = 0; i < colors; i++)
            {
                byte r, g, b;
                r = *c++;
                g = *c++;
                b = *c++;
                PaletteColors[i] = new Color32(r, g, b, a);
                //Debug.Log(string.Format("Color[{0}]: {1}", i, PaletteColors[i]));
            }
            off = c - data + 32;
        }

        // frame offset
        int frameCount = head.frame_count;

        offset_table_t[] frames = new offset_table_t[frameCount];
        fixed(byte *data = &filedata[off])
        {
            int *pdata = (int *)data;

            for (int i = 0; i < frameCount; i++)
            {
                frames[i].offset = *pdata++;
                frames[i].size   = *pdata++;
                //Debug.Log(string.Format("Frames {0}: [{1}, {2}]", i, frames[i].offset, frames[i].size));
            }
            off += (byte *)pdata - data;
        }

        // frames
        int w = head.width;
        int h = head.height;

        fixed(byte *data = &filedata[off])
        {
            byte *c = data;

            for (int f = 0; f < head.frame_count; f++)
            {
                SPRFRAME_NEW spr = (SPRFRAME_NEW)Marshal.PtrToStructure(new IntPtr(c), typeof(SPRFRAME_NEW));
                c += 8;
                w  = spr.Width;
                h  = spr.Height;
                //Debug.Log(string.Format("new frame {0} x {1}", w, h));
                Color32[] pixels    = new Color32[w * h];
                int       nNumLines = h;
                int       p         = 0;
                //for (; nNumLines > 0; nNumLines--)
                for (int l = h - 1; l >= 0; l--)
                {
                    int nLineLen = w;
                    while (nLineLen > 0)
                    {
                        int nNumPixels = *c++;
                        int nAlpha     = *c++;
                        nLineLen -= nNumPixels;
                        for (int n = 0; n < nNumPixels; n++)
                        {
                            try
                            {
                                //p = l * w + n;
                                if (nAlpha == 0)
                                {
                                    pixels[p++] = new Color32(0, 0, 0, 0);
                                }
                                else
                                {
                                    pixels[p++] = PaletteColors[*c++];
                                }
                            }
                            catch (Exception e)
                            {
                                Debug.LogError(e);
                                Debug.Log(l);
                                Debug.Log(n);
                                Debug.Log(p);
                                Debug.Log(w * h);
                                return(false);
                            }
                        }
                    }
                }
                Color32[] pixels2 = new Color32[w * h];
                for (int l = 0; l < h; l++)
                {
                    for (int k = 0; k < w; k++)
                    {
                        pixels2[l * w + k] = pixels[(h - l - 1) * w + k];
                    }
                }

                //Debug.Log(p);
                //Debug.Log(c - data);
                Texture2D tx = new Texture2D(w, h);
                tx.SetPixels32(pixels2);
                tx.Apply(false);

                SprResList.Add(tx);
            }
        }

        //Debug.Log(string.Format("Load {0} Done. frame={1}", filename, head.frame_count));
        return(true);
    }
Exemple #17
0
 private void lru_delete(head_t h)
 {
     // delete from current location
     h.prev.next = h.next;
     h.next.prev = h.prev;
 }
Exemple #18
0
 private void lru_delete(head_t h)
 {
     h.prev.next = h.next;
     h.next.prev = h.prev;
 }