【征集优秀的代码段】

ryfdizuo 2012-12-01 01:10:33
加精
idea来自最近公司内部搜集优秀的代码片段的活动。
最近江南style特火,作为开发人员的我们,长时间的coding肯定已经形成了自己独特的code style。没有标准 没有对错 没有最好 只有更好。欢迎大家来晾下自己优秀的代码片段。下面我先贴一段自己的片段,抛砖引玉~
class GLStroke
{
public:
enum {
SECTION_SEG = 1, // 颜色数组模式下至少两个带才能抗锯齿
PATH_SEG = 1, // 路径方向的段数
VER_SIZE = 2, // x y
COLOR_SIZE = 4, // r g b a
TEXCOORD_SIZE = 2, // u v
TRI_SIZE = 3, // three vertices
IDX_SIZE = 6
};

typedef sVertex Vertex; // 存储采用int
typedef Vec2f RVec; // 计算采用浮点数
typedef unsigned short IType;
typedef void(*PatchProcFuncType)(float*, float*);

GLStroke(Vertex* vBuffer, IType* iBuffer, IType beg);

~GLStroke();

// pV指向顶点坐标
void setupStroke(int* pV, int count, int wid, unsigned int c);

int getVertexNum() const { return verNum; }
int getIndexNum() const { return indiceNum; }
int getIndexBeg() const { return indiceBeg; }

void dumpToFile();

private:

// 开始和结尾处两个截面
void _processSpecialSection(int leftX, int unitX, const RVec& p1, const RVec& p2, int verPos, bool isBegCap=true);

// 取消射线求交方式,直接局部计算
void _setupVertices(int* pV, int count);

// 顶点变换到左手坐标系下 绘制三角形
void _setupIndicesPathFirst();

IType* pTriIndices;
Vertex* pVertex;
IType indiceBeg;

unsigned short indiceNum; // stroke的顶点索引个数
unsigned short verNum; // stroke顶点个数
unsigned short pathSize; // 放样路径的顶点个数

unsigned short width; // stroke的宽度
unsigned int color; // stroke的主色
};

我写的类习惯结构是:enum定义,typedef定义,构造析构其他;私有函数以下划线开始。类型用typedef,常量用枚举,宏一般只有注释代码段使用。

语言不限,内容不限 只要能体现自己的style,真实即可~
...全文
9852 137 打赏 收藏 转发到动态 举报
写回复
用AI写文章
137 条回复
切换为时间正序
请发表友善的回复…
发表回复
liangyangg 2014-08-01
  • 打赏
  • 举报
回复
小弟菜鸟也来晒晒,bug比较多让大家见笑了
package com.et.bothwayLikedList;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
★☆            @author: The One                                                               ☆★
★☆            @time:2014年4月8日 下午7:40:18                                      ☆★
★☆            @version:1.0                                                                        ☆★
★☆            @lastMotifyTime:                                                                 ☆★
★☆            @ClassAnnotation:                                                              ☆★
★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
 */
public class MyLinked<T> extends AbstractList<T> implements List<T> {

   private class Node {
      private Node last;
      private Node next;
      private T value;

      public Node(T value) {
         this.value = value;
      }
   }

   private int size;
   private Node tail;
   private Node root;

   @Override
   public void add(int index, T element) {
      Node newNode = new Node(element);
      if (index == 0) {
         newNode.next = root;
         root.last = newNode;
         root = newNode;
         size++;
         return;
      }
      if (index == size) {
         add(element);
         return;
      }
      Node firstNode = getNode(index).last;
      Node lastNode = firstNode.next;
      firstNode.next = newNode;
      newNode.last = firstNode;
      newNode.next = lastNode;
      lastNode.last = newNode;
      size++;
   }

   @Override
   public boolean add(T element) {
      Node newNode = new Node(element);
      if (root == null) {
         root = newNode;
         tail = newNode;
         size++;
         return true;
      }
      tail.next = newNode;
      newNode.last = tail;
      tail = newNode;
      size++;
      return true;
   }

   @Override
   public boolean addAll(Collection<? extends T> conllection) {
      for (T element : conllection) {
         add(element);
      }
      return true;
   }

   @Override
   public boolean addAll(int index, Collection<? extends T> collection) {
      checkIndex(index);
      for (T c : collection) {
         add(index++, c);
      }
      return true;
   }

   private void checkIndex(int index) {
      if ((index > (size - 1)) || (index < 0) || ((size == 0) && (index == 0))) {
         throw new IndexOutOfBoundsException();
      }
   }

   @Override
   public void clear() {
      size = 0;
      root = null;
      tail = null;
   }

   @Override
   public boolean contains(Object value) {
      Node nextNode = root;
      while (nextNode != null) {
         if (value.equals(nextNode.value)) {
            return true;
         }
         nextNode = nextNode.next;
      }
      return false;
   }

   @Override
   public boolean containsAll(Collection<?> collection) {
      for (Object object : collection) {
         if (!contains(object)) {
            return false;
         }
      }
      return true;
   }

   @Override
   public T get(int index) {
      checkIndex(index);
      T value = getNode(index).value;
      return value;
   }

   private Node getForward(int index) {
      Node tempNext = root;
      int i = 0;
      do {
         if (i == index) {
            return tempNext;
         }
         i++;
         tempNext = tempNext.next;
      } while (tempNext != null);
      return null;
   }

   public Node getNode(int index) {
      checkIndex(index);
      if (index >= (size / 2)) {
         return getReverse(index);
      } else {
         return getForward(index);
      }
   }

   private Node getReverse(int index) {
      Node tempLast = tail;
      int i = size - 1;
      do {
         if (i == index) {
            return tempLast;
         }
         i--;
         tempLast = tempLast.last;
      } while (tempLast != null);
      return null;

   }

   @Override
   public int indexOf(Object value) {
      Node nextNode = root;
      int i = 0;
      do {
         if (value.equals(nextNode.value)) {
            return i;
         }
         i++;
         nextNode = nextNode.next;
      } while (nextNode != null);
      return -1;
   }

   @Override
   public boolean isEmpty() {

      return size == 0;
   }

   @Override
   public int lastIndexOf(Object value) {
      Node lastNode = tail;
      int lastIndex = size - 1;
      do {
         if (value.equals(lastNode.value)) {
            return lastIndex;
         }
         lastIndex--;
         lastNode = lastNode.last;
      } while (lastNode != null);
      return -1;
   }

   @Override
   public T remove(int index) {
      checkIndex(index);
      Node remove = getNode(index);// 当前删除的Node
      Node father = remove.last;// 删除元素的上一个Node
      Node last = remove.next;// 删除元素的下一个Node

      if (root.equals(remove)) {// 删除的是头1:里面有两个以上元素,则头为下一个Node,并把上一个last赋为null。
         // 2:如果只存在一个元素,又要删除该元素,也是把头为下一个Node(此时下一个Node必然为null),因为删除的是该容器的最后一个元素,所以也把tail赋为null
         root = last;
         if (root != null) {
            root.last = null;
         }
         if ((size == 1) && (index == 0)) {
            tail = null;
         }
         size--;
         return remove.value;
      }

      if (tail.equals(remove)) {
         tail = father;
         tail.next = null;
         size--;
         return remove.value;
      }

      father.next = last;
      last.last = father;
      size--;
      return remove.value;
   }

   @Override
   public boolean remove(Object value) {
      Node nextNode = root;
      while (nextNode != null) {
         if (value.equals(nextNode.value)) {
            Node father = nextNode.last;
            Node last = nextNode.next;

            if (root.equals(nextNode)) {
               root = last;
               if (root != null) {
                  root.last = null;
               }
               if ((size == 1) && value.equals(root.value)) {
                  tail = null;
               }
               size--;
               return true;
            }

            if (tail.equals(nextNode)) {
               tail = father;
               tail.next = null;
               size--;
               return true;
            }

            father.next = last;
            last.last = father;
            size--;
            return true;
         }
         nextNode = nextNode.next;
      }
      return false;
   }

   @Override
   public boolean removeAll(Collection<?> collection) {
      for (Object object : collection) {
         remove(object);
      }
      return true;
   }

   @Override
   public T set(int index, T element) {
      Node setNode = getNode(index);
      T setValue = setNode.value;
      setNode.value = element;
      return setValue;
   }

   @Override
   public int size() {
      return size;
   }

   @Override
   public List<T> subList(int fromIndex, int toIndex) {
      if ((fromIndex < 0) || (fromIndex >= size) || (toIndex <= 0) || (toIndex > size)) {
         throw new IndexOutOfBoundsException();
      }
      Node fromNode = getNode(fromIndex);
      List<T> list = new ArrayList<>();

      while (fromNode != null) {
         if (fromIndex == toIndex) {
            break;
         }
         list.add(fromNode.value);
         fromNode = fromNode.next;
         fromIndex++;
      }
      return list;
   }

   @Override
   public Object[] toArray() {
      Object[] objects = subList(0, size).toArray();
      return objects;
   }

}
china_jeffery 2013-01-29
  • 打赏
  • 举报
回复
引用 100 楼 dizuo 的回复:
赞ls的code~ 100层了。
这是在赞我么,
searching999 2013-01-29
  • 打赏
  • 举报
回复
好贴,先mark下
ryfdizuo 2012-12-27
  • 打赏
  • 举报
回复
引用 133 楼 Big_Wang5 的回复:
看看到几楼了
结贴、疯楼啦。。。
bigwangdi 2012-12-26
  • 打赏
  • 举报
回复
看看到几楼了
recher2010 2012-12-22
  • 打赏
  • 举报
回复
alter("LZ"+"码畜")我接触最多的就是这,我擦,还不经典
赵4老师 2012-12-13
  • 打赏
  • 举报
回复
比用什么风格更重要的是在任何地方,任何时候都坚持用一种风格。 比爱哪种美眉更重要的是在任何地方,任何时候都坚持爱一个美眉。
仙境之桥2046 2012-12-13
  • 打赏
  • 举报
回复
引用 122 楼 dizuo 的回复:
引用 120 楼 lovenanforever 的回复:语言和行业不同怎么评价优秀 这个不矛盾哪,代码与诗歌十分类似,英文中的优秀诗歌我们要用英文来理解,中文的经典诗歌我们要通过中文来理解,看不懂莎士比亚的诗歌并不代表他的诗歌不优秀。
有道理
shiter 2012-12-13
  • 打赏
  • 举报
回复
引用 129 楼 zhao4zhong1 的回复:
比用什么风格更重要的是在任何地方,任何时候都坚持用一种风格。 比爱哪种美眉更重要的是在任何地方,任何时候都坚持爱一个美眉。
赞一下第二句。。。
bigbaldy 2012-12-12
  • 打赏
  • 举报
回复
引用 118 楼 forrest23 的回复:
再来段C#的代码 C# 遍历获取指定路径下文件夹和文件 C# code?1234567891011121314151617181920212223242526272829303132private void Form1_Load(object sender, EventArgs e) { string FilePuth = "C……
不错,比我写的好
bigbaldy 2012-12-12
  • 打赏
  • 举报
回复
引用 33 楼 suzhijie325 的回复:
看到以下划线开头的变量或者函数就恶心。 下划线开头的变量一般都是留给编译器使用的, 那天你的变量或者函数和编译器库中的同名你就杯具了。
同意,一大堆__,实在头大
swlilike 2012-12-12
  • 打赏
  • 举报
回复
看着真心舒坦,就是,所有的代码都一样吗?
ryfdizuo 2012-12-12
  • 打赏
  • 举报
回复
引用 124 楼 swlilike 的回复:
看着真心舒坦,就是,所有的代码都一样吗?
工整的code,命名高度一致,效果与作品中的排比句十分类似,最起码气势出来了。。。
dswjava1 2012-12-12
  • 打赏
  • 举报
回复
学习一下,瞻仰各位大神
cocoabird 2012-12-11
  • 打赏
  • 举报
回复
// WebServerInfo.h: interface for the CWebServerInfo class.
//
//////////////////////////////////////////////////////////////////////

#ifndef __WebServerInfo_H__
#define __WebServerInfo_H__

#pragma once

class CWebServerInfo
{
public:
	CWebServerInfo(void);
	~CWebServerInfo(void);

	int GetIISVersion(CString& strIISVersion);
	int GetApacheVersion(CString& strApacheVersion);
	int GetIISLogEventFiles(CString& strIISLogEevntFile);
	char** EnumFiles(const char *directory, int *count);

private:
	int GetIISLogFileDirectory();

	int GetApacheServerRoot(HKEY hKey, CString strSubKey, CString& strServerRoot);
	int GetApacheLogFileDirectory(CString strServerRoot);


	static	bool	IsIISInstalled();

private:
	DWORD m_dwIISMajorVersion;
};

#endif //__WebServerInfo_H__
该用户比较懒 2012-12-11
  • 打赏
  • 举报
回复
菜鸟,收藏了先
duxinfeng2010 2012-12-11
  • 打赏
  • 举报
回复
see see
wenzheng101 2012-12-11
  • 打赏
  • 举报
回复
很多牛人啊, 学习下你们的优秀代码风格
ryfdizuo 2012-12-11
  • 打赏
  • 举报
回复
引用 120 楼 lovenanforever 的回复:
语言和行业不同怎么评价优秀
这个不矛盾哪,代码与诗歌十分类似,英文中的优秀诗歌我们要用英文来理解,中文的经典诗歌我们要通过中文来理解,看不懂莎士比亚的诗歌并不代表他的诗歌不优秀。
hello_world000 2012-12-11
  • 打赏
  • 举报
回复
linux中部分代码:

#define __define_initcall(level,fn,id)                      \
static initcall_t __initcall_##fn##id __used                \
__attribute__((__section__(".initcall" level ".init"))) = fn
#define early_initcall(fn)		    __define_initcall("early",fn,early)
#define pure_initcall(fn)		    __define_initcall("0",fn,0)
#define core_initcall(fn)		    __define_initcall("1",fn,1)
#define core_initcall_sync(fn)		__define_initcall("1s",fn,1s)
#define postcore_initcall(fn)		__define_initcall("2",fn,2)
#define postcore_initcall_sync(fn)	__define_initcall("2s",fn,2s)
#define arch_initcall(fn)		    __define_initcall("3",fn,3)
#define arch_initcall_sync(fn)		__define_initcall("3s",fn,3s)
#define subsys_initcall(fn)		    __define_initcall("4",fn,4)
#define subsys_initcall_sync(fn)	__define_initcall("4s",fn,4s)
#define fs_initcall(fn)			    __define_initcall("5",fn,5)
#define fs_initcall_sync(fn)		__define_initcall("5s",fn,5s)
#define rootfs_initcall(fn)		    __define_initcall("rootfs",fn,rootfs)
#define device_initcall(fn)		    __define_initcall("6",fn,6)
#define device_initcall_sync(fn)    __define_initcall("6s",fn,6s)
#define late_initcall(fn)		    __define_initcall("7",fn,7)
#define late_initcall_sync(fn)		__define_initcall("7s",fn,7s)
#define __initcall(fn) device_initcall(fn)
#define __exitcall(fn) \
static exitcall_t __exitcall_##fn __exit_call = fn
#define console_initcall(fn)                                \
static initcall_t __initcall_##fn                           \
__used __section(.con_initcall.init) = fn
#define security_initcall(fn)                               \
static initcall_t __initcall_##fn                           \
__used __section(.security_initcall.init) = fn
#define __setup_param(str, unique_id, fn, early)			\
static const char __setup_str_##unique_id[] __initconst	    \
__aligned(1) = str;                                         \
static struct obs_kernel_param __setup_##unique_id	        \
__used __section(.init.setup)			                    \
__attribute__((aligned((sizeof(long)))))	                \
= { __setup_str_##unique_id, fn, early }
加载更多回复(116)

15,440

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧